Skip to content

Instantly share code, notes, and snippets.

@hailiang-wang
Created July 13, 2016 07:59
Show Gist options
  • Save hailiang-wang/231465755512f8cbdc3e4ac16b6c6b2f to your computer and use it in GitHub Desktop.
Save hailiang-wang/231465755512f8cbdc3e4ac16b6c6b2f to your computer and use it in GitHub Desktop.
var restify = require('restify');
var builder = require('botbuilder');
var config = require('./config');
//=========================================================
// Bot Setup
//=========================================================
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat bot
var connector = new builder.ChatConnector({
appId: config.ms_bot.appid,
appPassword: config.ms_bot.appsecret
});
var bot = new builder.UniversalBot(connector);
server.post('/api/ms-messaging', connector.listen());
//=========================================================
// Bots Dialogs
//=========================================================
bot.dialog('/', function(session) {
session.send("Hi %s, what can I help you with?", session.userData.name);
});
// Install First Run middleware and dialog
bot.use({
botbuilder: function(session, next) {
if (!session.userData.firstRun) {
session.userData.firstRun = true;
session.beginDialog('/firstRun');
} else {
next();
}
}
});
bot.dialog('/firstRun', [
function(session) {
builder.Prompts.text(session, "Hello... What's your name?");
},
function(session, results) {
// We'll save the prompts result and return control to main through
// a call to replaceDialog(). We need to use replaceDialog() because
// we intercepted the original call to main and we want to remove the
// /firstRun dialog from the callstack. If we called endDialog() here
// the conversation would end since the /firstRun dialog is the only
// dialog on the stack.
session.userData.name = results.response;
session.replaceDialog('/');
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment