Virtual Agent action scripts
Summarize
Summary of Virtual Agent action scripts
Virtual Agent action scripts in ServiceNow enable integration of chat capabilities with external providers (e.g., Twilio) by managing user data exchange, message transformation, and conversational flow control. These scripts are essential for passing user data to Virtual Agent Conversational Services (VACS), applying necessary transformations, and returning responses to chat interfaces. Workflow Studio is used to build and customize these scripts to handle inbound/outbound message processing and provider-specific protocols.
Show less
Key Components and Their Functions
- Provider Attribute Script: Extracts data from incoming messages, including user ID, content, authentication token, and context variables. It supports contextual actions (e.g., ENDCONVERSATION) and ensures message content appears in transcripts.
- Sender Subflow: Handles sending messages asynchronously to chat providers, managing message payloads and updating message displays based on user selections. It prevents blocking Virtual Agent processing during external communication.
- Contextual Action Script: Executes special keyword actions such as starting or ending conversations or transferring to a live agent based on detected contextual commands.
- Link Account Action Script: Manages user account linking by handling identifiers and status feedback between the chat user and ServiceNow user accounts.
- Response Processor Action Script: Performs additional platform actions based on responses from message send requests, including logging message status and response details.
- Outbound Transformer Scripts: Convert Virtual Agent’s outgoing rich control messages into provider-specific formats suitable for chat interfaces, such as SMS or Teams. Examples include text, picker options, links, and card formatting.
- Inbound Transformer Scripts: Convert incoming messages from chat providers into a format recognized by Virtual Agent, extracting user responses and handling unexpected inputs with support for natural language understanding (NLU).
Required vs Optional Scripts
| Script Name | Purpose | Required |
|---|---|---|
| Provider Attribute | Extracts user data and context from incoming messages | Yes |
| Sender Action | Sends messages asynchronously to providers | Yes |
| Outbound Transformer | Transforms outgoing messages to provider format | Yes |
| Inbound Transformer | Transforms incoming messages to Virtual Agent format | Yes |
| Contextual Action | Handles special keyword-based conversation actions | No |
| Link Account Action | Manages user account linking feedback | No |
| Response Processor | Handles additional response-based platform actions | No |
Practical Considerations for ServiceNow Customers
- Customization: Use Workflow Studio to customize these scripts to fit your chat provider’s protocol and your organization’s conversation needs.
- Contextual Actions: Ensure your provider attribute script includes proper context variables and typed values to enable features like ending conversations or switching to live agents.
- Message Handling: The sender subflow is optimized for asynchronous message sending, improving Virtual Agent responsiveness.
- Transformations: Outbound and inbound transformers enable seamless communication by translating Virtual Agent message formats to and from provider-specific protocols.
- Error Handling and Logging: Response processor scripts allow you to track message delivery status and debug issues with external chat providers.
Expected Outcomes
Implementing these action scripts ensures that your Virtual Agent can effectively integrate with external chat providers, correctly process user inputs, manage conversation flow based on context, and maintain accurate message status tracking. This leads to improved conversational experiences, reliable message delivery, and smoother handoffs to live agents or account linking workflows when needed.
You need to write a set of mandatory Workflow Studio action and subflow scripts to pass the user data to VACS, apply transformations and send the response back to your chat interface endpoint.
Provider and inbound/outbound scripts
A provider, such as Twilio, provides the chat capability. User action scripts perform provider actions, as well as conversational custom chat integration configuration for inbound and outbound transformer actions for your chatbot or custom conversations. Examples of each script are provided. They include a description of what each script does, as well as the action input and action output. You also see examples of the script JSON.
You use Workflow Studio to build these scripts to transform server and client messages. The provider, sender, and inbound/outbound transformer scripts are required. You can use the additional scripts to add more functionality to your chat.
| Script name | Purpose | Required (Y/N) |
|---|---|---|
| Provider attribute | Contains a Provider Auth token, a user identifier, user input, and context variables. | Y |
| Sender action | Bundles the request, and sends a response asynchronously via Workflow Studio or Integration Hub. |
Y |
| Response processor | Performs platform actions, such as an update message status, that's based on a response received for an outbound message. | N |
| Contextual action | Supports contextual actions. | N |
| Link account action | Provides a callback about whether account linking was successful or not. | N |
| Outbound transformer | Transforms a chat server's supported control to chat interface-specific rendering. | Y |
| Inbound transformer | Optional. You can use the default rich controls if they apply to your chat provider. You do not need to provide a separate inbound transform for each input control. If a default behavior does not work, you can override the default with your own inbound transform for a specific Virtual Agent server control. | Y |
Provider attributes action script
| Input | Output |
|---|---|
|
|
Example: Provider attributes action script.
(function execute(inputs, outputs) {
var headers = (inputs.headers);
var payload = (inputs.payload);
// check if an authentication token was included. this is optional.
var smsUtil = new VASMSTwilioUtil();
outputs.token = JSON.stringify(smsUtil.getToken(headers, payload));
var data = payload.data;
var request_context = {};
// inspect the actual message. it could be an MMS (attachment) or raw text
if (data['MediaUrl0']) {
var attachment_value = {};
attachment_value.url = data['MediaUrl0'];
attachment_value.content_type = data['MediaContentType0'];
attachment_value.name = smsUtil.getFileName(attachment_value.url , attachment_value.content_type);
request_context.attachment_value = attachment_value;
} else {
request_context.typed_value = data['Body'];
}
// set the mandatory outputs
outputs.request_context = request_context;
outputs.provider_user_id = data.From;
})(inputs, outputs);
Sender subflow
Instead of a script, the sender subflow handles the process of sending a message to a provider. The message is contained in a payload string as part of the subflow. The sender has a service_url and origin_service_url, while the outbound URL varies depending on the provider. For example, a message sent through MS Teams uses the value tenant_id. When the Virtual Agent sends the message containing options, the recipient selects an option. The subflow updates the message display based on their choice and removes the options. The conversation continues based on the user’s selected option. The Virtual Agent is freed to continue processing messages rather than trying to send external requests. When creating a subflow do not have an action call an action within the subflow. For more information on subflows, see Subflows and actions.
Contextual action script
The contextual action script performs special keyword actions such as typing agent while inside a Virtual Agent topic to get immediately handed off to a live agent.
| Input | Output |
|---|---|
|
None |
Example: Contextual action script.
(function execute(inputs, outputs) {
var contextual_action = inputs.request_context.contextual_action;
if (contextual_action === "END_CONVERSATION") {
sn_cs.VASystemObject.endConversation(inputs.conversation_id);
} else if (contextual_action === "AGENT") {
sn_cs.VASystemObject.switchToLiveAgent(inputs.conversation_id);
} else if (contextual_action === "START_CONVERSATION") {
sn_cs.VASystemObject.startConversation(inputs.conversation_id);
}
})(inputs, outputs);
Link account action script
The link account action script contains identifiers for the user who is linking. The script also contains the account that it is linked to.
| Input | Output |
|---|---|
|
None |
Example: Link account action script.
(function execute(inputs, outputs) {
var richControl = (inputs.rich_control);
var value = richControl.value;
outputs.result = richControl.header + ': ' + value.action;
})(inputs, outputs);
Response processor action script
The response processor action script performs specialty actions that are based on the response to a send message request. The framework already does basic message status tracking.
| Input | Output |
|---|---|
|
None |
Example: Response processor action script.
(function execute(inputs, outputs) {
gs.debug("Response from provider: message_id = " + inputs.message_id + ", status_code = " +
inputs.status_code + ", headers = " + JSON.stringify(inputs.headers) + ", body = " + inputs.body);
})(inputs, outputs);
Outbound transformer scripts
The outbound transformer scripts convert outgoing Virtual Agent messages to a protocol that is related to the conversational custom chat integration.
Input.
- rich_control (JSON) - Virtual Agent server rich control component that must be transformed.
- payload (JSON) - Last response that was received from a requester.
Output.
result (String) - Provider-appropriate component message to send.
| Outbound transformer name | Script example |
|---|---|
| Virtual Agent - SMS Twilio Adapter Default Text Outbound Transformer | |
| Virtual Agent - SMS Twilio Adapter Default Link Outbound Transformer | |
| Virtual Agent - SMS Twilio Adapter Default Picker Outbound Transformer | |
| Virtual Agent - SMS Twilio Adapter Default Multi Link Outbound Transformer | |
| Virtual Agent - SMS Twilio Adapter Default Card Outbound Transformer | |
Inbound transformer scripts
The inbound transformer scripts convert incoming conversational custom chat integration messages to a protocol that the Virtual Agent recognizes. It determines the required value to be injected into the rich control that is presented to the user.
Input.
- request_context (JSON) - The request passed from the Provider Attribute script.
- rich_control (JSON) - The last rich control sent to the requester that contains the original "question".
Output.
- value - sys_id or text value.
- search_text - Text that is used if the requester selected something that was unexpected. For example, a user typed something that the NLU could assist with, such as, "None of these options match what I want" in response to a topic picker component.
| Inbound transformer name | Script example |
|---|---|
| Virtual Agent - SMS Twilio
Adapter
Default Text Inbound Transformer Purpose: Find the text response to the InputText question posed |
|
| Virtual Agent - SMS Twilio
Adapter
Default Picker Inbound Transformer Purpose: Find the sys_id of the option that the user selected for this picker question. This script uses the same algorithm of the outbound transformer. If no options are selected, and the user typed something instead, then the "search_text" is set and fed into NLU if it is enabled on this instance. |
|