Script widget examples


Contact center admins can add and edit the Script widget. This article contains various example configurations of the Script widget.

Important:

This article covers:

Prerequisites for customizing the Script widget

Example 1: Look up caller ID using the Script widget and global variables

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.

Prerequisites

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 NameCustom Variable Name
GeneralaccountNumber
GeneralaccountStatus
GeneralqueueName
SecretscrmApiKey

Flow layout

Script widget

Script widget contains the following script:

New version

async function main () {
  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; 
}

Old version

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.

Condition Widget

Condition widget settings

Condition widget exits

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

Send Media widget

Script widget debugging

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"}

 

Example 2: Collect an account number and store it in a local flow variable

Note: This section shows an example configuration and is not meant to be a complete configuration guide.

The following example shows how to collect the data entered by the caller in the Collect Input widget (Account Number). This example uses local flow variables instead of global variables. Local variables can be used if your flow will be accessing those variables in generated media or text-to-speech. However, if you want to use the variables in the Condition widget, it is better to use global variables s as shown in the previous example.

In this example, the Collect Input widget collects an account number from the consumer. The account number is stored in a variable by using the Script widget, then the Send Media widget plays back the account number to the consumer.

Flow layout

Script widget

The script can return a variable. That variable can be accessed by widgets using the value variable.

New version

async function main () {
  var result = var_get()["AccountNumber.Digits"]; // Get Account Number input
  log.info("Received Account Number " + result);
  
  // do something with account Number
  // add your business requirements here

  // create a new local variable called ‘account_status’ for this widget
  var_set("account_status”,”active”);  // this is just an example on how to set a local widget variable

  
  return result
}

Old version

const log = require('./utils/log');
var {_get:var_get , _set:var_set} = require('./utils/variable');
module.exports = async () => {
  var result = var_get()["AccountNumber.Digits"]; // Get Account Number input
  log.info("Received Account Number " + result);
  
  // do something with account Number
  // add your business requirements here

  // create a new local variable called ‘account_status’ for this widget
  var_set("account_status”,”active”);  // this is just an example on how to set a local widget variable

  
  return result
}

Send Media widget

The Send Media widget plays the output of the Script widget to the consumer. The widget has these settings:

Script variable mapping

Script Widget Name

Is Script return value?

Variable set by var_set

Variable in Flow Engine

ScriptAcctNum

No

account_status

ScriptAcctNum.account_status

ScriptAcctNum

Yes

N/A

ScriptAcctNum.value