forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (61 loc) · 2.6 KB
/
app.js
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
// This loads the environment variables from the .env file
require('dotenv-extended').load();
var builder = require('botbuilder');
var restify = require('restify');
// 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 and listen for messages
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
server.post('/api/messages', connector.listen());
var userStore = [];
var bot = new builder.UniversalBot(connector, function (session) {
// store user's address
var address = session.message.address;
userStore.push(address);
// end current dialog
session.endDialog('You\'ve been invited to a survey! It will start in a few seconds...');
});
// Every 5 seconds, check for new registered users and start a new dialog
setInterval(function () {
var newAddresses = userStore.splice(0);
newAddresses.forEach(function (address) {
console.log('Starting survey for address:', address);
// new conversation address, copy without conversationId
var newConversationAddress = Object.assign({}, address);
delete newConversationAddress.conversation;
// start survey dialog
bot.beginDialog(newConversationAddress, 'survey', null, function (err) {
if (err) {
// error ocurred while starting new conversation. Channel not supported?
bot.send(new builder.Message()
.text('This channel does not support this operation: ' + err.message)
.address(address));
}
});
});
}, 5000);
bot.dialog('survey', [
function (session) {
builder.Prompts.text(session, 'Hello... What\'s your name?');
},
function (session, results) {
session.userData.name = results.response;
builder.Prompts.number(session, 'Hi ' + results.response + ', How many years have you been coding?');
},
function (session, results) {
session.userData.coding = results.response;
builder.Prompts.choice(session, 'What language do you code Node using? ', ['JavaScript', 'CoffeeScript', 'TypeScript']);
},
function (session, results) {
session.userData.language = results.response.entity;
session.endDialog('Got it... ' + session.userData.name +
' you\'ve been programming for ' + session.userData.coding +
' years and use ' + session.userData.language + '.');
}
]);