Some bots do a lot of computing via JavaScript. For optimal performance, follow these requirements and best practices.

Requirements

  • Custom JavaScript code should be compatible with ES5, i.e., ECMAScript 5 (2009). And there is an exception: The toLocaleString method isn't supported.
  • Custom JavaScript code must complete within 5 seconds. Otherwise, it times out, and the execution flow continues on as per the order of operations. In general, the code shouldn’t do heavy calculations and should be fast.

There’s no limit imposed by LivePerson Conversation Builder as to the size of your methods, but Java itself might impose one. If you encounter an error that your method code is too large, split the method into smaller ones.

Things to do

  • Catch issues early in bot development by inserting debug statements into the code to verify things work as you expect.
  • Use loops wisely. Avoid time-consuming external API calls from within loops, as the call might time out before the loop finishes.
  • Use try-catch blocks inside interactions that have code with methods that can throw exceptions that need to be caught. Log a custom event to save the error.

    Example try-catch block in Pre-Process Code of an interaction

  • Expect that tokens will expire and implement proper checks for this. It’s common to initialize a token within the bot’s Global Functions and use it in API calls elsewhere within the bot. If the API call fails, fetch the token again within the same global variables.
  • Take advantage of the Global Helper Functions dialog template and bot template provided by LivePerson. The bot template is for educational purposes, while the dialog template can be imported into your bot to “add in” the global helper functions that are included.

    The global helper functions are designed to simplify the use of custom code. They’re recommended and useful for bots that have logic implemented via custom code in interactions or integrations. Use the helper functions in your bot’s global functions to capture needed info and process common methods without having to write botContext methods.

  • When setting a bunch of variables in the Conversation Context Service (so you can use them to guide the bot flow), make a single API call submitting an object of key/value pairs. See our example.
  • When writing code that transfers to a skill, check for a null value for that skill. This helps to ensure a successful transfer.
  • When writing code that “wakes up” an idle bot (for example, with “hi”), take the bot’s configured session length into consideration. If the bot is awakened after the session has expired, there won’t be any available conversational context. So, the flow that follows will not be as desired.

Things to avoid

  • Avoid adding complex JavaScript in a single spot. Instead, practice component-based development: Try to componentize the code, breaking it into identifiable and reusable pieces that are easier to use and debug.
  • Avoid too many context or third-party API calls in one JavaScript layer. Instead, split the calls into multiple interactions if possible.

Handling exceptions and errors

  • Catch and handle exceptions and errors gracefully. And use the logCustomEvent scripting function to log the error.
  • Handle timeouts and recover from them gracefully. Timeouts can happen due to latency in calls to external APIs. One way to recover the flow is to put in place checks within the JavaScript or in the next interaction. Handle the issue gracefully, retry, or bring the conversation context back to the prior interaction, i.e., return to an earlier point in the flow.