@@ -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