bot.dialog('phonePrompt', [
function (session, args) {
if (args && args.reprompt) {
builder.Prompts.text(session, "Enter the number using a format of either: '(555) 123-4567' or '555-123-4567' or '5551234567'")
} else {
builder.Prompts.text(session, "What's your phone number?");
}
},
function (session, results) {
var matched = results.response.match(/\d+/g);
var number = matched ? matched.join('') : '';
if (number.length == 10 || number.length == 11) {
session.userData.phoneNumber = number; // Save the number.
session.endDialogWithResult({ response: number });
} else {
// Repeat the dialog
session.replaceDialog('phonePrompt', { reprompt: true });
}
}
]);
未輸入正確的格式,將會一直重問
{ reprompt: true } //表現session.replaceDialog也可以塞參數
var inMemoryStorage = new builder.MemoryBotStorage();
// This is a reservation bot that has a menu of offerings.
var bot = new builder.UniversalBot(connector, [
function(session){
session.send("Welcome to Contoso Hotel and Resort.");
session.beginDialog("mainMenu");
}
]).set('storage', inMemoryStorage); // Register in-memory storage
// Display the main menu and start a new request depending on user input.
bot.dialog("mainMenu", [
function(session){
builder.Prompts.choice(session, "Main Menu:", menuItems);
},
function(session, results){
if(results.response){
session.beginDialog(menuItems[results.response.entity].item);
}
}
])
.triggerAction({
// The user can request this at any time.
// Once triggered, it clears the stack and prompts the main menu again.
matches: /^main menu$/i,
confirmPrompt: "This will cancel your request. Are you sure?"
});
可以隨時在子dialog stack 呼叫 main menu
var inMemoryStorage = new builder.MemoryBotStorage();
// This is a reservation bot that has a menu of offerings.
var bot = new builder.UniversalBot(connector, [
function(session){
session.send("Welcome to Contoso Hotel and Resort.");
session.beginDialog("mainMenu");
}
]).set('storage', inMemoryStorage); // Register in-memory storage
// Display the main menu and start a new request depending on user input.
bot.dialog("mainMenu", [
function(session){
builder.Prompts.choice(session, "Main Menu:", menuItems);
},
function(session, results){
if(results.response){
session.beginDialog(menuItems[results.response.entity].item);
}
}
])
.triggerAction({
// The user can request this at any time.
// Once triggered, it clears the stack and prompts the main menu again.
matches: /^main menu$/i,
confirmPrompt: "This will cancel your request. Are you sure?"
});
// Main menu
var menuItems = {
"Order dinner": {
item: "orderDinner"
},
"Dinner reservation": {
item: "dinnerReservation"
},
"Schedule shuttle": {
item: "scheduleShuttle"
},
"Request wake-up call": {
item: "wakeupCall"
},
}
// The dinner menu
var dinnerMenu = {
//...other menu items...,
"Check out": {
Description: "Check out",
Price: 0 // Order total. Updated as items are added to order.
},
"Shit": {
Description: "Food",
Price: 100 // Order total. Updated as items are added to order.
},
"Pin": {
Description: "Drink",
Price: 10 // Order total. Updated as items are added to order.
}
};
bot.dialog("addDinnerItem", [
function(session, args){
if(args && args.reprompt){
session.send("What else would you like to have for dinner tonight?");
}
else{
// New order
// Using the conversationData to store the orders
session.conversationData.orders = new Array();
session.conversationData.orders.push({
Description: "Check out",
Price: 0
})
}
builder.Prompts.choice(session, "Dinner menu:", dinnerMenu);
},
function(session, results){
if(results.response){
if(results.response.entity.match(/^check out$/i)){
session.endDialog("Checking out...");
}
else {
var order = dinnerMenu[results.response.entity];
session.conversationData.orders[0].Price += order.Price; // Add to total.
var msg = `You ordered: ${order.Description} for a total of $${order.Price}.`;
session.send(msg);
session.conversationData.orders.push(order);
session.replaceDialog("addDinnerItem", { reprompt: true }); // Repeat dinner menu
}
}
}
])
.reloadAction(
"restartOrderDinner", "Ok. Let's start over.",
{
matches: /^start over$/i
}
);
// Menu: "Order dinner"
// This dialog allows user to order dinner and have it delivered to their room.
bot.dialog('orderDinner', [
function(session){
session.send("Lets order some dinner!");
session.beginDialog("addDinnerItem");
},
function (session, results) {
if (results.response) {
// Display itemize order with price total.
for(var i = 1; i < session.conversationData.orders.length; i++){
session.send(`You ordered: ${session.conversationData.orders[i].Description} for a total of $${session.conversationData.orders[i].Price}.`);
}
session.send(`Your total is: $${session.conversationData.orders[0].Price}`);
// Continue with the check out process.
builder.Prompts.text(session, "What is your room number?");
}
},
function(session, results){
if(results.response){
session.dialogData.room = results.response;
var msg = `Thank you. Your order will be delivered to room #${results.response}`;
session.send(msg);
session.replaceDialog("mainMenu");
}
}
])
完整範例!
可以籍由操作 conversationData 來做所有的設定。