Skip to main content

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 on "Run a code snippet."run-a-code-snippet-2

Next, give your action a nickname. Within the code editing pane, you'll see our default template for Node.js 10.x. The details of the "event" object and possible response object formats are detailed below.run-a-code-snippet-editor

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 will contain the following data:

The callback() function is used to send data back to the bot. The argument should be an object with the following data:

Code snippets in bots must finish running within 20 seconds and only use 128 MB of memory. Exceeding either of these limits will result in an error.

Several popular Node.js libraries are available for use within the code snippet.

The libraries can be loaded using the normal require() function at the top of your code.