/
websocket_features.js
112 lines (83 loc) · 3.21 KB
/
websocket_features.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
101
102
103
104
105
106
107
108
109
110
111
112
const { BotkitConversation } = require('botkit');
module.exports = function(controller) {
if (controller.adapter.name === 'Web Adapter') {
console.log('Loading sample Websocket features...');
controller.publicFolder('/',__dirname + '/../public');
/**
* Send a message to new users / people with no user id cookie set
*/
controller.on('hello', async(bot, message) => {
await bot.reply(message,'Nice to meet you, human!');
});
/**
* Send a message to returning users
*/
controller.on('welcome_back', async(bot, message) => {
await bot.reply(message,'Welcome back, human.');
});
/**
* demo the use of the quick reply feature
*/
controller.hears(new RegExp('quick'), 'message', async (bot, message) => {
await bot.reply(message,{
text: 'Here are some quick replies',
quick_replies: [
{
title: 'Foo',
payload: 'foo',
},
{
title: 'Bar',
payload: 'bar',
}
]
});
});
let typing = new BotkitConversation('typing', controller);
typing.say('I am going to type for a while now...');
typing.addAction('typing');
// start the typing indicator
typing.addMessage({type: 'typing'}, 'typing');
// trigger a gotoThread, which gives us an opportunity to delay the next message
typing.addAction('next_thread','typing');
typing.addMessage('typed!','next_thread');
// use the before handler to delay the next message
typing.before('next_thread', async() => {
return new Promise((resolve, reject) => {
// simulate some long running process
setTimeout(resolve, 3000);
});
});
controller.addDialog(typing);
controller.hears('typing dialog', 'message', async(bot, message) => {
await bot.beginDialog('typing');
});
controller.hears('typing reply', 'message', async(bot, message) => {
await bot.reply(message, {type: 'typing'});
setTimeout(async () => {
// will have to reset context because turn has now ended.
await bot.changeContext(message.reference);
await bot.reply(message, 'Typed!');
}, 1000);
});
let replies = new BotkitConversation('replies', controller);
replies.ask({
text: 'Click one of these suggestions!',
quick_replies: [
{
title: 'Foo',
payload: 'foo',
},
{
title: 'Bar',
payload: 'bar',
}
]
}, [], 'reply');
replies.say('You clicked {{vars.reply}}');
controller.addDialog(replies);
controller.hears('qqq', 'message', async(bot, message) => {
await bot.beginDialog('replies');
});
}
}