Contact center admins can add and edit the Script widget. This article contains example configurations using the old syntax of the Script widget.
The following example shows how to use a Script widget to place an REST API call using Axios. The values from the API response will be returned and then later accessed within the Flow.
In this example, we will query a REST API for the Caller’s phone number. If the caller’s phone number is found in the external database, the API response will return account number information about the caller. The flow will then take the API response and check whether a match was found in the external database. If a match was found, the flow will play back the account number for the caller and then send the caller to a queue.
Define the custom global variables shown below. These variables will be populated in the script based on the response from an API query.
Custom Variable Group Name |
Custom Variable Name |
General |
accountNumber |
General |
accountStatus |
General |
queueName |
Secrets |
crmApiKey |
Script widget contains the following script:
const log = require('./utils/log'); const req = require('axios'); var {_get:var_get , _set:var_set, _setGlobalVariable: global_var_set} = require('./utils/variable'); module.exports = async () => { let caller_number = var_get()["Start.From"]; let callee_number = var_get()["Start.To"]; log.info("Call from " + caller_number + " to " + callee_number); try { const result = await req.get('https://crm.example.com', { headers: {'Authorization': var_get()["global_custom.Secrets.crmApiKey"]}, params: { ani: caller_number } }); let jsonResponseBody = result.data; // Store the API response log.debug("Axios jsonResponseBody=" + JSON.stringify(jsonResponseBody)); // Set Global Variables global_var_set("global_custom.General.accountNumber",jsonResponseBody.account_number); global_var_set("global_custom.General.accountStatus",jsonResponseBody.account_status); global_var_set("global_system.Consumer.email",jsonResponseBody.email_address); global_var_set("global_system.Consumer.firstName",jsonResponseBody.first_name); global_var_set("global_system.Consumer.lastName",jsonResponseBody.last_name); } catch (error) { log.debug("Received error " + error); } return; }
Note: If your API may occasionally omit expected fields from the API response body, you should perform error checking within your script to prevent setting an undefined/null value to a global variable.
Exit 1: This exit is used to determine if we have matched on an account number. In the above example, the API needs to return {“account_status”:”active”} for the flow to match on the caller ID.
No Match: Select Route_to_Queue.
The Input Parameter section of the Script widget can be used for debugging. Within the Input Parameter box, enter JSON encoded variables that are required as input for the script. If you are accessing any global variables within the script, you will need to populate them here for debugging purposes.
Enter the following in the Input Parameter panel to test the above script:
{ "Start.From":"+16505551234", "global_custom.Secrets.crmApiKey":"this_is_the_api_secret" } Example output in the Result section: Wed Jul 27 2022 15:22:12 GMT-0700 (Pacific Daylight Time) [INFO] Call from +16505551234 to undefined Wed Jul 27 2022 15:22:13 GMT-0700 (Pacific Daylight Time) [DEBUG] Axios jsonResponseBody={"account_number":"99999","account_status":"active","email_address":"john@example.com","first_name":"John","last_name":"Doe"}
Important: