From e3ba435d7fd1e4cc19d36194ca58710dd9b8a610 Mon Sep 17 00:00:00 2001 From: Tom Kornblit Date: Mon, 2 May 2016 16:01:12 -0700 Subject: [PATCH 1/6] Added support for Facebook "attachment" type of objects for conversations. --- lib/CoreBot.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/CoreBot.js b/lib/CoreBot.js index c5e407049..6b7eeba0d 100755 --- a/lib/CoreBot.js +++ b/lib/CoreBot.js @@ -418,14 +418,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)] ); From fd323bab76cbae8d769df262b26af12aee1eaf20 Mon Sep 17 00:00:00 2001 From: Tom Kornblit Date: Mon, 2 May 2016 16:56:37 -0700 Subject: [PATCH 2/6] Part two for support for Facebook templates. This part allows to capture the values from the postback. --- lib/CoreBot.js | 2 +- lib/Facebook.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/CoreBot.js b/lib/CoreBot.js index 6b7eeba0d..522dcc7a2 100755 --- a/lib/CoreBot.js +++ b/lib/CoreBot.js @@ -71,7 +71,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]; } 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 = { From d3e22ea9e8b47bb0c00c390788d7a184e08fbd6c Mon Sep 17 00:00:00 2001 From: Tom Kornblit Date: Fri, 6 May 2016 13:36:58 -0700 Subject: [PATCH 3/6] findConvo method added to class. --- lib/CoreBot.js | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/lib/CoreBot.js b/lib/CoreBot.js index 522dcc7a2..13c40f2f9 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) { @@ -301,7 +324,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 +339,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), }); @@ -514,7 +537,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 +844,9 @@ function Botkit(configuration) { }); }; - if (cb) { cb(worker); } + if (cb) { + cb(worker); + } return worker; }; @@ -975,7 +1000,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 +1013,4 @@ function Botkit(configuration) { return botkit; } -module.exports = Botkit; +module.exports = Botkit; \ No newline at end of file From 34922752e742b5c4e56f0222f46e6abfa6122432 Mon Sep 17 00:00:00 2001 From: Tom Kornblit Date: Sat, 14 May 2016 21:41:37 -0400 Subject: [PATCH 4/6] Updated slackbot core to save user data model properly for application. --- lib/SlackBot.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) 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) { From 36169ec9cd9745f005a285e4f7d6ca309cc6b50e Mon Sep 17 00:00:00 2001 From: Tom Kornblit Date: Wed, 18 May 2016 18:58:31 -0400 Subject: [PATCH 5/6] Added hijacking capability to Botkit. --- lib/CoreBot.js | 74 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/lib/CoreBot.js b/lib/CoreBot.js index 13c40f2f9..bb0a7fff7 100755 --- a/lib/CoreBot.js +++ b/lib/CoreBot.js @@ -144,6 +144,10 @@ function Botkit(configuration) { } } + } else if (this.hijacked){ + + this.hijackHandler(message); + } else { // do nothing } @@ -391,29 +395,38 @@ function Botkit(configuration) { }; this.tick = function() { + var now = new Date(); + var handleTimeout = function() { + + // 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 + + }; + 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 + + handleTimeout.bind(this); + } else { if (this.messages.length) { if (typeof(this.messages[0].timestamp) == 'undefined' || @@ -490,15 +503,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) { + + handleTimeout.bind(this); + + } 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) { + + handleTimeout.bind(this); + + } 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); } } } From 1472dca5d085416d9bf9ea7d36098185455d05d5 Mon Sep 17 00:00:00 2001 From: Tom Kornblit Date: Wed, 18 May 2016 19:16:15 -0400 Subject: [PATCH 6/6] Added timeout function for 60 seconds. --- lib/CoreBot.js | 51 +++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/CoreBot.js b/lib/CoreBot.js index bb0a7fff7..0aa94676c 100755 --- a/lib/CoreBot.js +++ b/lib/CoreBot.js @@ -144,7 +144,7 @@ function Botkit(configuration) { } } - } else if (this.hijacked){ + } else if (this.hijacked) { this.hijackHandler(message); @@ -394,38 +394,43 @@ function Botkit(configuration) { this.task.conversationEnded(this); }; - this.tick = function() { + + this.checkTimeout = function() { var now = new Date(); - var handleTimeout = function() { + 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()); + // 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 (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'); - } + if (this.topics.timeout) { + this.status = 'ending'; + this.changeTopic('timeout'); + } else { + this.stop('timeout'); } - // otherwise do nothing + } + // otherwise do nothing - }; + }; + + this.tick = function() { + + var now = new Date(); if (this.isActive()) { if (this.handler) { - handleTimeout.bind(this); + this.checkTimeout(); } else { if (this.messages.length) { @@ -505,7 +510,7 @@ function Botkit(configuration) { if (this.isActive() && !this.messages.length && !this.handler) { if (this.hijacked) { - handleTimeout.bind(this); + this.checkTimeout(); } else { this.status = 'completed'; @@ -517,7 +522,7 @@ function Botkit(configuration) { } else if (this.sent.length) { // sent at least 1 message if (this.hijacked) { - handleTimeout.bind(this); + this.checkTimeout(); } else {