-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathbot.js
138 lines (116 loc) · 4.37 KB
/
bot.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
//
// Copyright (c) 2016 Cisco Systems
// Licensed under the MIT License
//
/*
* a bot to list current and upcoming DevNet events
*
* reads events from a REST API which reflects https://developer.cisco.com/site/devnet/events-contests/events/
*
*/
var debug = require("debug")("samples");
var fine = require("debug")("samples:fine");
// Starts a bot with default configuration, access token read from the ACCESS_TOKEN env variable
var SparkBot = require("node-sparkbot");
var bot = new SparkBot();
// removing the bot default triggering filter
bot.interpreter.prefix = ""; // not more "/" prepend to commands
// nodejs client to write back to Cisco Spark
var SparkClient = require("node-sparky");
var sparky = new SparkClient({ token: process.env.ACCESS_TOKEN });
// event API wrapper that preformats markdown messages to send back to Webex Teams
var Events = require("./events.js");
bot.onCommand("help", function (command) {
showHelp(command.message.roomId);
});
bot.onCommand("fallback", function (command) {
// so happy to join
sparky.messageSend({
roomId: command.message.roomId,
markdown: "**sorry, I did not understand.**"
})
.then(function (message) {
// show how to use
showHelp(command.message.roomId);
});
});
function showHelp(roomId) {
sparky.messageSend({
roomId: roomId,
markdown: "I can tell about upcoming events at DevNet. Try:\n- about\n- help\n- next [#max]: upcoming events, defaults to 5\n- now: events happening now\n"
});
}
bot.onCommand("about", function (command) {
sparky.messageSend({
roomId: command.message.roomId,
markdown: "```\n{\n 'author':'Stève Sfartz <stsfartz@cisco.com>',\n 'code':'https://github.com/CiscoDevNet/node-sparkbot-samples/blob/master/examples/devnet/bot.js',\n 'description':'inquire about upcoming events at DevNet',\n 'healthcheck':'GET https://devnet-events-sparkbot.herokuapp.com'\n}\n```"
});
});
bot.onCommand("next", function (command) {
// let's acknowledge we received the order
sparky.messageSend({
roomId: command.message.roomId,
markdown: "_heard you! asking my crystal ball..._"
});
var limit = parseInt(command.args[0]);
if (!limit) limit = 5;
if (limit < 1) limit = 1;
Events.fetchNext(limit, function (err, events) {
if (err) {
sparky.messageSend( {
roomId: command.message.roomId,
markdown: "**sorry, ball seems broken :-(**"
});
return;
}
sparky.messageSend({
roomId: command.message.roomId,
markdown: events
});
});
});
bot.onCommand("now", function (command) {
// let's acknowledge we received the order
sparky.messageSend({
roomId: command.message.roomId,
markdown: "_heard you! let's check what's happening now..._"
});
Events.fetchCurrent(function (err, events) {
if (err) {
sparky.messageSend({
roomId: command.message.roomId,
markdown: "**sorry, could not contact the organizers :-(**"
});
return;
}
sparky.messageSend({
roomId: command.message.roomId,
markdown: events
});
});
});
bot.onEvent("memberships", "created", function (trigger) {
var newMembership = trigger.data; // see specs here: https://developer.webex.com/endpoint-memberships-get.html
if (newMembership.personId == bot.interpreter.person.id) {
debug("bot's just added to room: " + trigger.data.roomId);
// so happy to join
sparky.messageSend({
roomId: trigger.data.roomId,
text: "Hi, I am the DevNet Bot !"
})
.then(function (message) {
if (message.roomType == "group") {
sparky.messageSend({
roomId: message.roomId,
markdown: "**Note: this is a 'Group' room, I will wake up only when mentionned, ex: @" + bot.interpreter.nickName + " " + bot.interpreter.prefix + "help**"
})
.then(function (message) {
showHelp(message.roomId);
});
}
else {
showHelp(message.roomId);
}
});
}
});