|
3 | 3 | // Licensed under the MIT License |
4 | 4 | // |
5 | 5 |
|
| 6 | + |
| 7 | +var debug = require('debug')('starterkit'); |
| 8 | + |
| 9 | + |
6 | 10 | // |
7 | 11 | // BotKit configuration |
8 | 12 | // |
9 | 13 |
|
10 | 14 | // Load environment variables from project .env file |
11 | 15 | require('node-env-file')(__dirname + '/.env'); |
12 | 16 |
|
| 17 | +// Fail fast |
13 | 18 | if (!process.env.SPARK_TOKEN) { |
14 | 19 | console.log("Could not start as bots require a Cisco Spark API access token."); |
15 | 20 | console.log("Please add env variable SPARK_TOKEN on the command line or to the .env file"); |
@@ -49,90 +54,132 @@ if (!public_url) { |
49 | 54 | var Botkit = require('botkit'); |
50 | 55 |
|
51 | 56 | var env = process.env.NODE_ENV || "development"; |
52 | | -var controller = Botkit.sparkbot({ |
53 | | - log: true, |
54 | | - public_address: public_url, |
| 57 | + |
| 58 | +var configuration = { |
| 59 | + public_address: process.env.PUBLIC_URL, |
55 | 60 | ciscospark_access_token: process.env.SPARK_TOKEN, |
56 | | - secret: process.env.SECRET, // this is a RECOMMENDED security setting that checks of incoming payloads originate from Cisco Spark |
| 61 | + secret: process.env.SECRET, // this is a RECOMMENDED security setting that checks if incoming payloads originate from Cisco Spark |
57 | 62 | webhook_name: process.env.WEBHOOK_NAME || ('built with BotKit (' + env + ')') |
58 | | -}); |
59 | | - |
60 | | -var bot = controller.spawn({ |
61 | | -}); |
62 | | - |
63 | | - |
64 | | -// |
65 | | -// Launch bot |
66 | | -// |
| 63 | +} |
67 | 64 |
|
68 | | -var port = process.env.PORT || 3000; |
69 | | -controller.setupWebserver(port, function (err, webserver) { |
70 | | - controller.createWebhookEndpoints(webserver, bot, function () { |
71 | | - console.log("Cisco Spark: Webhooks set up!"); |
| 65 | +// Load extra configuration modules |
| 66 | +try { |
| 67 | + var configurationPath = require("path").join(__dirname, "configurations"); |
| 68 | + require("fs").readdirSync(configurationPath).forEach(function (file) { |
| 69 | + try { |
| 70 | + if (file.endsWith(".js")) { |
| 71 | + require("./configurations/" + file)(configuration); |
| 72 | + console.log("loaded configuration:" + file); |
| 73 | + } |
| 74 | + } |
| 75 | + catch (err) { |
| 76 | + console.log("error, configuration not loaded: " + file); |
| 77 | + } |
72 | 78 | }); |
| 79 | +} |
| 80 | +catch (err) { |
| 81 | + if (err.code == "ENOENT") { |
| 82 | + debug("configurations directory not present, continuing...."); |
| 83 | + } |
| 84 | + else { |
| 85 | + // fail fast |
| 86 | + throw err; |
| 87 | + } |
| 88 | +} |
73 | 89 |
|
74 | | - // installing Healthcheck |
75 | | - var healthcheck = { |
76 | | - "up-since": new Date(Date.now()).toGMTString(), |
77 | | - "hostname": require('os').hostname() + ":" + port, |
78 | | - "version": "v" + require("./package.json").version, |
79 | | - "bot": "unknown", // loaded asynchronously |
80 | | - "botkit": "v" + bot.botkit.version() |
81 | | - }; |
82 | | - webserver.get(process.env.HEALTHCHECK_ROUTE, function (req, res) { |
83 | | - |
84 | | - // As the identity is load asynchronously from Cisco Spark token, we need to check until it's fetched |
85 | | - if (healthcheck.bot == "unknown") { |
86 | | - var identity = bot.botkit.identity; |
87 | | - if (bot.botkit.identity) { |
88 | | - healthcheck.bot = bot.botkit.identity.emails[0]; |
| 90 | +var controller = require('botkit').sparkbot(configuration); |
| 91 | + |
| 92 | +var sparkbot = controller.spawn({}, function (bot) { |
| 93 | + |
| 94 | + // Load bot extensions: append_mention, botcommons metadata |
| 95 | + try { |
| 96 | + var extensionsPath = require("path").join(__dirname, "extensions"); |
| 97 | + require("fs").readdirSync(extensionsPath).forEach(function (file) { |
| 98 | + try { |
| 99 | + if (file.endsWith(".js")) { |
| 100 | + require("./extensions/" + file)(bot); |
| 101 | + debug("extension loaded: " + file); |
| 102 | + } |
| 103 | + } |
| 104 | + catch (err) { |
| 105 | + debug("error, could not load extension: " + file); |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + catch (err) { |
| 110 | + if (err.code == "ENOENT") { |
| 111 | + debug("extensions directory not present, continuing...."); |
| 112 | + } |
| 113 | + else { |
| 114 | + // fail fast |
| 115 | + throw err; |
89 | 116 | } |
90 | 117 | } |
91 | | - |
92 | | - res.json(healthcheck); |
93 | 118 | }); |
94 | | - console.log("Cisco Spark: healthcheck available at: " + process.env.HEALTHCHECK_ROUTE); |
95 | | -}); |
96 | 119 |
|
97 | 120 |
|
98 | 121 | // |
99 | | -// Load skills |
| 122 | +// Launch bot |
100 | 123 | // |
101 | 124 |
|
102 | | -var normalizedPath = require("path").join(__dirname, "skills"); |
103 | | -require("fs").readdirSync(normalizedPath).forEach(function (file) { |
| 125 | +// Start Bot API |
| 126 | +controller.setupWebserver(process.env.PORT || 3000, function (err, webserver) { |
| 127 | + if (err) { |
| 128 | + console.log("could not start Web server, existing... err: " + err.message); |
| 129 | + throw err; |
| 130 | + } |
| 131 | + |
| 132 | + controller.createWebhookEndpoints(webserver, sparkbot, function (err, success) { |
| 133 | + debug("Webhook successfully setup"); |
| 134 | + }); |
| 135 | + |
| 136 | + // Load extra plugins: middlewares, healthchecks... |
104 | 137 | try { |
105 | | - require("./skills/" + file)(controller, bot); |
106 | | - console.log("Cisco Spark: loaded skill: " + file); |
| 138 | + var pluginsPath = require("path").join(__dirname, "plugins"); |
| 139 | + require("fs").readdirSync(pluginsPath).forEach(function (file) { |
| 140 | + try { |
| 141 | + if (file.endsWith(".js")) { |
| 142 | + require("./plugins/" + file)(controller, sparkbot); |
| 143 | + debug("plugin loaded: " + file); |
| 144 | + } |
| 145 | + } |
| 146 | + catch (err) { |
| 147 | + debug("error, could not load plugin: " + file); |
| 148 | + } |
| 149 | + }); |
107 | 150 | } |
108 | 151 | catch (err) { |
109 | | - if (err.code == "MODULE_NOT_FOUND") { |
110 | | - if (file != "utils") { |
111 | | - console.log("Cisco Spark: could not load skill: " + file); |
112 | | - } |
| 152 | + if (err.code == "ENOENT") { |
| 153 | + debug("plugins directory not present, continuing...."); |
| 154 | + } |
| 155 | + else { |
| 156 | + // fail fast |
| 157 | + throw err; |
113 | 158 | } |
114 | 159 | } |
115 | | -}); |
116 | | - |
117 | 160 |
|
118 | | -// |
119 | | -// Cisco Spark Utilities |
120 | | -// |
121 | | - |
122 | | -// Utility to add mentions if Bot is in a 'Group' space |
123 | | -bot.appendMention = function (message, command) { |
124 | | - |
125 | | - // if the message is a raw message (from a post message callback such as bot.say()) |
126 | | - if (message.roomType && (message.roomType == "group")) { |
127 | | - var botName = bot.botkit.identity.displayName; |
128 | | - return "`@" + botName + " " + command + "`"; |
| 161 | + // Load skills |
| 162 | + try { |
| 163 | + var skillsPath = require("path").join(__dirname, "skills"); |
| 164 | + require("fs").readdirSync(skillsPath).forEach(function (file) { |
| 165 | + try { |
| 166 | + if (file.endsWith(".js")) { |
| 167 | + require("./skills/" + file)(controller); |
| 168 | + debug("skill loaded: " + file); |
| 169 | + } |
| 170 | + } |
| 171 | + catch (err) { |
| 172 | + debug("error, could not load skill: " + file); |
| 173 | + } |
| 174 | + }); |
129 | 175 | } |
130 | | - |
131 | | - // if the message is a Botkit message |
132 | | - if (message.raw_message && (message.raw_message.data.roomType == "group")) { |
133 | | - var botName = bot.botkit.identity.displayName; |
134 | | - return "`@" + botName + " " + command + "`"; |
| 176 | + catch (err) { |
| 177 | + if (err.code == "ENOENT") { |
| 178 | + debug("skills directory not present, aborting...."); |
| 179 | + } |
| 180 | + |
| 181 | + // fail fast |
| 182 | + throw err; |
135 | 183 | } |
| 184 | +}); |
136 | 185 |
|
137 | | - return "`" + command + "`"; |
138 | | -} |
|
0 commit comments