JavaScript Module
Introduction
The JavaScript module allows you to define and execute arbitrary JavaScript code in the user’s browser. Ensure that the code does not contain any secrets, as it will be sent unencrypted.
Typical Use Cases
This module is useful for performing logical tasks using stored variables within the chat agent.
How to Use the Module
How to write variables from JavaScript
To write variables from JavaScript, define a function named evalFunction that returns an array of objects with key and value attributes.
Example:
function evalFunction() {
return [{"key": "somekey", "value": "testvalue"}];
}
This variable will not be set directly with the key somekey. The ID
of the process module will be appended to the given key as a safety measure.
So for example the variable define in the example above would have the key
somekey_e430ec70_3428_4616_a665_a2b4c68d3dec if the process module’s ID is
e430ec70-3428-4616-a665-a2b4c68d3dec. You can copy the ID directly when
configuring the module.
How to use LoyJoy data in the JavaScript module
In the script of the JavaScript module, you can use standard LoyJoy expressions / string templating to access variables or functions. For example:
const myVar = "${loyjoy_var}";
myFunction(myVar);
const data = {"a": ${num_var}, "b": "${other_var}"};
myOtherFunction(data);
This only works if the variables (loyjoy_var, num_var, and other_var) are defined in the process before the JavaScript module is executed.
Security Considerations
- Testing Restrictions: This module cannot be tested in the Manager or on Publish.
- Platform Support: The JavaScript module is not supported on LoyJoy sites due to security reasons.
- Caution: Use this module with caution due to potential security risks.