Skip to main content

JS code in a chatbot project

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

A bot can perform calculations, verify previously saved data and react depending on the results of the conversation. For example, a bot can store certain information in variables and re-use it during conversation. A bot can also check the communication channel and display corresponding graphics depending on the result.

JS code structure in a project

JS code in a project:

  • Expressions in if, else, elseiftags — condition functions used to define conditional jump rules.
  • script tag — script snippets used to perform calculations and calls to external systems.
  • init tag — initialization scripts; executed only once when the script is loaded and can be used to create global variables and bind pre/post processes.
  • Linked JS files — these files are linked to the script via the require tag. They contain objects and functions that can be used in any other script.
  • Built-in functions — special objects and functions provided by the platform. They can provide functions that can be used to invoke a matcher or external services.
  • Built-in libraries — JS libraries loaded automatically with each bot.
tip
Learn more on built-in variables, functions and services

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.

caution
Please note that JS files contain objects and functions that can be used in any other script.

.js files are linked at the start of the script via the require tag:

require: scripts/functions.js