Skip to content

Commit

Permalink
First draft of dialog samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Harrison committed Feb 22, 2017
0 parents commit 0aafc5c
Show file tree
Hide file tree
Showing 12 changed files with 511 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Dialog sample Bot

This bot has been created using [Microsoft Bot Framework](https://dev.botframework.com), and scaffolded using the [Bot Builder Yeoman generator](https://github.com/GeekTrainer/generator-botbuilder).

This bot is designed to do the following:

A sample bot to demonstrate dialogs

## Getting Started

### Structure

`app.js` references the bot and starts a [Restify](http://restify.com/) server. `bot.js` has a simple multi-turn dialog which sends the name and description of the bot, and then asks the user for their name.

### Running the bot

```
node app.js
```

### Configuring the bot

The template uses [dotenv](https://github.com/motdotla/dotenv) for managing application settings.
9 changes: 9 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const restify = require('restify');
require('dotenv').config();
const bot = require('./bot.js');

const server = restify.createServer();
server.post('/api/messages', bot.connector('*').listen());
server.listen(process.env.PORT, () => {
console.log(`${server.name} listening to ${server.url}`);
});
84 changes: 84 additions & 0 deletions bot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const builder = require('botbuilder');

const connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});

const bot = new builder.UniversalBot(connector, [
(session, args, next) => {

const card = new builder.ThumbnailCard(session);
card.buttons([
new builder.CardAction(session).title('Add a number').value('Add').type('imBack'),
new builder.CardAction(session).title('Get help').value('Help').type('imBack'),
]).text(`What would you like to do?`);

const message = new builder.Message(session);
message.addAttachment(card);

session.send(`Hi there! I'm the calculator bot! I can add numbers for you.`);
// we can end the conversation here
// the buttons will provide the appropriate message
session.endConversation(message);
},
]);

bot.dialog('AddNumber', [
(session, args, next) => {
let message = null;
if(!session.privateConversationData.runningTotal) {
message = `Give me the first number.`;
session.privateConversationData.runningTotal = 0;
} else {
message = `Give me the next number, or say **total** to display the total.`;
}
builder.Prompts.number(session, message, {maxRetries: 3});
},
(session, results, next) => {
if(results.response) {
session.privateConversationData.runningTotal += results.response;
session.replaceDialog('AddNumber');
} else {
session.endConversation(`Sorry, I don't understand. Let's start over.`);
}
},
])
.triggerAction({matches: /^add$/i})
.cancelAction('CancelAddNumber', 'Operation cancelled', {
matches: /^cancel$/,
onSelectAction: (session, args) => {
session.endConversation(`Operation cancelled.`);
},
confirmPrompt: `Are you sure you wish to cancel?`
})
.beginDialogAction('Total', 'Total', { matches: /^total$/})
.beginDialogAction('HelpAddNumber', 'Help', { matches: /^help$/, dialogArgs: {action: 'AddNumber'} });

bot.dialog('Total', [
(session, results, next) => {
session.endConversation(`The total is ${session.privateConversationData.runningTotal}`);
},
]);

bot.dialog('Help', [
(session, args, next) => {
let message = '';
switch(args.action) {
case 'AddNumber':
message = 'You can either type the next number, or use **total** to get the total.';
break;
default:
message = 'You can type **add** to add numbers.';
break;
}
session.endDialog(message);
}
]).triggerAction({
matches: /^help/i,
onSelectAction: (session, args) => {
session.beginDialog(args.action, args);
}
});

module.exports = bot;
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "dialog-sample",
"dependencies": {
"botbuilder": "^3.4.4",
"dotenv": "^2.0.0",
"restify": "^4.3.0"
},
"devDependencies": {
"@types/node": "^6.0.52",
"@types/restify": "^2.0.35"
}
}
77 changes: 77 additions & 0 deletions sample-beginDialogAction-help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const builder = require('botbuilder');

const connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});

const bot = new builder.UniversalBot(connector, [
(session, args, next) => {

const card = new builder.ThumbnailCard(session);
card.buttons([
new builder.CardAction(session).title('Add a number').value('Add').type('imBack'),
new builder.CardAction(session).title('Get help').value('Help').type('imBack'),
]).text(`What would you like to do?`);

const message = new builder.Message(session);
message.addAttachment(card);

session.send(`Hi there! I'm the calculator bot! I can add numbers for you.`);
// we can end the conversation here
// the buttons will provide the appropriate message
session.endConversation(message);
},
]);

bot.dialog('AddNumber', [
(session, args, next) => {
let message = null;
if(!session.privateConversationData.runningTotal) {
message = `Give me the first number.`;
session.privateConversationData.runningTotal = 0;
} else {
message = `Give me the next number, or say **total** to display the total.`;
}
builder.Prompts.number(session, message, {maxRetries: 3});
},
(session, results, next) => {
if(results.response) {
session.privateConversationData.runningTotal += results.response;
session.replaceDialog('AddNumber');
} else {
session.endConversation(`Sorry, I don't understand. Let's start over.`);
}
},
])
.triggerAction({matches: /^add$/i})
.beginDialogAction('Total', 'Total', { matches: /^total$/})
.beginDialogAction('HelpAddNumber', 'Help', { matches: /^help$/, dialogArgs: {action: 'AddNumber'} });

bot.dialog('Total', [
(session, results, next) => {
session.endConversation(`The total is ${session.privateConversationData.runningTotal}`);
},
]);

bot.dialog('Help', [
(session, args, next) => {
let message = '';
switch(args.action) {
case 'AddNumber':
message = 'You can either type the next number, or use **total** to get the total.';
break;
default:
message = 'You can type **add** to add numbers.';
break;
}
session.endDialog(message);
}
]).triggerAction({
matches: /^help/i,
onSelectAction: (session, args) => {
session.beginDialog(args.action, args);
}
});

module.exports = bot;
55 changes: 55 additions & 0 deletions sample-beginDialogAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const builder = require('botbuilder');

const connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});

const bot = new builder.UniversalBot(connector, [
(session, args, next) => {
const card = new builder.ThumbnailCard(session);
card.buttons([
new builder.CardAction(session).title('Add a number').value('Add').type('imBack'),
new builder.CardAction(session).title('Get help').value('Help').type('imBack'),
]).text(`What would you like to do?`);

const message = new builder.Message(session);
message.addAttachment(card);

session.send(`Hi there! I'm the calculator bot! I can add numbers for you.`);
// we can end the conversation here
// the buttons will provide the appropriate message
session.endConversation(message);
},
]);

bot.dialog('AddNumber', [
(session, args, next) => {
let message = null;
if(!session.privateConversationData.runningTotal) {
message = `Give me the first number.`;
session.privateConversationData.runningTotal = 0;
} else {
message = `Give me the next number, or say **total** to display the total.`;
}
builder.Prompts.number(session, message, {maxRetries: 3});
},
(session, results, next) => {
if(results.response) {
session.privateConversationData.runningTotal += results.response;
session.replaceDialog('AddNumber');
} else {
session.endConversation(`Sorry, I don't understand. Let's start over.`);
}
},
])
.triggerAction({matches: /^add$/i})
.beginDialogAction('Total', 'Total', { matches: /^total$/});

bot.dialog('Total', [
(session, results, next) => {
session.endConversation(`The total is ${session.privateConversationData.runningTotal}`);
},
]);

module.exports = bot;
84 changes: 84 additions & 0 deletions sample-cancelAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const builder = require('botbuilder');

const connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});

const bot = new builder.UniversalBot(connector, [
(session, args, next) => {

const card = new builder.ThumbnailCard(session);
card.buttons([
new builder.CardAction(session).title('Add a number').value('Add').type('imBack'),
new builder.CardAction(session).title('Get help').value('Help').type('imBack'),
]).text(`What would you like to do?`);

const message = new builder.Message(session);
message.addAttachment(card);

session.send(`Hi there! I'm the calculator bot! I can add numbers for you.`);
// we can end the conversation here
// the buttons will provide the appropriate message
session.endConversation(message);
},
]);

bot.dialog('AddNumber', [
(session, args, next) => {
let message = null;
if(!session.privateConversationData.runningTotal) {
message = `Give me the first number.`;
session.privateConversationData.runningTotal = 0;
} else {
message = `Give me the next number, or say **total** to display the total.`;
}
builder.Prompts.number(session, message, {maxRetries: 3});
},
(session, results, next) => {
if(results.response) {
session.privateConversationData.runningTotal += results.response;
session.replaceDialog('AddNumber');
} else {
session.endConversation(`Sorry, I don't understand. Let's start over.`);
}
},
])
.triggerAction({matches: /^add$/i})
.cancelAction('CancelAddNumber', 'Operation cancelled', {
matches: /^cancel$/,
onSelectAction: (session, args) => {
session.endConversation(`Operation cancelled.`);
},
confirmPrompt: `Are you sure you wish to cancel?`
})
.beginDialogAction('Total', 'Total', { matches: /^total$/})
.beginDialogAction('HelpAddNumber', 'Help', { matches: /^help$/, dialogArgs: {action: 'AddNumber'} });

bot.dialog('Total', [
(session, results, next) => {
session.endConversation(`The total is ${session.privateConversationData.runningTotal}`);
},
]);

bot.dialog('Help', [
(session, args, next) => {
let message = '';
switch(args.action) {
case 'AddNumber':
message = 'You can either type the next number, or use **total** to get the total.';
break;
default:
message = 'You can type **add** to add numbers.';
break;
}
session.endDialog(message);
}
]).triggerAction({
matches: /^help/i,
onSelectAction: (session, args) => {
session.beginDialog(args.action, args);
}
});

module.exports = bot;
26 changes: 26 additions & 0 deletions sample-defaultDialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const builder = require('botbuilder');

const connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});

const bot = new builder.UniversalBot(connector, [
(session, args, next) => {
const card = new builder.ThumbnailCard(session);
card.buttons([
new builder.CardAction(session).title('Add a number').value('Add').type('imBack'),
new builder.CardAction(session).title('Get help').value('Help').type('imBack'),
]).text(`What would you like to do?`);

const message = new builder.Message(session);
message.addAttachment(card);

session.send(`Hi there! I'm the calculator bot! I can add numbers for you.`);
const choices = ['Add', 'Help'];
builder.Prompts.choice(session, message, choices);
},
(session, results, next) => {
session.endConversation(`You chose ${results.response.entity}`);
},
]);
Loading

0 comments on commit 0aafc5c

Please sign in to comment.