Tutorial: Setting context variables for actions from the web chat
This tutorial shows how you can use the web chat to set the values of context variables that your actions can access.
For a complete, working version of the example described in this tutorial, see Setting context variables for watsonx Assistant web chat.
You can use context variables to store information that the assistant uses throughout the conversation.
Context data is maintained throughout the session. The context is sent to the assistant as part of each message, and returned with each response, in an object called context
. Context variables for actions skills are stored in the
following location:
"context": {
"skills": {
"action skill": {
"skill_variables": {
...
}
}
}
}
Any JSON data can be stored in the skill_variables
object and read or modified by either the actions or the web chat.
This example shows how you can store the customer's name in a context variable, which you can then use to show a personalized greeting. You can use this same approach to store any other information that your assistant might use. Examples might include the customer's location, an account balance, or stored preferences.
To set the value of a context variable from the web chat, follow these steps:
-
Create a handler for the
pre:send
event. This handler modifies the payload of outgoing message events to assign a value to a context variable calledUser_Name
.We want to set the user name only once at the beginning of the conversation. This example assumes that the home screen is not enabled, which means that the web chat initiates each new conversation with an empty message to trigger a greeting from the assistant.
For this example, we are using the hardcoded user name
Cade
. In a production assistant, you might retrieve the customer's name from a user profile on your website.function preSendHandler(event) { // Only do this on messages that request the welcome message. if (event.data.input && event.data.input.text === '') { event.data.context.skills['actions skill'] = event.data.context.skills['actions skill'] || {}; event.data.context.skills['actions skill'].skill_variables = event.data.context.skills['actions skill'].skill_variables || {}; event.data.context.skills['actions skill'].skill_variables.User_Name = 'Cade'; } }
-
In your
onLoad
event handler, use theon()
instance method to subscribe to thepre:send
event, registering thepreSendHandler()
function as the callback.instance.on({ type: 'pre:send', handler: preSendHandler });
The assistant actions can now access the User_Name
variable throughout the conversation.
For complete working code, see the Setting context variables for watsonx Assistant web chat example.