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;
|