The Event
The event
keyword is a special variable in CSML that contains whatever the user did last. It can be generally be used as a simple string, but it is in fact a much more complex object that contains a lot of information about what the user did.
In its essence, event
contains the full message payload as received by the bot. There is a type and a content. The content is an object that contains a text, a payload, a url, other objects... it can be anything, and in general, its content can be derived from the events type. However, using it as "{{event}}"
in a string (or simply in say event
without any curly brace and double quote) will behave as a string.
Event methods
event
contains a number of useful methods that can be used to perform various operations.
event.match()
In many cases we want to find whether the user's input matches some value to perform conditional logic. For example, in the following example, we want to make sure that the user clicked on the right answer:
In some cases, you may also want to make sure that the user clicked on a button or typed any accepted response, in which case you can add several parameters to the .match() method:
The result of event.match(...)
is whatever was matched, or Null
. So the above example, if using the language to its full potential, could also be used like this for even more control over what value is remembered:
event.match_array()
Similar to event.match()
, but takes an array as argument. It also returns the matched value, if any. In many places, it is easier to use as listing all the individual items of the array. The same example as above would now read:
event.get_type()
Returns the type of event. For example, if the user typed something, event.get_type()
will be "text"
. Or if the user clicked on a button (which yields a payload
the type of event is "payload"
.
Accessing event content values
To access any value in the content body of the event, simply use the dot notation with its expected property name. If it does not exist, this will return Null
.
Last updated