All files Alexa.js

7.69% Statements 6/78
0% Branches 0/43
0% Functions 0/13
7.89% Lines 6/76
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 1661x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                               1x  
var path = require('path');
var os = require('os');
var Botkit = require('./CoreBot');
var express = require('express');
var bodyParser = require('body-parser');
 
function Alexa(configuration) {
 
    var controller = Botkit(configuration || {});
 
    if (!configuration) {
        throw Error('Specify your \'account_sid\', \'auth_token\', and ' +
            '\'twilio_number\' as properties of the \'configuration\' object');
    }
 
    if (configuration && !configuration.account_sid) {
        throw Error('Specify an \'account_sid\' in your configuration object');
    }
 
    if (configuration && !configuration.auth_token) {
        throw Error('Specify an \'auth_token\'');
    }
 
    if (configuration && !configuration.twilio_number) {
        throw Error('Specify a \'twilio_number\'');
    }
 
    controller.defineBot(function(botkit, config) {
 
        var bot = {
            type: 'alexa',
            botkit: botkit,
            config: config || {},
            utterances: botkit.utterances
        };
 
        bot.startConversation = function(message, cb) {
            botkit.startConversation(bot, message, cb);
        };
 
        bot.createConversation = function(message, cb) {
            botkit.createConversation(bot, message, cb);
        };
 
 
        bot.send = function(message, cb) {
 
 
        };
 
        bot.reply = function(src, resp, cb) {
            var msg = {};
 
            if (typeof resp === 'string') {
                msg.text = resp;
            } else {
                msg = resp;
            }
 
            msg.channel = src.channel;
 
            if (typeof cb === 'function') {
                bot.say(msg, cb);
            } else {
                bot.say(msg, function() {});
            }
 
        };
 
        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++) {
 
                    var convo = botkit.tasks[t].convos[c];
                    var matchesConvo = (
                        convo.source_message.channel === message.channel ||
                        convo.source_message.user === message.user
                    );
 
                    if (convo.isActive() && matchesConvo) {
                        botkit.debug('FOUND EXISTING CONVO!');
                        cb(botkit.tasks[t].convos[c]);
                        return;
                    }
 
                }
            }
 
            cb();
        };
 
        return bot;
    });
 
    controller.handleWebhookPayload = function(req, res, bot) {
 
        controller.log('=> Got a message hook');
        console.log(req.body);
        var message = {};
        controller.receiveMessage(bot, message);
 
    };
 
    // set up a web route for receiving outgoing webhooks
    controller.createWebhookEndpoints = function(webserver, bot, cb) {
 
        controller.log('** Serving webhook endpoints for Twilio Programmable SMS' +
            ' at: ' + os.hostname() + ':' + controller.config.port + '/alexa/receive');
 
        var endpoint = controller.config.endpoint || '/alexa/receive';
 
        webserver.post(endpoint, function(req, res) {
            controller.handleWebhookPayload(req, res, bot);
 
            res.send('');
        });
 
        if (cb) cb();
 
        return controller;
    };
 
    controller.setupWebserver = function(port, cb) {
 
        if (!port) {
            throw new Error('Cannot start webserver without a \'port\' parameter');
        }
 
        if (isNaN(port)) {
            throw new TypeError('Specified \'port\' parameter is not a valid number');
        }
 
        var static_dir = path.join(__dirname, '/public');
 
        var config = controller.config;
 
        if (config && config.webserver && 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));
 
        controller.webserver.listen(controller.config.port, function() {
 
            controller.log('*> Listening on port ' + controller.config.port);
            controller.startTicking();
            if (cb) cb(null, controller.webserver);
 
        });
 
        return controller;
    };
 
    return controller;
}
 
module.exports = Alexa;