-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
106 lines (87 loc) · 3.21 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
//
// Copyright (c) 2017 Cisco Systems
// Licensed under the MIT License
//
// Load env variables
var env = require('node-env-file');
env(__dirname + '/.env');
//
// BotKit initialization
//
var Botkit = require('botkit');
// Fetch token from environement
// [COMPAT] supports SPARK_TOKEN for backward compatibility
var accessToken = process.env.ACCESS_TOKEN || process.env.SPARK_TOKEN
if (!accessToken) {
console.log("Could not start as this bot requires a Webex Teams API access token.");
console.log("Please invoke with an ACCESS_TOKEN environment variable");
console.log("Example: ");
console.log("> ACCESS_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");
console.log("Example: ");
console.log("> ACCESS_TOKEN=XXXXXXXXXXXX PUBLIC_URL=YYYYYYYYYYYYY node bot.js");
process.exit(1);
}
var env = process.env.NODE_ENV || "development";
var controller = Botkit.webexbot({
log: true,
public_address: process.env.PUBLIC_URL,
access_token: accessToken,
secret: process.env.SECRET, // this is a RECOMMENDED security setting that checks if incoming payloads originate from Webex
webhook_name: process.env.WEBHOOK_NAME || ('built with BotKit (' + env + ')')
});
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;
// Start Bot API
controller.setupWebserver(process.env.PORT || 3000, function (err, webserver) {
controller.createWebhookEndpoints(webserver, bot, function () {
console.log("webhooks setup is complete");
});
// installing Healthcheck
webserver.get('/ping', function (req, res) {
res.json(bot.commons);
});
console.log("healthcheck available at: " + bot.commons.healthcheck);
});
// Load skills
var normalizedPath = require("path").join(__dirname, "skills");
require("fs").readdirSync(normalizedPath).forEach(function (file) {
try {
require("./skills/" + file)(controller);
console.log("loaded skill: " + file);
}
catch (err) {
if (err.code == "MODULE_NOT_FOUND") {
if (file != "utils") {
console.log("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 + "`";
}