All files / lib BotFramework.js

4.05% Statements 3/74
0% Branches 0/35
0% Functions 0/11
4.05% Lines 3/74
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 1801x 1x                                                                                                                                                                                                                                                                                                                                                                 1x  
var Botkit = require(__dirname + '/CoreBot.js');
var builder = require('botbuilder');
 
function BotFrameworkBot(configuration) {
 
    // Create a core botkit bot
    var bf_botkit = Botkit(configuration || {});
 
    // customize the bot definition, which will be used when new connections
    // spawn!
    bf_botkit.defineBot(function(botkit, config) {
 
        var bot = {
            botkit: botkit,
            config: config || {},
            utterances: botkit.utterances,
        };
 
        bot.send = function(message, cb) {
            function done(err, res) {
                if (cb) {
                    cb(err);
                }
            }
 
            if (!message || !message.address) {
                if (cb) {
                    cb(new Error('Outgoing message requires a valid address...'));
                }
                return;
            }
 
            // Copy message minus user & channel fields
            var bf_message = {};
            for (var key in message) {
                switch (key) {
                    case 'user':
                    case 'channel':
                        // ignore
                    break;
                    default:
                        bf_message[key] = message[key];
                    break;
                }
            }
            if (!bf_message.type) {
                bf_message.type = 'message';
            }
 
 
            // Ensure the message address has a valid conversation id.
            if (!bf_message.address.conversation) {
                bot.connector.startConversation(bf_message.address, function(err, adr) {
                    if (!err) {
                        // Send message through connector
                        bf_message.address = adr;
                        bot.connector.send([bf_message], done);
                    } else {
                        done(err);
                    }
                });
            } else {
                // Send message through connector
                bot.connector.send([bf_message], done);
            }
        };
 
        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;
            msg.address = src.address;
            msg.to = src.user;
 
            bot.say(msg, 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.tasks[t].convos[c].source_message.channel == message.channel &&
                        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();
        };
 
        // Create connector
        bot.connector = new builder.ChatConnector(config);
 
        return bot;
 
    });
 
 
    bf_botkit.middleware.normalize.use(function(bot, message, next) {
 
        // Break out user & channel fields from event
        // - These fields are used as keys for tracking conversations and storage.
        // - Prefixing with channelId to ensure that users & channels for different
        //   platforms are unique.
 
        var prefix = message.address.channelId + ':';
        message.user = prefix + message.address.user.id;
        message.channel = prefix + message.address.conversation.id;
 
        // MS supplies a type field that is 'message' for most messages, but we want it to be our more generic message_received event
        if (message.type == 'message') {
            message.type = 'message_received';
        }
 
        next();
 
    });
 
    bf_botkit.middleware.format.use(function(bot, message, platform_message, next) {
        // clone the incoming message
        for (var k in message) {
            platform_message[k] = message[k];
        }
 
        next();
    });
 
    // set up a web route for receiving outgoing webhooks and/or slash commands
 
    bf_botkit.createWebhookEndpoints = function(webserver, bot, cb) {
 
        // Listen for incoming events
        bf_botkit.log(
            '** Serving webhook endpoints for the Microsoft Bot Framework at: ' +
                'http://' + bf_botkit.config.hostname + ':' +
                bf_botkit.config.port + '/botframework/receive');
        webserver.post('/botframework/receive', bot.connector.listen());
 
        // Receive events from chat connector
        bot.connector.onEvent(function(events, done) {
            for (var i = 0; i < events.length; i++) {
                var bf_event = events[i];
 
                bf_botkit.ingest(bot, bf_event, null);
 
            }
 
            if (done) {
                done(null);
            }
        });
 
        if (cb) {
            cb();
        }
 
        bf_botkit.startTicking();
 
        return bf_botkit;
    };
 
    return bf_botkit;
};
 
module.exports = BotFrameworkBot;