Skip to main content

JavaScript in a script

tip
JAICP DSL uses JavaScript code snippets (ECMAScript 5 revision) to describe business logic and integration with external systems.

Here are a few examples of how JS code can be used in projects:

  • The bot can use variables to store certain information, such as client names, to be re-used during conversation.
  • The bot can make calculations, check user information saved previously and respond depending on the result. For example, the bot can detect the channel used for the communication and display the UI depending on that channel.

JS code structure in a project

JS code in a project:

ObjectObject
if
else
elseif
Conditional functions used to describe the rules of conditional transitions.
scriptScript inserts for making any calculations and calling external systems.
initInitialization scripts are executed only once at script load and are used to create global variables and bind pre/post processes.
Linked JS filesFiles are connected via require. Contains objects and functions available for use in any other scripts.
Built-in variablesVariables provided by the platform that can be used in a bot script.
Built-in functionsSpecial objects and functions provided by the platform. For example, they provide feature to call a match or external services.
Built-in servicesJS-libraries that are automatically loaded with each bot.

JS files

You can define your logic directly in the .sc script file. But we recommend moving all your JS code to a separate file. You will need to create a file with the .js extension in the src folder, e.g. functions.js. Add functions to it that can later be called from the script file after the script tag.

Please note that JS files contain objects and functions that can be used in any other script. Files are linked at the start of the script via the require tag:

require: scripts/functions.js

Global scope

The global runtime context can be expressly accessed by the $global similar to the window.document entity in a browser. The global runtime context stores all the loaded functions and variables directly declared in JS files.

The $global object can only be modified at the bot initialization stage when JS files are loaded and init blocks are executed. The $global object cannot be modified after the initialization is over.