-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christopher Harrison
committed
Feb 22, 2017
0 parents
commit 0aafc5c
Showing
12 changed files
with
511 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}, | ||
]); |
Oops, something went wrong.