Responding to the User

The essence of a chatbot interaction is responding to a user. The user sends the bot a message, and the bot says something back — something relevant, something that creates the illusion that the bot understands the user.

Output sent to the user is managed by Messages. There are three ways to trigger a Message from user input:

Pattern Matching

Pattern matching is the most basic way to respond to user input. The bot checks user input against patterns created by you, and selects the message based on the match. Patterns are similar to regular expressions, but much simpler.

Matching Strings

The simplest pattern is a plain string. So, for example, if you create the pattern hello, this will only match if the user types hello. This will not match if the user types hello there or oh, hello.

Alternates

A match pattern can include several alternate substrings. Sets of alternates are wrapped in parenthesis and separated by vertical pipes.

For example, (hello|hi|hey) there will match to user inputs of hello there, hi there, or hey there. This can be used for words (like (hello|hi|hey)), whole phrases ((how are you|hello there|what’s up)*), or letters within a word (colo(|u)r).

There’s a lot you can do with alternates. See our Working with Alternates best practice guide for ideas.

Bounded or Unbounded Patterns

A bounded patten will only match user inputs if the whole input message matches the pattern. With an unbounded pattern, the input can include additional text at the beginning or end.

Unbounded patterns are marked with an asterisk at the beginning, end, or both.

  • Pattern test
    • will match test
    • will not match please test test now please test now
  • Pattern *test
    • will match test please test
    • will not match test now please test now
  • Pattern test*
    • will match test test now
    • will not match please test please test now
  • Pattern *test*
    • will match test test now please test please test now

Its usually a good idea to not put a space between the asterisk and the rest of the pattern, because then the space become an integral part of the pattern.

  • Pattern * test
    • will match please test
    • will not match retest test

StarVars

You can access the parts of a user message that match the variable portions of a pattern with starVar variables. These are numbered variables corresponding to each variable portion of the pattern — leading and trailing asterisks or sets of alternates.

The first variable portion is starVar1, the second is starVar2 and so on.

In result content, starVars can be accessed inside curly braces, using the botContext object: {$botContext.starVar1}

In Embedded Code, use the getBotVariable() function.

botContext.sendImmediateReply('You said: ' + botContext.getBotVariable('starVar1'));

Example with asterisks only

  • Pattern *test*
    • starVar1everything before “test”
    • starVar2everything after “test”
  • Message please test now
    • starVar1 == please (with a trailing space)
    • starVar2 == now (with a leading space)

Example with alternate sets

  • Pattern test (foo|fie|fee) (bar|baz|bat) end
    • starVar1(foo|fie|fee)
    • starVar2(bar|baz|bat)
  • Message test foo bar end
    • starVar1 == foo
    • starVar2 == bar
  • This pattern would not match:
    • test foobar end
    • please test foo bar end now

Example with nested alternates

  • Pattern test (foo(bar|baz)|fie)
    • starVar1(foo(bar|baz)|fie)
    • starVar2(bar|baz)
  • Message test foobar
    • starVar1 == foobar
    • starVar2 == bar
  • Message test fie
    • starVar1 == fie
    • starVar2 == undefined

Notice the last example. In a set of nested alternates, it is possible that one or more variables will not be defined, because they are inside an option that doesn’t match.

Complex Example

  • Pattern *test (foo(bar|baz)|fie(bat|bam)) now*
    • starVar1everything before “test”
    • starVar2(foo(bar|baz)|fie(bat|bam))
    • starVar3(bar|baz)
    • starVar4(bat|bam)
    • starVar5everything after “now”
  • Message please test foobar now, okay?
  • starVar1 == please
  • starVar2 == foobar
  • starVar3 == bar
  • starVar4 == undefined
  • starVar5 == , okay?

Intents

Pattern matching is great for some cases, but it can be limiting. To create a more natural interaction, you can use Intents.

An Intent is a model of a user’s meaning or intention.

For example, a user who wishes to begin an interaction might say hello or hior any number of other things. It would be nearly impossible to create a pattern that matches every possible user input.

With an Intent, you provide the platform with a number of example inputs that all mean essentially the same thing. The bot platform compares user inputs to example (or seed) sentences to determine if they match the intent.

So, for example, you might create an Intent and call it Greeting. Then give it the following seed sentences:

  • hi
  • hello
  • hey
  • hola

If a user sends the input howdy, that should match the intent and trigger whatever message you have set up for it.

Seed sentences can be longer and more complicated than this example. For example, the following seed sentences might be used for a broken product Intent:

  • it’s broken
  • the item I bought doesn’t work
  • your product is terrible
  • the thing isn’t working

Creating and Assigning Intents

Intents are created independently of ChatBots or Messages. This lets you build an intent and then use it in several different Bots.

To create an intent, start by switching over to the intents view, by clicking on the intent button button. Then Create Intent.

../_images/new-intent.png

Give your new intent an Intent Name and select the appropriate Domain. Setting the domain helps the platform understand your meaning.

Create a Clarifying Question. The bot will ask this question to the user if it thinks the intent is being triggered, but isn’t quite sure. For example, if you are creating an intent to model users complaining about bad customer service, your Clarifying Question might be It sounds like you had a bad customer service experience. Is that right? or Did you have a bad experience with customer service? If the user answers positively to your Clarifying Question, the intent will be triggered.

Add several Seed Sentences. These are examples of possible user inputs that all mean the same thing, that share your intent. So, for the bad customer service example, your Seed Sentences might be:

  • i don’t like you
  • i had a bad time at your store
  • store employee was rude to me
  • your service is terrible

The more Seed Sentences you add, and the more variety, the better the platform will be able to understand your intent and recognize it in user inputs. If you have records of actual customer interactions, these can be a great resource for Seed Sentences. Don’t be afraid to add slang or poor grammar, either.

../_images/intent-bad-service.png

Testing and Improving Intents

Once you have created an intent, its a good idea to test it. On the left side of the screen you can enter new sentences to see how well they match your intent. You should test both positives and negatives (sentences that should and should not match your intent).

../_images/intent-test.png

As your Bot interacts with users, it will collect all the sentences that triggered the intent. You can review these in Learned Sentences. The platform uses these Learned Sentences to improve its understanding of the intent — so it’s a good idea to review them periodically. You can approve or disapprove of learned sentences to help improve your Bot’s understanding.

Adding an Intent to a Bot Message

To add an intent as a trigger on a Message, simply select it when creating a message.

../_images/intent-on-message.png

You can only select one Intent per Message, and you can only assign an Intent to one Message per Bot. (You can, however, use an Intent in several different Bots.)

Using setTriggerNextMessage()

The last way to trigger a Bot Message is to use the setTriggerNextMessage() function.

You can use this in any of the Embedded Code panels. When placed in the Preprocess Code section of the default message, it can be used in place of Pattern Matching or Intents to respond to user inputs.