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:
1
say Question(
2
"What is 2+2?",
3
buttons=[Button(4) as ok, Button(5) as notok]
4
)
5
hold
6
​
7
if (event.match(ok)) say "That's correct!"
8
else say "Wrong answer. The right answer was {{ok.title}}"
Copied!
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:
1
say Question(
2
"What is your favorite color",
3
buttons=[
4
Button("red") as btnred,
5
Button("blue") as btnblue,
6
]
7
)
8
hold
9
​
10
// this will be true if the user clicked
11
// on one of the buttons, or typed "red" or "blue"
12
if (event.match(btnred, btnblue)) remember favcolor = event
13
else say "{{event}} is not a valid answer"
14
​
Copied!
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:
1
say Question(
2
"What is your favorite color",
3
buttons=[
4
Button("red", accepts=["Red", "Dark orange"], payload="COLOR_RED") as btnred,
5
Button("blue", accepts=["Blue", "sky", "navy"], payload="COLOR_BLUE") as btnblue,
6
]
7
)
8
hold
9
​
10
// for a valid choice, the user can either click on the button or
11
// manually type any of the accepted options for each button
12
do matched = event.match(btnred, btnblue)
13
​
14
// matched will contain the matched button object or Null
15
​
16
// in case it contains the button object
17
// we can prefer to remember its manually set payload
18
// instead of the button's title
19
if (matched) remember favcolor = matched.payload
20
​
21
// or continue the same way as before
22
else say "{{event}} is not a valid answer"
Copied!

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:
1
say Question(
2
"What is your favorite color",
3
buttons=[
4
Button("red", accepts=["Red", "Dark orange"]),
5
Button("blue", accepts=["Blue", "sky", "navy"]),
6
] as buttons
7
)
8
hold
9
​
10
// with match_array(), you don't have to list all options
11
do matched = event.match_array(buttons)
12
​
13
if (matched) remember favcolor = matched.title
14
else say "{{event}} is not a valid answer"
Copied!

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".
1
say "Type something below"
2
hold
3
say event.get_type() // "text"
Copied!

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.
1
// Example event object:
2
{
3
"content_type": "text",
4
"content": {
5
// a valid text event will always have a content.text property
6
"text": "Hi there",
7
// additional properties can be added
8
"more_data": "some extra data",
9
// extra properties of an event can be of any type.
10
"some_number": 42,
11
"an_object": { "wow": 1.23456, "impressive": true }
12
}
13
}
14
​
15
say "text: {{event.text}}" // Hi there
16
say "more_data: {{event.more_data}}" // some extra data
17
say "doesnotexist: {{event.doesnotexist}}" // Null
Copied!
​
Last modified 8mo ago