forked from WebexSamples/webex-bot-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
494 lines (468 loc) · 14.2 KB
/
index.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//Webex Bot Starter - featuring the webex-node-bot-framework - https://www.npmjs.com/package/webex-node-bot-framework
require("dotenv").config();
var framework = require("webex-node-bot-framework");
var webhook = require("webex-node-bot-framework/webhook");
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.json());
app.use(express.static("images"));
const config = {
webhookUrl: process.env.WEBHOOKURL,
token: process.env.BOTTOKEN,
port: process.env.PORT,
};
// init framework
var framework = new framework(config);
framework.start();
console.log("Starting framework, please wait...");
framework.on("initialized", () => {
console.log("framework is all fired up! [Press CTRL-C to quit]");
});
// A spawn event is generated when the framework finds a space with your bot in it
// If actorId is set, it means that user has just added your bot to a new space
// If not, the framework has discovered your bot in an existing space
framework.on("spawn", (bot, id, actorId) => {
if (!actorId) {
// don't say anything here or your bot's spaces will get
// spammed every time your server is restarted
console.log(
`While starting up, the framework found our bot in a space called: ${bot.room.title}`,
);
} else {
// When actorId is present it means someone added your bot got added to a new space
// Lets find out more about them..
var msg =
"You can say `help` to get the list of words I am able to respond to.";
bot.webex.people
.get(actorId)
.then((user) => {
msg = `Hello there ${user.displayName}. ${msg}`;
})
.catch((e) => {
console.error(
`Failed to lookup user details in framwork.on("spawn"): ${e.message}`,
);
msg = `Hello there. ${msg}`;
})
.finally(() => {
// Say hello, and tell users what you do!
if (bot.isDirect) {
bot.say("markdown", msg);
} else {
let botName = bot.person.displayName;
msg += `\n\nDon't forget, in order for me to see your messages in this group space, be sure to *@mention* ${botName}.`;
bot.say("markdown", msg);
}
});
}
});
// Implementing a framework.on('log') handler allows you to capture
// events emitted from the framework. Its a handy way to better understand
// what the framework is doing when first getting started, and a great
// way to troubleshoot issues.
// You may wish to disable this for production apps
framework.on("log", (msg) => {
console.log(msg);
});
// Process incoming messages
// Each hears() call includes the phrase to match, and the function to call if webex mesages
// to the bot match that phrase.
// An optional 3rd parameter can be a help string used by the frameworks.showHelp message.
// An optional fourth (or 3rd param if no help message is supplied) is an integer that
// specifies priority. If multiple handlers match they will all be called unless the priority
// was specified, in which case, only the handler(s) with the lowest priority will be called
/* On mention with command
ex User enters @botname framework, the bot will write back in markdown
*/
framework.hears(
"framework",
(bot) => {
console.log("framework command received");
bot.say(
"markdown",
"The primary purpose for the [webex-node-bot-framework](https://github.com/jpjpjp/webex-node-bot-framework) was to create a framework based on the [webex-jssdk](https://webex.github.io/webex-js-sdk) which continues to be supported as new features and functionality are added to Webex. This version of the project was designed with two themes in mind: \n\n\n * Mimimize Webex API Calls. The original flint could be quite slow as it attempted to provide bot developers rich details about the space, membership, message and message author. This version eliminates some of that data in the interests of efficiency, (but provides convenience methods to enable bot developers to get this information if it is required)\n * Leverage native Webex data types. The original flint would copy details from the webex objects such as message and person into various flint objects. This version simply attaches the native Webex objects. This increases the framework's efficiency and makes it future proof as new attributes are added to the various webex DTOs ",
);
},
"**framework**: (learn more about the Webex Bot Framework)",
0,
);
/* On mention with command, using other trigger data, can use lite markdown formatting
ex User enters @botname 'info' phrase, the bot will provide personal details
*/
framework.hears(
"info",
(bot, trigger) => {
console.log("info command received");
//the "trigger" parameter gives you access to data about the user who entered the command
let personAvatar = trigger.person.avatar;
let personEmail = trigger.person.emails[0];
let personDisplayName = trigger.person.displayName;
let outputString = `Here is your personal information: \n\n\n **Name:** ${personDisplayName} \n\n\n **Email:** ${personEmail} \n\n\n **Avatar URL:** ${personAvatar}`;
bot.say("markdown", outputString);
},
"**info**: (get your personal details)",
0,
);
/* On mention with bot data
ex User enters @botname 'space' phrase, the bot will provide details about that particular space
*/
framework.hears(
"space",
(bot) => {
console.log("space. the final frontier");
let roomTitle = bot.room.title;
let spaceID = bot.room.id;
let roomType = bot.room.type;
let outputString = `The title of this space: ${roomTitle} \n\n The roomID of this space: ${spaceID} \n\n The type of this space: ${roomType}`;
console.log(outputString);
bot
.say("markdown", outputString)
.catch((e) => console.error(`bot.say failed: ${e.message}`));
},
"**space**: (get details about this space) ",
0,
);
/*
Say hi to every member in the space
This demonstrates how developers can access the webex
sdk to call any Webex API. API Doc: https://webex.github.io/webex-js-sdk/api/
*/
framework.hears(
"say hi to everyone",
(bot) => {
console.log("say hi to everyone. Its a party");
// Use the webex SDK to get the list of users in this space
bot.webex.memberships
.list({ roomId: bot.room.id })
.then((memberships) => {
for (const member of memberships.items) {
if (member.personId === bot.person.id) {
// Skip myself!
continue;
}
let displayName = member.personDisplayName
? member.personDisplayName
: member.personEmail;
bot.say(`Hello ${displayName}`);
}
})
.catch((e) => {
console.error(`Call to sdk.memberships.get() failed: ${e.messages}`);
bot.say("Hello everybody!");
});
},
"**say hi to everyone**: (everyone gets a greeting using a call to the Webex SDK)",
0,
);
// Buttons & Cards data
let cardJSON = {
$schema: "http://adaptivecards.io/schemas/adaptive-card.json",
type: "AdaptiveCard",
version: "1.0",
body: [
{
type: "ColumnSet",
columns: [
{
type: "Column",
width: "5",
items: [
{
type: "Image",
url: "Your avatar appears here!",
size: "large",
horizontalAlignment: "Center",
style: "person",
},
{
type: "TextBlock",
text: "Your name will be here!",
size: "medium",
horizontalAlignment: "Center",
weight: "Bolder",
},
{
type: "TextBlock",
text: "And your email goes here!",
size: "small",
horizontalAlignment: "Center",
isSubtle: true,
wrap: false,
},
],
},
],
},
],
};
/* On mention with card example
ex User enters @botname 'card me' phrase, the bot will produce a personalized card - https://developer.webex.com/docs/api/guides/cards
*/
framework.hears(
"card me",
(bot, trigger) => {
console.log("someone asked for a card");
let avatar = trigger.person.avatar;
cardJSON.body[0].columns[0].items[0].url = avatar
? avatar
: `${config.webhookUrl}/missing-avatar.jpg`;
cardJSON.body[0].columns[0].items[1].text = trigger.person.displayName;
cardJSON.body[0].columns[0].items[2].text = trigger.person.emails[0];
bot.sendCard(
cardJSON,
"This is customizable fallback text for clients that do not support buttons & cards",
);
},
"**card me**: (a cool card!)",
0,
);
/* On mention reply example
ex User enters @botname 'reply' phrase, the bot will post a threaded reply
*/
framework.hears(
"reply",
(bot, trigger) => {
console.log("someone asked for a reply. We will give them two.");
bot.reply(
trigger.message,
"This is threaded reply sent using the `bot.reply()` method.",
"markdown",
);
var msg_attach = {
text: "This is also threaded reply with an attachment sent via bot.reply(): ",
file: "https://media2.giphy.com/media/dTJd5ygpxkzWo/giphy-downsized-medium.gif",
};
bot.reply(trigger.message, msg_attach);
},
"**reply**: (have bot reply to your message)",
0,
);
/* On mention with command
ex User enters @botname help, the bot will write back in markdown
*
* The framework.showHelp method will use the help phrases supplied with the previous
* framework.hears() commands
*/
framework.hears(
/help|what can i (do|say)|what (can|do) you do/i,
(bot, trigger) => {
console.log(`someone needs help! They asked ${trigger.text}`);
bot
.say(`Hello ${trigger.person.displayName}.`)
// .then(() => sendHelp(bot))
.then(() => bot.say("markdown", framework.showHelp()))
.catch((e) => console.error(`Problem in help hander: ${e.message}`));
},
"**help**: (what you are reading now)",
0,
);
/* On mention with command
ex User enters @botname demo, the bot will write back in markdown
*
* The framework.showHelp method will use the help phrases supplied with the previous
* framework.hears() commands
*/
framework.hears(
/demo|show me a (demo|invite)/i,
(bot, trigger) => {
console.log(`someone needs a demo! They asked ${trigger.text}`);
let card = {
type: "AdaptiveCard",
body: [
{
type: "ColumnSet",
columns: [
{
type: "Column",
items: [
{
type: "Image",
style: "Person",
url: `${config.webhookUrl}/meeting.jpg`,
size: "Medium",
height: "50px",
},
],
width: "auto",
},
{
type: "Column",
items: [
{
type: "TextBlock",
text: "Meeting Recommendation",
weight: "Lighter",
color: "Accent",
},
{
type: "TextBlock",
weight: "Bolder",
text: "Connect to Leon",
horizontalAlignment: "Left",
wrap: true,
color: "Light",
size: "Large",
spacing: "Small",
},
],
width: "stretch",
},
],
},
{
type: "ColumnSet",
columns: [
{
type: "Column",
width: 35,
items: [
{
type: "TextBlock",
text: "Date suggestion:",
color: "Light",
},
{
type: "TextBlock",
text: "Meeting Partner",
weight: "Lighter",
color: "Light",
spacing: "Small",
},
{
type: "TextBlock",
text: "You",
weight: "Lighter",
color: "Light",
spacing: "Small",
},
],
},
{
type: "Column",
width: 65,
items: [
{
type: "TextBlock",
text: "Aug 6, 2019",
color: "Light",
},
{
type: "TextBlock",
text: "Leon Bernard | Design",
color: "Light",
weight: "Lighter",
spacing: "Small",
},
{
type: "TextBlock",
text: "Bennedikt Völker | Programming",
weight: "Lighter",
color: "Light",
spacing: "Small",
},
],
},
],
spacing: "Padding",
horizontalAlignment: "Center",
},
{
type: "TextBlock",
text: "We're making it easier for you to connect with new people and exchange interesting ideas to further develop projects and products.",
wrap: true,
},
{
type: "TextBlock",
text: "Take action:",
},
{
type: "ColumnSet",
columns: [
{
type: "Column",
width: "auto",
items: [
{
type: "Image",
altText: "",
url: `${config.webhookUrl}/chat3.png`,
size: "Small",
width: "35px",
},
],
spacing: "Small",
},
{
type: "Column",
width: "auto",
items: [
{
type: "TextBlock",
text: "[Chat With Your Meeting Partner]()",
horizontalAlignment: "Left",
size: "Medium",
color: "Accent",
},
],
verticalContentAlignment: "Center",
horizontalAlignment: "Left",
spacing: "Small",
},
],
},
],
$schema: "http://adaptivecards.io/schemas/adaptive-card.json",
version: "1.3",
};
bot.sendCard(
card,
"This is customizable fallback text for clients that do not support buttons & cards",
);
// bot
// .say(`Hello ${trigger.person.displayName}.`)
// .then(() => bot.say("markdown", framework.showHelp()))
// .catch((e) => console.error(`Problem in help hander: ${e.message}`));
},
"**help**: (what you are reading now)",
0,
);
/* On mention with unexpected bot command
Its a good practice is to gracefully handle unexpected input
Setting the priority to a higher number here ensures that other
handlers with lower priority will be called instead if there is another match
*/
framework.hears(
/.*/,
(bot, trigger) => {
// This will fire for any input so only respond if we haven't already
console.log(`catch-all handler fired for user input: ${trigger.text}`);
bot
.say(`Sorry, I don't know how to respond to "${trigger.text}"`)
.then(() => bot.say("markdown", framework.showHelp()))
// .then(() => sendHelp(bot))
.catch((e) =>
console.error(
`Problem in the unexepected command hander: ${e.message}`,
),
);
},
99999,
);
//Server config & housekeeping
// Health Check
app.get("/", (req, res) => {
res.send(`I'm alive.`);
});
app.post("/", webhook(framework));
var server = app.listen(config.port, () => {
framework.debug("framework listening on port %s", config.port);
});
// gracefully shutdown (ctrl-c)
process.on("SIGINT", () => {
framework.debug("stopping...");
server.close();
framework.stop().then(() => {
process.exit();
});
});