All files / lib Web.js

1.86% Statements 3/161
0% Branches 0/78
0% Functions 0/35
1.89% Lines 3/159
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 3841x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         1x  
var Botkit = require(__dirname + '/CoreBot.js');
var WebSocket = require('ws');
 
function WebBot(configuration) {
 
    var controller = Botkit(configuration || {});
 
    if (controller.config.typingDelayFactor === undefined) {
        controller.config.typingDelayFactor = 1;
    }
 
    controller.excludeFromConversations(['hello', 'welcome_back', 'reconnect']);
 
    controller.openSocketServer = function(server) {
 
        // create the socket server along side the existing webserver.
        var wss = new WebSocket.Server({
            server
        });
 
        // Expose the web socket server object to the controller so it can be used later.
        controller.wss = wss;
 
        function heartbeat() {
            this.isAlive = true;
        }
 
        wss.on('connection', function connection(ws) {
            ws.isAlive = true;
            ws.on('pong', heartbeat);
            // search through all the convos, if a bot matches, update its ws
            var bot = controller.spawn();
            bot.ws = ws;
            bot.connected = true;
 
            ws.on('message', function incoming(message) {
                try {
                    var message = JSON.parse(message);
                    controller.ingest(bot, message, ws);
                } catch (e) {
                    var alert = [
                        `Error parsing incoming message from websocket.`,
                        `Message must be JSON, and should be in the format documented here:`,
                        `https://botkit.ai/docs/readme-web.html#message-objects`
                    ];
                    console.error(alert.join('\n'));
                    console.error(e);
                }
 
            });
 
            ws.on('error', (err) => console.error('Websocket Error: ', err));
 
            ws.on('close', function() {
                bot.connected = false;
            });
 
        });
 
        setInterval(function ping() {
            wss.clients.forEach(function each(ws) {
                if (ws.isAlive === false) {
                    return ws.terminate();
                }
                //  if (ws.isAlive === false) return ws.terminate()
                ws.isAlive = false;
                ws.ping('', false, true);
            });
        }, 30000);
 
    };
 
 
    controller.middleware.ingest.use(function(bot, message, reply_channel, next) {
 
        /*
         * this could be a message from the WebSocket
         * or it might be coming from a webhook.
         * configure the bot appropriately so the reply goes to the right place!
         */
        if (!bot.ws) {
            bot.http_response = reply_channel;
        }
 
        /*
         * look for an existing conversation for this user/channel combo
         * why not just pass in message? because we only care if there is a conversation  ongoing
         * and we might be dealing with "silent" message that would not otherwise match a conversation
         */
        bot.findConversation({
            user: message.user,
            channel: message.channel
        }, function(convo) {
            if (convo) {
                if (bot.ws) {
                    // replace the websocket connection
                    convo.task.bot.ws = bot.ws;
                    convo.task.bot.connected = true;
                    if (message.type == 'hello' || message.type == 'welcome_back') {
                        message.type = 'reconnect';
                    }
                } else {
 
                    /*
                     * replace the reply channel in the active conversation
                     * this is the one that gets used to send the actual reply
                     */
                    convo.task.bot.http_response = bot.http_response;
                }
            }
            next();
        });
    });
 
    controller.middleware.categorize.use(function(bot, message, next) {
 
        if (message.type == 'message') {
            message.type = 'message_received';
        }
 
        next();
 
    });
 
    // simple message clone because its already in the right format!
    controller.middleware.format.use(function(bot, message, platform_message, next) {
 
        for (var key in message) {
            platform_message[key] = message[key];
        }
        if (!platform_message.type) {
            platform_message.type = 'message';
        }
        next();
 
    });
 
 
    controller.defineBot(function(botkit, config) {
        var bot = {
            type: 'socket',
            botkit: botkit,
            config: config || {},
            utterances: botkit.utterances,
        };
 
        bot.startConversation = function(message, cb) {
            botkit.startConversation(this, message, cb);
        };
 
        bot.createConversation = function(message, cb) {
            botkit.createConversation(this, message, cb);
        };
 
        bot.send = function(message, cb) {
            if (bot.connected || !bot.ws) {
                if (bot.ws) {
                    try {
                        if (bot.ws && bot.ws.readyState === WebSocket.OPEN) {
                            bot.ws.send(JSON.stringify(message), function(err) {
                                if (cb) {
                                    return cb(err, message);
                                }
                            });
                        } else {
                            console.error('Cannot send message to closed socket');
                        }
                    } catch (err) {
                        return cb(err);
                    }
                } else {
                    try {
                        bot.http_response.json(message);
                        if (cb) {
                            cb(null, message);
                        }
                    } catch (err) {
                        if (cb) {
                            return cb(err, message);
                        } else {
                            console.error('ERROR SENDING', err);
                        }
                    }
                }
            } else {
                setTimeout(function() {
                    bot.send(message, cb);
                }, 3000);
            }
        };
 
        bot.startTyping = function() {
            if (bot.connected) {
                try {
                    if (bot.ws && bot.ws.readyState === WebSocket.OPEN) {
                        bot.ws.send(JSON.stringify({
                            type: 'typing'
                        }), function(err) {
                            if (err) {
                                console.error('startTyping failed: ' + err.message);
                            }
                        });
                    } else {
                        console.error('Socket closed! Cannot send message');
                    }
                } catch (err) {
                    console.error('startTyping failed: ', err);
                }
            }
        };
 
        bot.typingDelay = function(message) {
 
            return new Promise(function(resolve) {
                var typingLength = 0;
                if (message.typingDelay) {
                    typingLength = message.typingDelay;
                } else {
                    var textLength;
                    if (message.text) {
                        textLength = message.text.length;
                    } else {
                        textLength = 80; //default attachment text length
                    }
 
                    var avgWPM = 150;
                    var avgCPM = avgWPM * 7;
 
                    typingLength = Math.min(Math.floor(textLength / (avgCPM / 60)) * 1000, 2000) * controller.config.typingDelayFactor;
                }
 
                setTimeout(function() {
                    resolve();
                }, typingLength);
            });
 
        };
 
        bot.replyWithTyping = function(src, resp, cb) {
 
            bot.startTyping();
            bot.typingDelay(resp).then(function() {
 
                if (typeof(resp) == 'string') {
                    resp = {
                        text: resp
                    };
                }
 
                resp.user = src.user;
                resp.channel = src.channel;
                resp.to = src.user;
 
                bot.say(resp, cb);
            });
        };
 
 
        bot.reply = function(src, resp, cb) {
 
            if (typeof(resp) == 'string') {
                resp = {
                    text: resp
                };
            }
 
            resp.user = src.user;
            resp.channel = src.channel;
            resp.to = src.user;
 
            if (resp.typing || resp.typingDelay || controller.config.replyWithTyping) {
                bot.replyWithTyping(src, resp, cb);
            } else {
                bot.say(resp, cb);
            }
        };
 
        bot.findConversation = function(message, cb) {
            botkit.debug('CUSTOM FIND CONVO', message.user, message.channel);
            for (var t = 0; t < botkit.tasks.length; t++) {
                for (var c = 0; c < botkit.tasks[t].convos.length; c++) {
                    if (
                        botkit.tasks[t].convos[c].isActive() &&
                        botkit.tasks[t].convos[c].source_message.user == message.user &&
                        botkit.excludedEvents.indexOf(message.type) == -1 // this type of message should not be included
                    ) {
                        botkit.debug('FOUND EXISTING CONVO!');
                        cb(botkit.tasks[t].convos[c]);
                        return;
                    }
                }
            }
 
            cb();
        };
 
 
        /*
         * return info about the specific instance of this bot
         * including identity information, and any other info that is relevant
         */
        bot.getInstanceInfo = function(cb) {
 
            return new Promise(function(resolve) {
                var instance = {
                    identity: {},
                    team: {},
                };
 
                if (bot.identity) {
                    instance.identity.name = bot.identity.name;
                    instance.identity.id = bot.identity.id;
 
                    instance.team.name = bot.identity.name;
                    instance.team.url = bot.identity.root_url;
                    instance.team.id = bot.identity.name;
 
                } else {
                    instance.identity.name = 'Botkit Web';
                    instance.identity.id = 'web';
                }
 
                if (cb) cb(null, instance);
                resolve(instance);
 
            });
        };
 
        bot.getMessageUser = function(message, cb) {
            return new Promise(function(resolve) {
                // normalize this into what botkit wants to see
                controller.storage.users.get(message.user, function(err, user) {
 
                    if (!user) {
                        user = {
                            id: message.user,
                            name: 'Unknown',
                            attributes: {},
                        };
                    }
 
                    var profile = {
                        id: user.id,
                        username: user.name,
                        first_name: user.attributes.first_name || '',
                        last_name: user.attributes.last_name || '',
                        full_name: user.attributes.full_name || '',
                        email: user.attributes.email, // may be blank
                        gender: user.attributes.gender, // no source for this info
                        timezone_offset: user.attributes.timezone_offset,
                        timezone: user.attributes.timezone,
                    };
 
                    if (cb) {
                        cb(null, profile);
                    }
                    resolve(profile);
                });
            });
 
        };
 
 
        return bot;
    });
 
    controller.handleWebhookPayload = function(req, res) {
        var payload = req.body;
        controller.ingest(controller.spawn({}), payload, res);
    };
 
    // change the speed of typing a reply in a conversation
    controller.setTypingDelayFactor = function(delayFactor) {
        controller.config.typingDelayFactor = delayFactor;
    };
 
    // Substantially shorten the delay for processing messages in conversations
    controller.setTickDelay(10);
 
    return controller;
}
 
module.exports = WebBot;