Botkit: Trigger a default event if none of the event Regexp matches.

Created on 25 Aug 2017  路  6Comments  路  Source: howdyai/botkit

How can I trigger a default event if none of the event Regexp defined in "botkit.hears" is triggered?

Most helpful comment

@tejzpr a key factor I neglected to mention: you have to define these catch-all handlers _at the end of your code_. Botkit fires handlers in the order they are specified, and will stop handling once a hears pattern is matched.

All 6 comments

You can do a catch all handler in a few ways.

capture every mention that didn't get otherwise heard:

controller.on('direct_mention', function(bot, message) {
// .. do default action 
});

or, hear a wildcard:

controller.hears('.*', 'direct_mention', function(bot, message) {
// .. do default action 
});

@benbrown Thank you for the suggestions 馃憤 . I tried both your suggestions, but it does not work as expected. The requirement is that if none of the predefined "controller.hear" is triggered then the default action should trigger.

1) controller.on('direct_mention') : Calls the default action every time a request is made, regardless of other existing actions. For e.g. I have an action defined for the keyword "help", controller.on('direct_mention') is called first every time, then the 'help' is also triggered.

2) controller.hears('.*') : overrides all other keywords. for e.g. When I trigger the keyword "help", only default action is triggered and "help" is not triggered.

@tejzpr a key factor I neglected to mention: you have to define these catch-all handlers _at the end of your code_. Botkit fires handlers in the order they are specified, and will stop handling once a hears pattern is matched.

@benbrown Thank you! That worked. 馃挴

Just to be sure: It has to be the last loaded skill for the bot ? I am also facing weird effects

@simonfranzen yes, hears are processed in order.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abinashmohanty picture abinashmohanty  路  4Comments

TheJimFactor picture TheJimFactor  路  4Comments

JonnyBoy333 picture JonnyBoy333  路  3Comments

koubenecn picture koubenecn  路  3Comments

stelio picture stelio  路  4Comments