Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Added support for Facebook "attachment" type of objects for conversat… #208

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 92 additions & 32 deletions lib/CoreBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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];
}

Expand Down Expand Up @@ -121,6 +144,10 @@ function Botkit(configuration) {
}

}
} else if (this.hijacked) {

this.hijackHandler(message);

} else {
// do nothing
}
Expand Down Expand Up @@ -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),
};
Expand All @@ -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),
});
Expand Down Expand Up @@ -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' ||
Expand Down Expand Up @@ -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)]
);
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -821,7 +877,9 @@ function Botkit(configuration) {
});
};

if (cb) { cb(worker); }
if (cb) {
cb(worker);
}
return worker;
};

Expand Down Expand Up @@ -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.');
}
Expand All @@ -986,4 +1046,4 @@ function Botkit(configuration) {
return botkit;
}

module.exports = Botkit;
module.exports = Botkit;
5 changes: 3 additions & 2 deletions lib/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
23 changes: 17 additions & 6 deletions lib/SlackBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down