All files Sockets.js

5.88% Statements 7/119
0% Branches 0/43
0% Functions 0/21
6.03% Lines 7/116
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 2421x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                               1x                        
var Botkit = require(__dirname + '/CoreBot.js');
var WebSocket = require('ws');
var express = require('express');
var bodyParser = require('body-parser');
var querystring = require('querystring');
var http = require('http');
 
function SocketBot(configuration) {
 
    var controller = Botkit(configuration || {});
 
 
 
    controller.openSocketServer = function(server) {
 
        // create the socket server along side the existing webserver.
        var wss = new WebSocket.Server({ server });
 
        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) {
 
                console.log('received: %s', message);
 
                var message = JSON.parse(message);
                bot.findConversation(message, function(convo) {
                    var reconnected = false;
                    if (convo) {
                        if (convo.task.bot.connected) {
                            console.log('WEIRD STILL CONNECTED?');
                        } else {
                            console.log('BOT JUST RECONNECTED');
                            convo.task.bot.ws = bot.ws;
                            convo.task.bot.connected = true;
                            reconnected = true;
                            controller.trigger('reconnect', [bot, message]);
                        }
                    }
 
                    if (message.type == 'message') {
                        controller.receiveMessage(bot, message);
                    } else if (message.type == 'hello' && !reconnected) {
 
                        var uid = message.user;
                        if (!uid) {
                            uid = guid();
                            message.user = uid;
                            bot.send({
                                type: 'hello',
                                user: uid,
                            });
                            controller.trigger(message.type, [bot, message]);
                        } else {
                            controller.trigger('welcome_back', [bot, message]);
                        }
 
                    } else {
                        controller.trigger(message.type, [bot, message]);
                    }
 
 
                });
 
            });
 
            ws.on('close', function(err) {
                console.log('CLOSED', err);
                bot.connected = false;
            });
 
        });
 
        var interval = setInterval(function ping() {
            wss.clients.forEach(function each(ws) {
                if (ws.isAlive === false) {
                    console.log('!!!!!!!!!!!!!!!!!!!! ws terminated !!!!!!!!!!!!!!!!!!!!');
                    return ws.terminate();
                }
                //  if (ws.isAlive === false) return ws.terminate()
                ws.isAlive = false;
                ws.ping('', false, true);
            });
        }, 30000);
    };
 
 
    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) {
                console.log('SEND', message);
                try {
                    console.log('sending type ahead');
                    bot.ws.send(JSON.stringify({typing: true}));
 
                    var textLength;
                    if (message.text) {
                        textLength = message.text.length;
                    } else {
                        textLength = 80; //default attachement text length
                    }
 
                    var avgWPM = 85;
                    var avgCPM = avgWPM * 7;
 
                    var typingLength = Math.min(Math.floor(textLength / (avgCPM / 60)) * 1000, 5000);
 
                    setTimeout(function() {
                        console.log('sending for reals');
                        try {
                            bot.ws.send(JSON.stringify(message));
                        } catch (err) {
                            console.error('ERROR SENDING', err);
                            if (cb) return cb(err, message);
                        }
                        if (cb) cb(null, message);
 
                    }, typingLength);
                } catch (err) {
                    console.error('ERROR SENDING', err);
                }
            } else {
                console.log('not connected. wait to resend');
                setTimeout(function() {
                    bot.send(message, cb);
                }, 3000);
            }
        };
 
        bot.reply = function(src, resp, cb) {
 
            if (typeof(resp) == 'string') {
                resp = {
                    text: resp
                };
            }
 
            resp.user = src.user;
            resp.channel = src.channel;
 
            bot.send(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.debug('FOUND EXISTING CONVO!');
                        cb(botkit.tasks[t].convos[c]);
                        return;
                    }
                }
            }
 
            cb();
        };
        return bot;
 
 
    });
 
    controller.setupWebserver = function(port, cb) {
 
        if (!port) {
            throw new Error('Cannot start webserver without a port');
        }
        if (isNaN(port)) {
            throw new Error('Specified port is not a valid number');
        }
 
        var static_dir =  process.cwd() + '/public';
 
        if (controller.config && controller.config.webserver && controller.config.webserver.static_dir)
            static_dir = controller.config.webserver.static_dir;
 
        controller.config.port = port;
 
        controller.webserver = express();
        controller.webserver.use(bodyParser.json());
        controller.webserver.use(bodyParser.urlencoded({ extended: true }));
        controller.webserver.use(express.static(static_dir));
 
        var server = controller.webserver.listen(
            controller.config.port,
            controller.config.hostname,
            function() {
                controller.log('** Starting webserver on port ' +
                    controller.config.port);
                if (cb) { cb(null, controller.webserver); }
            });
 
        return controller;
 
    };
 
 
    return controller;
 
}
 
module.exports = SocketBot;
 
 
function guid() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000)
          .toString(16)
          .substring(1);
    }
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
      s4() + '-' + s4() + s4() + s4();
}