All files / lib TwilioIPMBot.js

3.01% Statements 5/166
0% Branches 0/97
0% Functions 0/37
3.01% Lines 5/166
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 3431x 1x 1x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   1x  
var Botkit = require(__dirname + '/CoreBot.js');
var request = require('request');
var Twilio = require('twilio');
var async = require('async');
 
 
function Twiliobot(configuration) {
 
    // Create a core botkit bot
    var twilio_botkit = Botkit(configuration || {});
 
    // customize the bot definition, which will be used when new connections
    // spawn!
    twilio_botkit.defineBot(function(botkit, config) {
        var bot = {
            type: 'twilioipm',
            botkit: botkit,
            config: config || {},
            utterances: botkit.utterances,
        };
 
        bot.send = function(message, cb) {
            botkit.debug('SEND ', message);
 
            bot.api.channels(message.channel).messages.create(message)
                .then(function(response) {
                    cb(null, response);
                }).catch(function(err) {
                    cb(err);
                });
        };
 
        bot.reply = function(src, resp, cb) {
            var msg = {};
 
            if (typeof(resp) == 'string') {
                msg.text = resp;
            } else {
                msg = resp;
            }
 
            msg.user = src.user;
            msg.channel = src.channel;
 
            bot.say(msg, cb);
        };
 
        bot.autoJoinChannels = function() {
            bot.api.channels.list().then(function(full_channel_list) {
                if (bot.config.autojoin === true) {
                    bot.channels = full_channel_list;
                    bot.channels.forEach(function(chan) {
                        bot.api.channels(chan.sid).members.create({
                            identity: bot.identity
                        }).then(function(response) {
                            botkit.debug('added ' +
                              bot.identity + ' as a member of the ' + chan.friendlyName);
                        }).catch(function(error) {
                            botkit.debug('Couldn\'t join the channel: ' +
                                chan.friendlyName + ': ' + error);
                        });
                    });
                } else if (bot.identity) {
 
                    // load up a list of all the channels that the bot is currently
 
                    bot.channels = [];
 
                    async.each(full_channel_list, function(chan, next) {
                        bot.api.channels(chan.sid).members.list().then(function(members) {
                            for (var x = 0; x < members.length; x++) {
                                if (members[x].identity === bot.identity) {
                                    bot.channels.push(chan);
                                }
                            }
                            next();
                        }).catch(function(error) {
                            botkit.log('Error loading channel member list: ', error);
                            next();
                        });
                    });
                }
            }).catch(function(error) {
                botkit.log('Error loading channel list: ' + error);
                // fails if no channels exist
                // set the channels to empty
                bot.channels = { channels: [] };
            });
 
        };
 
        bot.configureBotIdentity = function() {
            if (bot.identity !== null || bot.identity !== '') {
                var userRespIter = 0;
                var existingIdentity = null;
 
                // try the get by identity thing
                bot.api.users(bot.identity).fetch().then(function(response) {
                    bot.autoJoinChannels();
                }).catch(function(error) {
                    // if not make the new user and see if they need to be added to all the channels
                    bot.api.users.create({
                        identity: bot.identity
                    }).then(function(response) {
                        bot.autoJoinChannels();
                    }).catch(function(error) {
                        botkit.log('Could not get Bot Identity:');
                        botkit.log(error);
                        process.exit(1);
                    });
                });
            }
        };
 
        /**
        * This handles the particulars of finding an existing conversation or
        * topic to fit the message into...
        */
        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.tasks[t].convos[c].source_message.channel == message.channel
                    ) {
                        botkit.debug('FOUND EXISTING CONVO!');
                        cb(botkit.tasks[t].convos[c]);
                        return;
                    }
                }
            }
 
            cb();
        };
 
 
        bot.client = new Twilio(config.TWILIO_ACCOUNT_SID, config.TWILIO_AUTH_TOKEN);
        bot.api = bot.client.chat.services(config.TWILIO_IPM_SERVICE_SID);
 
        if (config.identity) {
            bot.identity = config.identity;
            bot.configureBotIdentity();
        }
 
        return bot;
 
    });
 
    // set up a web route for receiving outgoing webhooks and/or slash commands
    twilio_botkit.createWebhookEndpoints = function(webserver, bot) {
 
        twilio_botkit.log(
            '** Serving webhook endpoints for receiving messages ' +
            'webhooks at: http://' + twilio_botkit.config.hostname + ':' +
                twilio_botkit.config.port + '/twilio/receive');
        webserver.post('/twilio/receive', function(req, res) {
 
            res.status(200);
            res.send('ok');
            twilio_botkit.handleWebhookPayload(req, res, bot);
 
        });
 
 
        return twilio_botkit;
    };
 
    twilio_botkit.handleWebhookPayload = function(req, res, bot) {
        var message = req.body;
        twilio_botkit.ingest(bot, message, res);
    };
 
    //normalize the message to ensure channel, and user properties are present
    twilio_botkit.middleware.normalize.use(function normalizeMessage(bot, message, next) {
        if (message.EventType == 'onMessageSent') {
 
            // customize fields to be compatible with Botkit
            message.text = message.Body;
            message.from = message.From;
            message.to = message.To;
            message.user = message.From;
            message.channel = message.ChannelSid;
 
        } else if (message.EventType == 'onChannelAdded' || message.EventType == 'onChannelAdd') {
            // this event has a channel sid but not a user
            message.channel = message.ChannelSid;
 
        } else if (message.EventType == 'onChannelDestroyed' || message.EventType == 'onChannelDestroy') {
            // this event has a channel sid but not a user
            message.channel = message.ChannelSid;
 
        } else if (message.EventType == 'onMemberAdded' || message.EventType == 'onMemberAdd') {
            // should user be MemberSid the The Member Sid of the newly added Member
            message.user = message.Identity;
            message.channel = message.ChannelSid;
        } else if (message.EventType == 'onMemberRemoved' || message.EventType == 'onMemberRemove') {
            message.user = message.Identity;
            message.channel = message.ChannelSid;
        }
 
        next();
    });
 
    //categorize the message type with appropriate checks
    twilio_botkit.middleware.categorize.use(function categorizeMessage(bot, message, next) {
        if (message.EventType === 'onMessageSent') {
            //discard echo messages
            if (bot.identity && message.from === bot.identity) {
                return;
            }
 
            // message without text is probably an edit
            if (!message.text) {
                return;
            }
 
            if (bot.identity) {
                var channels = bot.channels;
 
                // if its not in a channel with the bot
                var apprChan = channels.filter(function(ch) {
                    return ch.sid === message.channel;
                });
 
                if (apprChan.length === 0) {
                    return;
                }
            }
 
            message.type = 'message_received';
 
        } else if (message.EventType === 'onMemberRemoved') {
 
            if (bot.identity && message.user === bot.identity) {
                // remove that channel from bot.channels.channels
                var chan_to_rem = bot.channels.map(function(ch) {
                    return ch.sid;
                }).indexOf(message.channel);
 
                if (chan_to_rem != -1) {
                    bot.channels.splice(chan_to_rem, 1);
                    twilio_botkit.debug('Unsubscribing from channel because of memberremove.');
 
                }
 
            } else if (bot.identity) {
                var channels = bot.channels;
 
                // if its not in a channel with the bot
                var apprChan = channels.filter(function(ch) {
                    return ch.sid == message.channel;
                });
 
                if (apprChan.length === 0) {
                    return;
                }
            }
 
            if (bot.identity && bot.identity == message.user) {
                message.type = 'bot_channel_leave';
            } else {
                message.type = 'user_channel_leave';
            }
        } else if (message.EventType === 'onMemberAdded') {
 
            if (bot.identity && message.user === bot.identity) {
                bot.api.channels(message.channel).fetch().then(function(response) {
                    bot.channels.push(response);
                    twilio_botkit.debug('Subscribing to channel because of memberadd.');
                }).catch(function(error) {
                    botkit.log(error);
                });
            } else if (bot.identity) {
                var channels = bot.channels;
 
                // if its not in a channel with the bot
                var apprChan = channels.filter(function(ch) {
                    return ch.sid == message.channel;
                });
 
                if (apprChan.length === 0) {
                    return;
                }
            }
 
            if (bot.identity && bot.identity == message.user) {
                message.type = 'bot_channel_join';
            } else {
                message.type = 'user_channel_join';
            }
        } else if (message.EventType === 'onChannelDestroyed') {
            if (bot.identity) {
                var chan_to_rem = bot.channels.map(function(ch) {
                    return ch.sid;
                }).indexOf(message.channel);
                if (chan_to_rem !== -1) {
                    bot.channels.splice(chan_to_rem, 1);
                    twilio_botkit.debug('Unsubscribing from destroyed channel.');
                }
                message.type = 'channel_destroyed';
            }
        } else if (message.EventType === 'onChannelAdded') {
            if (bot.identity && bot.config.autojoin === true) {
                // join the channel
                bot.api.channels(message.channel).members.create({
                    identity: bot.identity
                }).then(function(response) {
                    return bot.api.channels(message.channel).fetch().then(function(response) {
                        bot.channels.push(response);
                        twilio_botkit.debug('Subscribing to new channel.');
                    });
                }).catch(function(error) {
                    botkit.log(error);
                });
                message.type = message.EventType;
                message.type = 'channel_created';
            }
        } else {
            message.type = message.EventType;
        }
 
        next();
    });
 
    twilio_botkit.middleware.format.use(function formatMessage(bot, message, platform_message, next) {
        platform_message.channel = message.channel;
        platform_message.body = message.text;
        if (bot.identity) {
            platform_message.from = bot.identity;
        }
        next();
    });
 
    twilio_botkit.startTicking();
 
    return twilio_botkit;
 
}
 
module.exports = Twiliobot;