Scripts
The Scripts page in the admin panel allows users to create, manage, and configure scripts that define actions for agents within the system. Scripts can be written in Python or JavaScript and can interact with variables and other system components.
Helper Buttons
- Add Variable: While editing a script, you can click the "Add Variable" button to open a dialog for creating new variables.
- Lookup Names: Use this button to see available names for different components (variables, prompts, etc.) that can be used within your scripts.
Accessing the Scripts Page
To access the Scripts page:
- Log in to the admin panel.
- Ensure that an account is selected. If no account is selected, you will be prompted to select one.
- Navigate to the "Scripts" section in the sidebar.
Adding a New Script
Script Body Requirements
The Javascript code in the Script Body must adhere to the following rules:
- The function must be named
main
and accept one argument,pyvar
. The signature must bemain(pyvar)
. - The
pyvar
argument will be a JSON object containing all the engagement variables (vars). The keys of this object will match the names created in the Variables tab. - We use
pyvar
here as the argument name becausevar
is a reserved keyword in Javascript and cannot be used as a variable name.
To add a new script:
- Click on the "New Script" expander.
- Fill in the required fields in the "Add Script" form:
- Script Name: A unique name for the script.
- Active: Toggle to activate or deactivate the script.
- Agent(s): Select one or more agents that will use this script.
- Script Type: Choose the script language ("javascript" or "python").
- Body: Enter the script code in the selected language. You can include variables using the syntax
pyvar.variableName
for javascript and{var["variableName"]}
for python scripts. - Result Variable: Select a variable from the dropdown to store the script's response. This variable must have "script" in its scope.
- Click the "Create Script" button to save the new script.
Listing Scripts
The "Scripts" section displays a list of all scripts associated with the selected account and the currently selected agent. Each script entry shows:
- Script Name: The name of the script.
- Status: An icon indicating whether the script is active (✅) or inactive (❌).
- Agent(s): A comma-separated list of agents associated with the script.
Clicking on a script entry expands it to show:
- Type: The script language (Python or JavaScript).
- Result Variable: The variable used to store the script's response.
Editing a Script
To edit an existing script:
- Locate the script in the list and expand its details.
- Click the "Edit" button next to the script you want to modify.
- The "Edit Script" form will appear, pre-populated with the script's current details.
- Modify the fields as needed.
- Click "Save Changes" to update the script or "Cancel" to discard changes.
Deleting a Script
To delete a script:
- Locate the script in the list and expand its details.
- Click the "Delete" button next to the script you want to remove.
- Confirm the deletion when prompted. The script will be permanently removed from the system.
Example
Here is an example of a script that takes a phone_number
as input, normalizes it to E.164 format, and returns several formats of the number.
Variables used from the pyvar
json engagement for this script would be ["phone_number"]
.
function main(pyvar) {
if (
!pyvar ||
typeof pyvar.phone_number === "undefined" ||
pyvar.phone_number === null
) {
const received_keys = pyvar
? JSON.stringify(Object.keys(pyvar))
: "pyvar was null or undefined";
return {
error: "Input 'phone_number' was not found in the pyvar object.",
pyvar_keys_received: received_keys,
national_digits: "",
unique_id: "",
phone_number_e164: "",
};
}
let numberStr = pyvar.phone_number;
if (typeof numberStr !== "string") {
numberStr = String(numberStr);
}
let digits = numberStr.replace(/[^0-9]/g, "");
if (digits.startsWith("1") && digits.length === 11) {
} else if (digits.length === 10) {
let isLikelyUS = true;
const areaCode = parseInt(digits.substring(0, 3), 10);
if (areaCode >= 650 && areaCode <= 659 && areaCode !== 657) {
isLikelyUS = false;
}
if (isLikelyUS) {
digits = "1" + digits;
}
}
const unique_id = digits;
const phone_number_e164 = "+" + digits;
const national_digits =
digits.startsWith("1") && digits.length > 10 ? digits.substring(1) : digits;
return {
national_digits: national_digits,
unique_id: unique_id,
phone_number_e164: phone_number_e164,
};
}