Skip to content

Commit 1862003

Browse files
author
Ben Brown
authored
Merge pull request howdyai#1313 from howdyai/handleActions
0.6.13 - bug fixes
2 parents e16892c + 45ada10 commit 1862003

File tree

10 files changed

+237
-298
lines changed

10 files changed

+237
-298
lines changed

changelog.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44

55
[Want to contribute? Read our guide!](https://github.com/howdyai/botkit/blob/master/CONTRIBUTING.md)
66

7+
# 0.6.13
8+
9+
* Fix bugs and refactor handling of message actions, particularly as they relate to Botkit Studio scripts
10+
11+
* Adjust mechanism used to construct facebook quick reply payload in order to support future formats. [PR #1301](https://github.com/howdyai/botkit/pull/1301)
12+
13+
* Promisify Facebook Messenger profile API. [PR #1300](https://github.com/howdyai/botkit/pull/1300)
14+
15+
* Lower the log priority from 'log' to 'debug' for several messages in Botkit Core and Botkit Slack.
16+
17+
718
# 0.6.12
819

920
* [Botkit has a brand new docs site!](https://botkit.ai/docs) We have begun transitioning the documentation out of this repo into a [dedicated documentation repo](https://github.com/howdyai/botkit-docs).

lib/BotFramework.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,12 @@ function BotFrameworkBot(configuration) {
5454
if (!err) {
5555
// Send message through connector
5656
bf_message.address = adr;
57-
console.log('SENDING VIA BOT FRAMEWORK', bf_message);
58-
5957
bot.connector.send([bf_message], done);
6058
} else {
6159
done(err);
6260
}
6361
});
6462
} else {
65-
console.log('SENDING VIA BOT FRAMEWORK', bf_message);
6663
// Send message through connector
6764
bot.connector.send([bf_message], done);
6865
}
@@ -82,7 +79,6 @@ function BotFrameworkBot(configuration) {
8279
msg.address = src.address;
8380
msg.to = src.user;
8481

85-
console.log('SENDING REPLY', msg);
8682
bot.say(msg, cb);
8783
};
8884

lib/CiscoSparkbot.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ function Sparkbot(configuration) {
5454
controller.api.webhooks.list().then(function(list) {
5555
for (var i = 0; i < list.items.length; i++) {
5656
controller.api.webhooks.remove(list.items[i]).then(function(res) {
57-
console.log('Removed subscription: ' + list.items[i].name);
57+
// console.log('Removed subscription: ' + list.items[i].name);
5858
}).catch(function(err) {
59-
console.log('Error removing subscription:', err);
59+
console.error('Error removing subscription:', err);
6060
});
6161
}
6262
});
@@ -103,7 +103,7 @@ function Sparkbot(configuration) {
103103
console.log('Cisco Spark: SUCCESSFULLY UPDATED CISCO SPARK WEBHOOKS');
104104
if (cb) cb();
105105
}).catch(function(err) {
106-
console.log('FAILED TO REGISTER WEBHOOK', err);
106+
console.error('FAILED TO REGISTER WEBHOOK', err);
107107
throw new Error(err);
108108
});
109109

@@ -119,7 +119,7 @@ function Sparkbot(configuration) {
119119
console.log('Cisco Spark: SUCCESSFULLY REGISTERED CISCO SPARK WEBHOOKS');
120120
if (cb) cb();
121121
}).catch(function(err) {
122-
console.log('FAILED TO REGISTER WEBHOOK', err);
122+
console.error('FAILED TO REGISTER WEBHOOK', err);
123123
throw new Error(err);
124124
});
125125

@@ -142,7 +142,6 @@ function Sparkbot(configuration) {
142142
controller.middleware.ingest.use(function limitUsers(bot, message, res, next) {
143143

144144
if (controller.config.limit_to_org) {
145-
console.log('limit to org', controller.config.limit_to_org, message.raw_message.orgId);
146145
if (!message.raw_message.orgId || message.raw_message.orgId != controller.config.limit_to_org) {
147146
// this message is from a user outside of the proscribed org
148147
console.log('WARNING: this message is from a user outside of the proscribed org', controller.config.limit_to_org);

lib/CoreBot.js

Lines changed: 90 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,85 @@ function Botkit(configuration) {
313313
}
314314
};
315315

316+
317+
this.handleAction = function(condition) {
318+
// condition.action
319+
// if (condition.action=='execute_script')
320+
// condition.execute will be present
321+
var that = this;
322+
switch (condition.action) {
323+
case 'execute_script':
324+
if (condition.execute) {
325+
var script = condition.execute.script;
326+
var thread = condition.execute.thread;
327+
328+
// this will stop the conversation from automatically ending while the transition takes place
329+
that.status = 'transitioning';
330+
331+
botkit.studio.get(that.context.bot, script, that.source_message.user, that.source_message.channel, that.source_message).then(function(new_convo) {
332+
333+
that.context.transition_to = new_convo.context.script_name || null;
334+
that.context.transition_to_id = new_convo.context.script_id || null;
335+
that.stop('transitioning to ' + script);
336+
337+
// copy any question responses
338+
for (var key in that.responses) {
339+
new_convo.responses[key] = that.responses[key];
340+
}
341+
342+
// copy old variables into new conversation
343+
for (var key in that.vars) {
344+
new_convo.setVar(key, that.vars[key]);
345+
}
346+
347+
new_convo.context.transition_from = that.context.script_name || null;
348+
new_convo.context.transition_from_id = that.context.script_id || null;
349+
350+
// if thread == default, this is the normal behavior and we don't need to call gotoThread
351+
// in fact, calling gotoThread will cause it to override behaviors in the scripts `before` hook.
352+
if (thread != 'default') {
353+
new_convo.gotoThread(thread);
354+
}
355+
356+
new_convo.activate();
357+
}).catch(function(err) {
358+
console.error('Error executing script transition:', err);
359+
});
360+
}
361+
break;
362+
363+
case 'next':
364+
that.next();
365+
break;
366+
case 'repeat':
367+
that.repeat();
368+
that.next();
369+
break;
370+
case 'stop':
371+
that.stop();
372+
break;
373+
case 'wait':
374+
that.silentRepeat();
375+
break;
376+
case 'complete':
377+
that.stop('completed');
378+
break;
379+
case 'timeout':
380+
that.stop('timeout');
381+
break;
382+
default:
383+
if (typeof(condition.action) == 'function') {
384+
condition.action(that);
385+
} else {
386+
that.gotoThread(condition.action);
387+
}
388+
break;
389+
}
390+
391+
};
392+
316393
this.evaluateCondition = function(condition) {
317394

318-
// console.log('EVALUATE A CONDITION', condition);
319395
var that = this;
320396
var left = this.replaceTokens(condition.left);
321397
var right = this.replaceTokens(condition.right);
@@ -348,55 +424,7 @@ function Botkit(configuration) {
348424
}
349425

350426
if (passed) {
351-
switch (condition.action) {
352-
case 'execute_script':
353-
if (condition.execute) {
354-
var script = condition.execute.script;
355-
var thread = condition.execute.thread;
356-
botkit.studio.get(that.context.bot, script, that.source_message.user, that.source_message.channel, that.source_message).then(function(new_convo) {
357-
358-
that.context.transition_to = new_convo.context.script_name || null;
359-
that.context.transition_to_id = new_convo.context.script_id || null;
360-
that.stop('transitioning to ' + script);
361-
362-
// copy any question responses
363-
for (var key in that.responses) {
364-
new_convo.responses[key] = that.responses[key];
365-
}
366-
367-
// copy old variables into new conversation
368-
for (var key in that.vars) {
369-
new_convo.setVar(key, that.vars[key]);
370-
}
371-
372-
new_convo.context.transition_from = that.context.script_name || null;
373-
new_convo.context.transition_from_id = that.context.script_id || null;
374-
375-
new_convo.gotoThread(thread);
376-
new_convo.activate();
377-
});
378-
}
379-
break;
380-
381-
case 'next':
382-
that.next();
383-
break;
384-
case 'repeat':
385-
// before continuing, repeat the last send message
386-
// use sayFirst, so that it prepends it to the front of script
387-
that.sayFirst(that.sent[that.sent.length - 1]);
388-
that.next();
389-
break;
390-
case 'stop':
391-
that.stop();
392-
break;
393-
case 'wait':
394-
that.silentRepeat();
395-
break;
396-
default:
397-
that.changeTopic(condition.action);
398-
break;
399-
}
427+
that.handleAction(condition);
400428
}
401429

402430
this.tick();
@@ -447,9 +475,15 @@ function Botkit(configuration) {
447475

448476
this.repeat = function() {
449477
if (this.sent.length) {
450-
this.messages.push(this.sent[this.sent.length - 1]);
478+
// is this the last message in the queue? then just push it on again
479+
// if not, sayFirst it to the front so it doesn't repeat AFTER other messages
480+
if (!this.messages.length) {
481+
this.messages.push(this.sent[this.sent.length - 1]);
482+
} else {
483+
this.sayFirst(this.sent[this.sent.length - 1]);
484+
}
451485
} else {
452-
// console.log('TRIED TO REPEAT, NOTHING TO SAY');
486+
// do nothing
453487
}
454488
};
455489

@@ -856,7 +890,6 @@ function Botkit(configuration) {
856890
if (typeof(this.messages[0].timestamp) == 'undefined' ||
857891
this.messages[0].timestamp <= now.getTime()) {
858892
var message = this.messages.shift();
859-
//console.log('HANDLING NEW MESSAGE',message);
860893
// make sure next message is delayed appropriately
861894
if (this.messages.length && this.messages[0].delay) {
862895
this.messages[0].timestamp = now.getTime() + this.messages[0].delay;
@@ -871,11 +904,9 @@ function Botkit(configuration) {
871904
} else {
872905

873906
if (message.handler) {
874-
//console.log(">>>>>> SET HANDLER IN TICK");
875907
this.handler = message.handler;
876908
} else {
877909
this.handler = null;
878-
//console.log(">>>>>>> CLEARING HANDLER BECAUSE NO HANDLER NEEDED");
879910
}
880911
if (message.capture_options) {
881912
this.capture_options = message.capture_options;
@@ -925,25 +956,11 @@ function Botkit(configuration) {
925956
});
926957
}
927958
if (message.action) {
928-
if (typeof(message.action) == 'function') {
929-
message.action(this);
930-
} else if (message.action == 'repeat') {
931-
this.repeat();
932-
} else if (message.action == 'wait') {
933-
this.silentRepeat();
934-
} else if (message.action == 'stop') {
935-
this.stop();
936-
} else if (message.action == 'complete') {
937-
this.stop('completed');
938-
} else if (message.action == 'timeout') {
939-
this.stop('timeout');
940-
} else if (this.threads[message.action]) {
941-
this.gotoThread(message.action);
942-
}
959+
this.handleAction(message);
943960
}
944961
} // if not conditional
945962
} else {
946-
//console.log('Waiting to send next message...');
963+
// do nothing
947964
}
948965

949966
// end immediately instad of waiting til next tick.
@@ -1030,7 +1047,7 @@ function Botkit(configuration) {
10301047
};
10311048

10321049
this.taskEnded = function() {
1033-
botkit.log('[End] ', this.id, ' Task for ',
1050+
botkit.debug('[End] ', this.id, ' Task for ',
10341051
this.source_message.user, 'in', this.source_message.channel);
10351052

10361053
this.status = 'completed';
@@ -1427,7 +1444,7 @@ function Botkit(configuration) {
14271444
var task = new Task(bot, message, this);
14281445

14291446
task.id = botkit.taskCount++;
1430-
botkit.log('[Start] ', task.id, ' Task for ', message.user, 'in', message.channel);
1447+
botkit.debug('[Start] ', task.id, ' Task for ', message.user, 'in', message.channel);
14311448

14321449
var convo = task.startConversation(message);
14331450

0 commit comments

Comments
 (0)