Running code snippets in bots

When creating or editing a bot, you may add a webhook 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 will see our default template for Node.js 6.10. The details of the "event" object and possible response object formats are detailed below.run-a-code-snippet-editor

 

Once you've saved your action, the code will be triggered once the applicable part of the conversation has been reached.

There are three main items 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 contains details for the visitor and chat session.
  • The callback() function is used to pass data back to the bot and user and should be called in the exports.main function.
  •  Notes:
    • The context argument is currently unused.
    • The first argument of the callback() function is ignored, and should always be null.

The event object will contain the following data:

{
  "userMessage": {
  // Details for the last message sent to your bot
    "message": "100-500",
    // The last message received by your bot, sent by the visitor
    "quickReply": {
    // If the visitor selected any quick reply options, this will be a list of the selected options.
    // Will be 'null' if no options were selected.
      "quickReplies":[
      // A list of quick reply options selected by the visitor
         {
            "value":"100-500",
            "label":"100-500"
         }
      ],
  },
  "session": {
    "vid": 12345,
    // The contact VID of the visitor, if known.
    "properties": {
    // A list of properties collected by the bot in the current session.
      "CONTACT": {
        "firstname": {
          "value": "John",
          "syncedAt": 1534362540592
        },
        "email": {
          "value": "testing@domain.com",
          "syncedAt": 1534362541764
        },
        "lastname": {
          "value": "Smith",
          "syncedAt": 1534362540592
        }
      }
    }
  }
}

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

{
  "botMessage": "Thanks for checking out our website!",
  // This is the message your bot will display to the visitor.
"quickReplies": [
{
value:'option', // Passed to the bot as the response on click
label:'Option' // Gets displayed as the button label
}
], // the quickReplies object is optional "nextModuleNickname": "SuggestAwesomeProduct", // The nickname of the next module the bot should execute. If undefined, the bot will follow the default configured behavior "responseExpected": false // If true, the bot will display the returned botMessage, wait for a response, then execute this code snippet again with that new response. }

Available libraries

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

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

const request = require('request');

exports.main = (event, context, callback) => {
   
    request('http://time.jsontest.com/', function (error, response, body) {

        const responseJson = {
            "botMessage": "The current time in GMT is " + JSON.parse(body).time,
            "responseExpected": false
        }

        callback(null, responseJson);
      
    });
  
};