Instructions and for running your own code snippets in a bot action.
When creating or editing a bot, you can add a code snippet by clicking the ”+” to add an action as you normally would. From the action selection panel, click Run a code snippet.
Next, give your action a nickname and description. Within the code editing pane, you’ll see the default template for Node.js 10.x. The details of the “event” object and possible response object formats are detailed below.
The code will be triggered when the saved action is reached in a conversation.There are three main things to keep in mind when working with code snippets:
The exports.main() function is called when the code snippet action is executed.
The event argument is an object containing details for the visitor and chat session.
The callback() function is used to pass data back to the bot and user. It should be called in the exports.main function.
The event object contains the following properties:
Property
Type
Description
userMessage
Object
An object that contains two fields:
message: A string that represents the last message sent to your bot.
quickReply: A nested object that contains a quickReplies array if the visitor selected any quick reply options. Each entry in the array contains an object with two fields:
value: The actual value of the quick reply (e.g., "yes_option")
label: The text that appears on the quick reply button (e.g., “Yes”).
.
session
Object
An object that contains the following fields:
vid: The contact VID of the visitor, expressed as a number, if known.
properties: A list of properties collected by the bot in the current session, such as the firstname and lastname of the contact.
customState
Object
Only present if customState was passed in from a previous callback payload.
The code block provides an example event object for a contact who was chatting with a bot.
Code snippets in bots are subject to the following limits:
For snippets created prior to August 1st, 2025, code snippets must finish running within 20 seconds. Any snippets created after August 1st, 2025 must finish running within 45 seconds.
Code snippets can only use up to 128 MB of memory.
Exceeding either of these limits above will result in an error.