-
Notifications
You must be signed in to change notification settings - Fork 6
/
bot.js
211 lines (171 loc) · 6.63 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
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
// Copyright (c) 2017 Altus Consulting
// Licensed under the MIT License
// Portions of this code are licensed and copyright as follows:
// Copyright (c) 2017 Cisco Systems
// Licensed under the MIT License
// Load env variables
var env = require('node-env-file');
env(__dirname + '/.env');
var Broadcast = require('./lib/broadcast.js');
// Storage
var redisConfig = {
"hash_methods": ['subscriptions', 'topics', 'counter', 'admins'],
"sorted_set_methods": ['messages'],
"url": process.env.REDIS_URL,
"namespace": "broadcast"
};
var storage = require('./lib/storage.js')(redisConfig);
const express = require('express');
const path = require('path');
//
// BotKit initialization
//
var Botkit = require('botkit');
if (!process.env.SPARK_TOKEN) {
console.log("Could not start as bots require a Cisco Spark API access token.");
console.log("Please add env variable SPARK_TOKEN on the command line or to the .env file");
console.log("Example: ");
console.log("> SPARK_TOKEN=XXXXXXXXXXXX PUBLIC_URL=YYYYYYYYYYYYY node bot.js");
process.exit(1);
}
if (!process.env.PUBLIC_URL) {
console.log("Could not start as this bot must expose a public endpoint.");
console.log("Please add env variable PUBLIC_URL on the command line or to the .env file");
console.log("Example: ");
console.log("> SPARK_TOKEN=XXXXXXXXXXXX PUBLIC_URL=YYYYYYYYYYYYY node bot.js");
process.exit(1);
}
var env = process.env.NODE_ENV || "development";
var controller = Botkit.sparkbot({
log: true,
public_address: process.env.PUBLIC_URL,
ciscospark_access_token: process.env.SPARK_TOKEN,
secret: process.env.SECRET, // this is a RECOMMENDED security setting that checks of incoming payloads originate from Cisco Spark
webhook_name: process.env.WEBHOOK_NAME || ('built with BotKit (' + env + ')'),
storage: storage,
limit_to_domain: process.env.ALLOWED_DOMAINS.split(",")
});
var bot = controller.spawn({});
// Load BotCommons properties
bot.commons = {};
bot.commons["healthcheck"] = process.env.PUBLIC_URL + "/ping";
bot.commons["up-since"] = new Date(Date.now()).toGMTString();
bot.commons["version"] = "v" + require("./package.json").version;
bot.commons["owner"] = process.env.owner;
bot.commons["support"] = process.env.support;
bot.commons["platform"] = process.env.platform;
bot.commons["nickname"] = process.env.BOT_NICKNAME || "unknown";
bot.commons["code"] = process.env.code;
// Function to emulate Python's .format syntax. In the future will
// be moved to a separate file
String.prototype.format = function() {
var i = 0,
args = arguments;
return this.replace(/{}/g, function() {
return typeof args[i] != 'undefined' ? args[i++] : '';
});
};
var api_base_url = "/api/v1";
var notificationController = Broadcast.notifications({
storage: controller.storage,
token_secret: process.env.SECRET,
api_base_url: api_base_url
});
var topicController = Broadcast.topics({
storage: controller.storage,
token_secret: process.env.SECRET,
api_base_url: api_base_url
});
var messageController = Broadcast.messages({
storage: controller.storage,
token_secret: process.env.SECRET,
api_base_url: api_base_url
});
var authController = Broadcast.auth({
storage: controller.storage,
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
redirect_uri: process.env.REDIRECT_URI,
allowed_admin: process.env.ALLOWED_ADMIN,
token_secret: process.env.SECRET,
api_base_url: api_base_url
});
var adminController = Broadcast.admins({
storage: controller.storage,
token_secret: process.env.SECRET,
api_base_url: api_base_url
});
var subscriptionsController = Broadcast.subscriptions({
storage: controller.storage,
token_secret: process.env.SECRET,
api_base_url: api_base_url
})
// Start Bot API
controller.setupWebserver(process.env.PORT || 3000, function(err, webserver) {
controller.createWebhookEndpoints(webserver, bot, function() {
console.log("Cisco Spark: Webhooks set up!");
});
webserver.use(express.static(path.join(__dirname, 'dist')));
webserver.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, x-access-token");
next();
});
webserver.get('/avatar.png', function(req, res) {
res.sendFile(path.join(__dirname, 'lib/images/avatar.png'));
});
notificationController.createNotificationEndpoints(webserver, bot, function() {
console.log("Broadcast Bot: Notification endpoints set up!");
});
topicController.createTopicEndpoints(webserver, bot, function() {
console.log("Broadcast Bot: Topic endpoints set up!");
});
messageController.createMessageEndpoints(webserver, bot, function() {
console.log("Broadcast Bot: Message endpoints set up!");
});
authController.createAuthEndpoints(webserver, bot, function() {
console.log("Broadcast Bot: Auth endpoints set up!");
});
adminController.createAdminEndpoints(webserver, bot, function() {
console.log("Broadcast Bot: Admin endpoints set up!");
});
subscriptionsController.createSubscriptionsEndpoints(webserver, bot, function() {
console.log("Broadcast Bot: Subscriptions endpoints set up!")
});
webserver.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
// installing Healthcheck
webserver.get('/ping', function(req, res) {
res.json(bot.commons);
});
console.log("Cisco Spark: healthcheck available at: " + bot.commons.healthcheck);
});
// Load skills
var normalizedPath = require("path").join(__dirname, "lib/skills");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
try {
require("./lib/skills/" + file)(controller);
console.log("Cisco Spark: loaded skill: " + file);
} catch (err) {
if (err.code == "MODULE_NOT_FOUND") {
if (file != "utils") {
console.log("Cisco Spark: could not load skill: " + file);
}
}
}
});
// Utility to add mentions if Bot is in a 'Group' space
bot.enrichCommand = function(message, command) {
var botName = process.env.BOT_NICKNAME || "BotName";
if ("group" == message.roomType) {
return "`@" + botName + " " + command + "`";
}
if (message.original_message) {
if ("group" == message.original_message.roomType) {
return "`@" + botName + " " + command + "`";
}
}
return "`" + command + "`";
}