-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (71 loc) · 2.49 KB
/
Copy pathapp.js
File metadata and controls
83 lines (71 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//test bot/////////////////////////////////////////////////
var restify = require('restify');
var builder = require('botbuilder');
//=========================================================
// 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: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
//Root dialog-
bot.dialog('/', [
function (session) {
//Get user info
session.beginDialog('/ensureProfile', session.userData.profile);
},
function (session, results) {
//We've gotten the user's information and can now give a response based on that data
session.userData.profile = results.response;
session.send('Hello %(name)s! I love %(company)s!', session.userData.profile);
}
]);
bot.dialog('/ensureProfile', [
function (session, args, next) {
session.dialogData.profile = args || {};
//Checks whether or not we already have the user's name
if (!session.dialogData.profile.name) {
builder.Prompts.text(session, "What's your name?");
} else {
next();
}
},
function (session, results, next) {
if (results.response) {
session.dialogData.profile.name = results.response;
}
//Checks whether or not we already have the user's company
if (!session.dialogData.profile.company) {
builder.Prompts.text(session, "What company do you work for?");
} else {
next();
}
},
function (session, results) {
if (results.response) {
session.dialogData.profile.company = results.response;
}
//We now have the user's info (name, company), so we end this dialog
session.endDialogWithResult({ response: session.dialogData.profile });
}
]);
bot.dialog('help',[
(session) => {
session.endDialog(`I'm a simple bot...`);
}
]).triggerAction({
matches: /^help$/i,
onSelectAction: (session,args) => {
session.beginDialog(args.action, args);
}
})
// Listen for messages from users
server.post('/api/messages', connector.listen());