Skip to content

Commit 81d8aa1

Browse files
committed
Updated demo-skype example to use actions.
1 parent 9c81ec7 commit 81d8aa1

File tree

1 file changed

+89
-71
lines changed
  • Node/examples/demo-skype

1 file changed

+89
-71
lines changed

Node/examples/demo-skype/app.js

Lines changed: 89 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,20 @@ bot.on('deleteUserData', function (message) {
111111
// Anytime the major version is incremented any existing conversations will be restarted.
112112
bot.use(builder.Middleware.dialogVersion({ version: 1.0, resetCommand: /^reset/i }));
113113

114+
//=========================================================
115+
// Bots Global Actions
116+
//=========================================================
117+
118+
bot.endConversationAction('goodbye', 'Goodbye :)', { matches: /^goodbye/i });
119+
bot.beginDialogAction('help', '/help', { matches: /^help/i });
120+
114121
//=========================================================
115122
// Bots Dialogs
116123
//=========================================================
117124

118125
bot.dialog('/', [
119126
function (session) {
120-
// Send a greeting and start the menu.
127+
// Send a greeting and show help.
121128
var card = new builder.HeroCard(session)
122129
.title("Microsoft Bot Framework")
123130
.text("Your bots - wherever your users are talking.")
@@ -127,6 +134,10 @@ bot.dialog('/', [
127134
var msg = new builder.Message(session).attachments([card]);
128135
session.send(msg);
129136
session.send("Hi... I'm the Microsoft Bot Framework demo bot for Skype. I can show you everything you can use our Bot Builder SDK to do on Skype.");
137+
session.beginDialog('/help');
138+
},
139+
function (session, results) {
140+
// Display menu
130141
session.beginDialog('/menu');
131142
},
132143
function (session, results) {
@@ -137,7 +148,7 @@ bot.dialog('/', [
137148

138149
bot.dialog('/menu', [
139150
function (session) {
140-
builder.Prompts.choice(session, "What demo would you like to run?", "prompts|picture|cards|list|carousel|receipt|(quit)");
151+
builder.Prompts.choice(session, "What demo would you like to run?", "prompts|picture|cards|list|carousel|receipt|actions|(quit)");
141152
},
142153
function (session, results) {
143154
if (results.response && results.response.entity != '(quit)') {
@@ -152,6 +163,12 @@ bot.dialog('/menu', [
152163
// The menu runs a loop until the user chooses to (quit).
153164
session.replaceDialog('/menu');
154165
}
166+
]).reloadAction('reloadMenu', null, { matches: /^menu|show menu/i });
167+
168+
bot.dialog('/help', [
169+
function (session) {
170+
session.endDialog("Global commands that are available anytime:\n\n* menu - Exits a demo and returns to the menu.\n* goodbye - End this conversation.\n* help - Displays these commands.");
171+
}
155172
]);
156173

157174
bot.dialog('/prompts', [
@@ -160,65 +177,37 @@ bot.dialog('/prompts', [
160177
builder.Prompts.text(session, "Prompts.text()\n\nEnter some text and I'll say it back.");
161178
},
162179
function (session, results) {
163-
if (results && results.response) {
164-
session.send("You entered '%s'", results.response);
165-
builder.Prompts.number(session, "Prompts.number()\n\nNow enter a number.");
166-
} else {
167-
session.endDialog("You canceled.");
168-
}
180+
session.send("You entered '%s'", results.response);
181+
builder.Prompts.number(session, "Prompts.number()\n\nNow enter a number.");
169182
},
170183
function (session, results) {
171-
if (results && results.response) {
172-
session.send("You entered '%s'", results.response);
173-
session.send("Bot Builder includes a rich choice() prompt that lets you offer a user a list choices to pick from. On Facebook these choices by default surface using buttons if there are 3 or less choices. If there are more than 3 choices a numbered list will be used but you can specify the exact type of list to show using the ListStyle property.");
174-
builder.Prompts.choice(session, "Prompts.choice()\n\nChoose a list style (the default is auto.)", "auto|inline|list|button|none");
175-
} else {
176-
session.endDialog("You canceled.");
177-
}
184+
session.send("You entered '%s'", results.response);
185+
session.send("Bot Builder includes a rich choice() prompt that lets you offer a user a list choices to pick from. On Facebook these choices by default surface using buttons if there are 3 or less choices. If there are more than 3 choices a numbered list will be used but you can specify the exact type of list to show using the ListStyle property.");
186+
builder.Prompts.choice(session, "Prompts.choice()\n\nChoose a list style (the default is auto.)", "auto|inline|list|button|none");
178187
},
179188
function (session, results) {
180-
if (results && results.response) {
181-
var style = builder.ListStyle[results.response.entity];
182-
builder.Prompts.choice(session, "Prompts.choice()\n\nNow pick an option.", "option A|option B|option C", { listStyle: style });
183-
} else {
184-
session.endDialog("You canceled.");
185-
}
189+
var style = builder.ListStyle[results.response.entity];
190+
builder.Prompts.choice(session, "Prompts.choice()\n\nNow pick an option.", "option A|option B|option C", { listStyle: style });
186191
},
187192
function (session, results) {
188-
if (results && results.response) {
189-
session.send("You chose '%s'", results.response.entity);
190-
builder.Prompts.confirm(session, "Prompts.confirm()\n\nSimple yes/no questions are possible. Answer yes or no now.");
191-
} else {
192-
session.endDialog("You canceled.");
193-
}
193+
session.send("You chose '%s'", results.response.entity);
194+
builder.Prompts.confirm(session, "Prompts.confirm()\n\nSimple yes/no questions are possible. Answer yes or no now.");
194195
},
195196
function (session, results) {
196-
if (results && results.resumed == builder.ResumeReason.completed) {
197-
session.send("You chose '%s'", results.response ? 'yes' : 'no');
198-
builder.Prompts.time(session, "Prompts.time()\n\nThe framework can recognize a range of times expressed as natural language. Enter a time like 'Monday at 7am' and I'll show you the JSON we return.");
199-
} else {
200-
session.endDialog("You canceled.");
201-
}
197+
session.send("You chose '%s'", results.response ? 'yes' : 'no');
198+
builder.Prompts.time(session, "Prompts.time()\n\nThe framework can recognize a range of times expressed as natural language. Enter a time like 'Monday at 7am' and I'll show you the JSON we return.");
202199
},
203200
function (session, results) {
204-
if (results && results.response) {
205-
session.send("Recognized Entity: %s", JSON.stringify(results.response));
206-
builder.Prompts.attachment(session, "Prompts.attachment()\n\nYour bot can wait on the user to upload an image or video. Send me an image and I'll send it back to you.");
207-
} else {
208-
session.endDialog("You canceled.");
209-
}
201+
session.send("Recognized Entity: %s", JSON.stringify(results.response));
202+
builder.Prompts.attachment(session, "Prompts.attachment()\n\nYour bot can wait on the user to upload an image or video. Send me an image and I'll send it back to you.");
210203
},
211204
function (session, results) {
212-
if (results && results.response) {
213-
var msg = new builder.Message(session)
214-
.ntext("I got %d attachment.", "I got %d attachments.", results.response.length);
215-
results.response.forEach(function (attachment) {
216-
msg.addAttachment(attachment);
217-
});
218-
session.endDialog(msg);
219-
} else {
220-
session.endDialog("You canceled.");
221-
}
205+
var msg = new builder.Message(session)
206+
.ntext("I got %d attachment.", "I got %d attachments.", results.response.length);
207+
results.response.forEach(function (attachment) {
208+
msg.addAttachment(attachment);
209+
});
210+
session.endDialog(msg);
222211
}
223212
]);
224213

@@ -340,29 +329,25 @@ bot.dialog('/carousel', [
340329
builder.Prompts.choice(session, msg, "select:100|select:101|select:102");
341330
},
342331
function (session, results) {
343-
if (results.response) {
344-
var action, item;
345-
var kvPair = results.response.entity.split(':');
346-
switch (kvPair[0]) {
347-
case 'select':
348-
action = 'selected';
349-
break;
350-
}
351-
switch (kvPair[1]) {
352-
case '100':
353-
item = "the <b>Space Needle</b>";
354-
break;
355-
case '101':
356-
item = "<b>Pikes Place Market</b>";
357-
break;
358-
case '101':
359-
item = "the <b>EMP Museum</b>";
360-
break;
361-
}
362-
session.endDialog('You %s "%s"', action, item);
363-
} else {
364-
session.endDialog("You canceled.");
332+
var action, item;
333+
var kvPair = results.response.entity.split(':');
334+
switch (kvPair[0]) {
335+
case 'select':
336+
action = 'selected';
337+
break;
338+
}
339+
switch (kvPair[1]) {
340+
case '100':
341+
item = "the <b>Space Needle</b>";
342+
break;
343+
case '101':
344+
item = "<b>Pikes Place Market</b>";
345+
break;
346+
case '101':
347+
item = "the <b>EMP Museum</b>";
348+
break;
365349
}
350+
session.endDialog('You %s "%s"', action, item);
366351
}
367352
]);
368353

@@ -422,3 +407,36 @@ bot.dialog('/signin', [
422407
session.endDialog(msg);
423408
}
424409
]);
410+
411+
412+
bot.dialog('/actions', [
413+
function (session) {
414+
session.send("Bots can register global actions, like the 'help' & 'goodbye' actions, that can respond to user input at any time. You can even bind actions to buttons on a card.");
415+
416+
var msg = new builder.Message(session)
417+
.textFormat(builder.TextFormat.xml)
418+
.attachments([
419+
new builder.HeroCard(session)
420+
.title("Hero Card")
421+
.subtitle("Space Needle")
422+
.text("The <b>Space Needle</b> is an observation tower in Seattle, Washington, a landmark of the Pacific Northwest, and an icon of Seattle.")
423+
.images([
424+
builder.CardImage.create(session, "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/Seattlenighttimequeenanne.jpg/320px-Seattlenighttimequeenanne.jpg")
425+
])
426+
.buttons([
427+
builder.CardAction.dialogAction(session, "weather", "Seattle, WA", "Current Weather")
428+
])
429+
]);
430+
session.send(msg);
431+
432+
session.endDialog("The 'Current Weather' button on the card above can be pressed at any time regardless of where the user is in the conversation with the bot. The bot can even show the weather after the conversation has ended.");
433+
}
434+
]);
435+
436+
// Create a dialog and bind it to a global action
437+
bot.dialog('/weather', [
438+
function (session, args) {
439+
session.endDialog("The weather in %s is 71 degrees and raining.", args.data);
440+
}
441+
]);
442+
bot.beginDialogAction('weather', '/weather'); // <-- no 'matches' option means this can only be triggered by a button.

0 commit comments

Comments
 (0)