diff --git a/lib/CoreBot.js b/lib/CoreBot.js index c5e407049..0aa94676c 100755 --- a/lib/CoreBot.js +++ b/lib/CoreBot.js @@ -8,6 +8,7 @@ var simple_storage = require(__dirname + '/storage/simple_storage.js'); var ConsoleLogger = require(__dirname + '/console_logger.js'); var LogLevels = ConsoleLogger.LogLevels; var ware = require('ware'); +var _ = require("lodash"); function Botkit(configuration) { var botkit = { @@ -35,6 +36,28 @@ function Botkit(configuration) { receive: ware(), }; + botkit.findConvo = function(id) { + + var tasks = this.tasks; + + for (var i = 0; i < tasks.length; i++) { + + var task = tasks[i]; + + var convos = task.convos; + + for (var j = 0; j < convos.length; j++) { + + convo = convos[j]; + + if (convo.id === id) { + return convo; + } + } + } + + }; + function Conversation(task, message) { @@ -71,7 +94,7 @@ function Botkit(configuration) { // if text is an array, get 1st if (typeof(this.sent[this.sent.length - 1].text) == 'string') { response.question = this.sent[this.sent.length - 1].text; - } else { + } else if (Array.isArray(this.sent[this.sent.length - 1])) { response.question = this.sent[this.sent.length - 1].text[0]; } @@ -121,6 +144,10 @@ function Botkit(configuration) { } } + } else if (this.hijacked) { + + this.hijackHandler(message); + } else { // do nothing } @@ -301,7 +328,7 @@ function Botkit(configuration) { res[key] = { question: this.responses[key].length ? - this.responses[key][0].question : this.responses[key].question, + this.responses[key][0].question : this.responses[key].question, key: key, answer: this.extractResponse(key), }; @@ -316,7 +343,7 @@ function Botkit(configuration) { res.push({ question: this.responses[key].length ? - this.responses[key][0].question : this.responses[key].question, + this.responses[key][0].question : this.responses[key].question, key: key, answer: this.extractResponse(key), }); @@ -367,30 +394,44 @@ function Botkit(configuration) { this.task.conversationEnded(this); }; + + this.checkTimeout = function() { + + var now = new Date(); + + var timeLimit = 60000; //One minute + + // check timeout! + // how long since task started? + var duration = (now.getTime() - this.task.startTime.getTime()); + // how long since last active? + var lastActive = (now.getTime() - this.lastActive.getTime()); + + if (timeLimit && // has a timelimit + (duration > timeLimit) && // timelimit is up + (lastActive > timeLimit) // nobody has typed for 60 seconds at least + ) { + + if (this.topics.timeout) { + this.status = 'ending'; + this.changeTopic('timeout'); + } else { + this.stop('timeout'); + } + } + // otherwise do nothing + + }; + this.tick = function() { + var now = new Date(); if (this.isActive()) { if (this.handler) { - // check timeout! - // how long since task started? - var duration = (now.getTime() - this.task.startTime.getTime()); - // how long since last active? - var lastActive = (now.getTime() - this.lastActive.getTime()); - - if (this.task.timeLimit && // has a timelimit - (duration > this.task.timeLimit) && // timelimit is up - (lastActive > this.task.timeLimit) // nobody has typed for 60 seconds at least - ) { - - if (this.topics.timeout) { - this.status = 'ending'; - this.changeTopic('timeout'); - } else { - this.stop('timeout'); - } - } - // otherwise do nothing + + this.checkTimeout(); + } else { if (this.messages.length) { if (typeof(this.messages[0].timestamp) == 'undefined' || @@ -418,14 +459,14 @@ function Botkit(configuration) { this.transcript.push(message); this.lastActive = new Date(); - if (message.text || message.attachments) { + if (message.text || message.attachments || message.attachment) { // clone this object so as not to modify source var outbound = JSON.parse(JSON.stringify(message)); if (typeof(message.text) == 'string') { outbound.text = this.replaceTokens(message.text); - } else { + } else if (message.text) { outbound.text = this.replaceTokens( message.text[Math.floor(Math.random() * message.text.length)] ); @@ -467,15 +508,30 @@ function Botkit(configuration) { // end immediately instad of waiting til next tick. // if it hasn't already been ended by a message action! if (this.isActive() && !this.messages.length && !this.handler) { + if (this.hijacked) { + + this.checkTimeout(); + + } else { + this.status = 'completed'; + botkit.debug('Conversation is over!'); + this.task.conversationEnded(this); + } + } + + } else if (this.sent.length) { // sent at least 1 message + if (this.hijacked) { + + this.checkTimeout(); + + } else { + this.status = 'completed'; botkit.debug('Conversation is over!'); this.task.conversationEnded(this); + } - } else if (this.sent.length) { // sent at least 1 message - this.status = 'completed'; - botkit.debug('Conversation is over!'); - this.task.conversationEnded(this); } } } @@ -514,7 +570,7 @@ function Botkit(configuration) { this.conversationEnded = function(convo) { botkit.log('> [End] ', convo.id, ' Conversation with ', - convo.source_message.user, 'in', convo.source_message.channel); + convo.source_message.user, 'in', convo.source_message.channel); this.trigger('conversationEnded', [convo]); convo.trigger('end', [convo]); var actives = 0; @@ -821,7 +877,9 @@ function Botkit(configuration) { }); }; - if (cb) { cb(worker); } + if (cb) { + cb(worker); + } return worker; }; @@ -975,7 +1033,9 @@ function Botkit(configuration) { } } else if (configuration.json_file_store) { botkit.log('** Using simple storage. Saving data to ' + configuration.json_file_store); - botkit.storage = simple_storage({path: configuration.json_file_store}); + botkit.storage = simple_storage({ + path: configuration.json_file_store + }); } else { botkit.log('** No persistent storage method specified! Data may be lost when process shuts down.'); } @@ -986,4 +1046,4 @@ function Botkit(configuration) { return botkit; } -module.exports = Botkit; +module.exports = Botkit; \ No newline at end of file diff --git a/lib/Facebook.js b/lib/Facebook.js index d940264d7..6cc369be6 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -137,13 +137,14 @@ function Facebookbot(configuration) { } else if (facebook_message.postback) { var message = { - payload: facebook_message.postback.payload, + text: facebook_message.postback.payload, user: facebook_message.sender.id, channel: facebook_message.sender.id, timestamp: facebook_message.timestamp, }; - facebook_botkit.trigger('facebook_postback', [bot, message]); + facebook_botkit.receiveMessage(bot, message); + } else if (facebook_message.optin) { var message = { diff --git a/lib/SlackBot.js b/lib/SlackBot.js index 33e34044c..1ca01716a 100755 --- a/lib/SlackBot.js +++ b/lib/SlackBot.js @@ -362,18 +362,29 @@ function Slackbot(configuration) { slack_botkit.trigger('update_team', [bot, team]); } - slack_botkit.storage.users.get(identity.user_id, function(err, user) { + var type = "slack"; + + var id = type + "-" + identity.user_id; + + slack_botkit.storage.users.get(id, function(err, user) { + isnew = false; + if (!user) { isnew = true; user = { - id: identity.user_id, - access_token: auth.access_token, - scopes: scopes, - team_id: identity.team_id, - user: identity.user, + id : id, + type: type, + platform: { + id: identity.user_id, + user: identity.user, + access_token: auth.access_token, + scopes: scopes, + team_id: identity.team_id, + } }; } + slack_botkit.storage.users.save(user, function(err, id) { if (err) {