forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
100 lines (89 loc) · 3.72 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*-----------------------------------------------------------------------------
This demo shows how to create a progress dialog that will periodically notify
users of the status of a long running task.
-----------------------------------------------------------------------------*/
var restify = require('restify');
var builder = require('botbuilder');
// 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 connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
// Create your bot with a function to receive messages from the user.
var bot = new builder.UniversalBot(connector, [
function (session) {
// We can wrap a potentially long running operation with a progress dialog. This
// will periodically notify the user that the bot is still working on their
// request.
var options = {
initialText: 'Please wait... This may take a few moments.',
text: 'Please wait... This is taking a little longer then expected.',
speak: '<speak>Please wait.<break time="2s"/></speak>'
};
progress(session, options, function (callback) {
// Make our async call here. If the call completes quickly then no progress
// message will be sent.
setTimeout(function () {
callback('You said: ' + session.message.text);
}, 20000);
});
},
function (session, results) {
session.send(results.response);
}
]);
/**
* Wrapper function to simplify calling our progress dialog.
* @param {*} session Current session object.
* @param {*} options Options to control messages sent by the progress dialog.
* Values:
* * text: (Required) progress text to send.
* * speak: (Optional) ssml to send with each progress message.
* * delay: (Optional) delay (in ms) before progress is sent.
* * initialText: (Optional) initial progress text.
* * initialSpeak: (Optional) initial progress ssml.
* * initialDelay: (Optional) delay before initial progress is sent.
* @param {*} asyncFn Async function to call. Will be passed a callback with a
* signature of (response: any) => void.
*/
function progress(session, options, asyncFn) {
session.beginDialog('progressDialog', {
asyncFn: asyncFn,
options: options
})
}
bot.dialog('progressDialog', function (session, args) {
if(!args) return;
var asyncFn = args.asyncFn;
var options = args.options;
var count = 0;
function sendProgress() {
if (count++ > 0) {
session.say(options.text, options.speak, { inputHint: builder.InputHint.ignoringInput });
} else {
var text = options.initialText || options.text;
var speak = options.initialSpeak || options.speak;
session.say(text, speak, { inputHint: builder.InputHint.ignoringInput });
}
hTimer = setTimeout(sendProgress, options.delay || 9000);
}
// Start progress timer
var hTimer = setTimeout(sendProgress, options.initialDelay || 1000);
// Call async function
try {
asyncFn(function (response) {
// Stop timer and return response
clearTimeout(hTimer);
session.endDialogWithResult({ response: response });
});
} catch (err) {
session.error(err);
}
});