Widget functions
Define functions that run steps when a widget loads or a customer taps or types: set states, call actions, send messages and open links.
Working draft. These docs are illustrative: names, endpoints and examples describe the intended behaviour and can change before launch.
A function is a named list of steps. It runs when something happens in the widget: the widget appears, a button is tapped or an input changes. Use functions to load data, react to choices and hand the customer's input to the agent or an action.
Define and attach a function
Functions live under functions. Components refer to them by name through the event properties onClick and onChange. onLoad at the top level runs when the widget first appears.
{
"onLoad": "loadSlots",
"functions": {
"loadSlots": [
{ "step": "setState", "key": "status", "value": "loading" },
{ "step": "callAction", "action": "act_list_slots", "args": { "date": "{{context.date}}" }, "saveTo": "slots" },
{ "step": "setState", "key": "status", "value": "ready" }
],
"pickSlot": [
{ "step": "setState", "key": "selectedSlot", "value": "{{event.value}}" },
{ "step": "sendMessage", "text": "I would like the {{event.value}} slot." }
]
},
"components": [
{ "type": "button", "label": "Pick 10:00", "onClick": "pickSlot", "value": "10:00" }
]
}
Steps run in order. If a step fails, the remaining steps are skipped and the function stops.
Steps
| Step | Properties | What it does |
|---|---|---|
setState |
key, value |
Sets a state. The value can use bindings. |
callAction |
action, args, saveTo |
Runs one of the agent's actions, such as a Custom action, and stores the result in the state named by saveTo. |
sendMessage |
text |
Sends a message into the conversation as the customer. The agent answers it. |
openUrl |
url |
Opens a link in a new tab. |
if |
when, then, else |
Runs the then steps when the binding in when is true, and the else steps otherwise. |
The action value is the action id, such as act_list_slots. Find it in the action's settings under Actions. See Custom action for how to build an action that returns data.
Events and bindings
| Event | Fires when | event.value holds |
|---|---|---|
onLoad |
The widget first appears. | Nothing. |
onClick |
A button or option is tapped. | The component's value, if it has one. |
onChange |
A form input changes. | The new input value. |
Handle failures
Use an if step after a callAction to show something helpful when the data is empty.
{
"step": "if",
"when": "{{states.slots}}",
"then": [{ "step": "setState", "key": "status", "value": "ready" }],
"else": [{ "step": "setState", "key": "status", "value": "error" }]
}
Tip: Name functions after what they do, such as
loadSlotsorpickSlot. The names show up in error messages in the code editor.
Related pages
- States explains the values functions change.
- Interactive components lists the components that trigger functions.
- Click tracking measures taps without writing a function.