Triggers

Triggers are used for matching incoming messages from your users. These are the key words or phrases that you tell your bot to look out for and respond accordingly. These always start with a + (plus) sign, followed by a space, and followed by your key words or phrases.

+ this is a trigger

Some basic things to remember about triggers:

The Catch-All Trigger

A very special trigger is called the catch-all trigger. Instead of words, it simply looks like:

+ *

This means match anything. Literally anything. It will match hi. It will match goodbye. It will even match 😂🙄🎉. And it will respond with the response underneath.

Use catch-all triggers as a way to guide and educate lost users back onto a conversational path that you have designed. It’s a natural place to respond with messages that provide help.
We strongly encourage you to give your bot at least one catch-all trigger. There are just too many things that your users might say that you won't have written other triggers for. Read more under Best Practices.

Optional Parts of a Trigger

You can wrap words or phrases within a trigger with [ and ] to mark them as optional. For example:

+ [i love] dexter  

Will match just dexter as well as I love dexter

Optional Catch-All

Combining the optional [] brackets with the catch all * trigger allows for the creation of triggers that are not exact matches.

+ [*] coffee [*]

Will match coffee as well as i'd like to order a coffee and what coffee do you serve?

We at Dexter find ourselves using this + [*] keyword [*] pattern a lot. Unless you are sure that your user will send messages matching your trigger exactly, err on the side of caution and surround your keywords with optional catch-alls.

Multiple Matches

You can match on multiple phrases when you specify them using a vertical bar | and group them with parentheses:

+ i love (dexter|betaworks|bots) 


Advanced Trigger Concepts

Now that we’ve gotten the hang of writing triggers and responses, let’s kick it up a notch by introducing a few more advanced concepts that will spruce up the dialogue your bot has with users.

Before proceeding forward, we recommend first taking a look at the section on directions for recalling and remembering data if you haven’t already.

Conditionals

Conditionals allow the bot to make more intelligent responses by checking for whether a requirement is met before choosing how to respond to a user.

For example, if the bot wanted to greet a user by name, it would need to first know the user’s name! By including a conditional, the bot can check to see if it has previously saved the user’s name in a name variable before responding back.

Include a conditional by adding a new line immediately after a trigger. Start this line with an * (asterisk) sign, followed by a conditional test, followed by an => arrow and the response you want the bot to respond with if the conditional test ends up being true. If the conditional test is not met, the bot will default to the - response following the conditional.

+ hello
* <get name> == undefined => Hi there! I don’t believe we’ve met before, what’s your name?
- Hi <get name>, fancy meeting you again!
 
+ my name is *
- <set name=<star>>That’s a lovely name!

Conditional tests can check for more than just equality ==. Try writing tests with inequality !=, less than <, less than or equal to, greater than >, or greater than or equal to >= conditions.

The equality, less than, or greater than symbols must be padded by spaces. This will not work:

+ this will break
* <get name>==undefined => Hi there! I won't work.

One trigger-response pair can include more than one conditional. They will be checked from top to bottom. The first conditional test that comes back true will determine how the bot responds.

+ is my name billy
* <get name> == billy => Billy!!!
* <get name> == bill => Bill? Close enough.
* <get name> == william => William is a bit of a stretch...
- You’re not Billy, who are you!?
You can only evaluate one condition in a condition. Currently, you cannot use AND or OR statements in conditionals.

Punctuation in Triggers

You should not include any punctuation when writing your triggers. Most common punctuation marks that your users send will automatically be stripped out before your bot attempts to match against your triggers, the default set being .,!?;:'. This means that the trigger

+ whats up

will match messages whats up, what's up, and what's up?!?!??

There some scenarios where you don't want to remove punctuation your users sent—a common one is if you're trying to capture the exact data your user sent to your bot (learn more). You can override the punctuation marks that your bot strips out by adding a directive at the very top of your default topic.

For example, if you want to only strip out !? before matching against a trigger, put this at the top of your default topic:

# punc ?!

If you don't want any punctuations to be stripped, put this at the top of your default topic:

# punc nostrip
Even if you are not stripping out punctuation, you should never put any punctuation marks inside your triggers.

Triggers and Topics

For each message from a user, your bot will only attempt to match this message with the triggers found within the user's current topic. If no matches are found, it will look at the default topic for matching triggers. For more info, see Topics.

Weights on Triggers

In general, the bot will respond to the trigger that is the best possible match for the incoming user input.

For example, the bot would select the response of the first trigger Great, pick you up at 7! between the following two pairs when the user says "Let's go to the mall!"

+ lets go to the mall
- Great, pick you up at 7!
 
+ [*] mall [*]
- I love malls!

You can assign the second trigger more importance by attaching a weight to it, like so:

+ [*] mall [*] {weight=10}
- I love malls!

Now, since the user's suggestion Let's go to the mall! matches both triggers, it will respond with the trigger with the highest importance: I love malls!