chatbot-constructor
Version:
A chatbot constructor/builder that can help you develop chatbots in no time either using javascript and nodejs or without programming using excel sheet or JSON format. And it can also easily integrate with AI services like LUIS, Watson, Lex, Octane.AI, Wi
97 lines (72 loc) • 2.59 kB
JavaScript
var Botkit = require(__dirname + '/CoreBot.js');
var request = require('request');
var express = require('express');
var bodyParser = require('body-parser');
var readline = require('readline');
function TextBot(configuration) {
// Create a core botkit bot
var text_botkit = Botkit(configuration || {});
text_botkit.middleware.spawn.use(function(bot, next) {
text_botkit.listenStdIn(bot);
next();
});
text_botkit.defineBot(function(botkit, config) {
var bot = {
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) {
console.log('BOT:', message.text);
cb && cb(null, message);
};
bot.reply = function(src, resp, cb) {
var msg = {};
if (typeof(resp) == 'string') {
msg.text = resp;
} else {
msg = resp;
}
msg.channel = src.channel;
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.debug('FOUND EXISTING CONVO!');
cb(botkit.tasks[t].convos[c]);
return;
}
}
}
cb();
};
return bot;
});
text_botkit.listenStdIn = function(bot) {
text_botkit.startTicking();
var rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: false });
rl.on('line', function(line) {
var message = {
text: line,
user: 'user',
channel: 'text',
timestamp: Date.now()
};
text_botkit.receiveMessage(bot, message);
});
};
return text_botkit;
};
module.exports = TextBot;