ServiceNow and Twilio IVR: Integrated for More Responsive IT Management

November 01, 2022
Written by
Arun Wagle
Twilion
Reviewed by
Paul Kamp
Twilion

Build more efficient ServiceNow workflows with faster response times with Twilio Event Streams and IVR solutions, ServiceNow, and Amazon Kinesis.

According to Gartner, the average cost of IT downtime, in terms of lost business and sales, is $5,600 per minute. In this post, I want to demonstrate how Twilio can enable customers to minimize their exposure to these costly outages and manage their digital and IT operations more efficiently by integrating with leading digital workflow platforms, such as ServiceNow.

  • Twilio is a next generation fully programmable customer engagement platform.
  • ServiceNow IT Service Management (ITSM) is a cloud-based platform designed to improve IT services, increase user satisfaction, and boost IT flexibility and agility. ServiceNow ITSM is a de facto standard for building digital workflows for IT operations using ITSM.
  • With Twilio Event Streams and AWS Kinesis, it is straightforward to build a fully event driven incident management platform to automate more processes and improve the customer experiences.

In the following example I will show how to integrate Twilio Event Streams to capture events from a Twilio IVR and ingest them into Amazon Kinesis in real-time.

High Level Architecture: Event Streams, IVRs, ServiceNow, and Kinesis

Architecture diagram for ServiceNow and Twilio IVR integration

When a new incident is recorded in ServiceNow, an automated IVR workflow will be triggered based on the high priority incident business rules created in ServiceNow. The programmable IVR flow (built with Twilio) will try to find and call an agent who can accept the incident, reject it so that the next available agent can be called, and so on. The agent has the ability to escalate to the appropriate resources until either someone acknowledges the incident by accepting the call or exits the flow. At the end of the flow, the incident ticket is updated with the appropriate comments.

The application is targeted to improve the incident management workflows for internal IT operations employees and the line of businesses within the company.

Find Agent Or Escalate Flow

Let's look at a typical workflow of an incident management IVR system.

Typical workflow of an incident management IVR system

ServiceNow flow that will trigger the call from Twilio Studio. As the call progresses the call events are sent from EventStream to AWS Kinesis stream

IVR Flow

The Twilio Studio IVR flow is triggered from 2 places

  1. When the ServiceNow incident is created from the developer instance.
  2. When the Twilio Flow ends, an event is created in the sink created in Twilio Event Streams (backed by AWS Kinesis)

IVR Flow States

 

Action

IVR Flow 

Description

1

A new ServiceNow incident is created

First available agent receives

Call. 

User is presented with option

  1. To accept the ticket
  2. To transfer to the next available agent.
  3. To escalate
 

2

Agent selects 1

Updates ServiceNow ticket and ends the flow.

 

3

Agent selects 2

a. Current execution flow ends. 

b. An event is generated

c. Triggers Flow again with information of the next available manager.

Continues with same flow as #1

4

Agent Selects 3

a. Current execution flow ends. 

b. An event is generated

Triggers Flow again with information of the escalation manager.

Continues with same flow as #1

5

No Agents available

Executes #2 with comments

 

IVR Flow Diagram

IVR Flow for ServiceNow Integration

Demo Setup

Before you can get started on the solution, you will need to set up – or log into – a few accounts.

Prerequisites

Setup Steps

Also before you begin, the tutorial is more straightforward if you install a few tools locally. You’ll want to set up local GitHub, the Twilio CLI, a ServiceNow Developer Instance, and the AWS CLI.

If you don’t have one (or more than one!) of those set up, here are the dedicated instructions:

  1. Install Local Github
  2. Insert Twilio CLI
  3. Get a ServiceNow Developer Instance
  4. Install AWS CLI

Awesome, you’re now ready to begin.

Let's Get Started

A complete setup is detailed in the Readme file in the Readme in the repo.

Get the code from Github

First, you’ll want to clone the code locally so you can work on it. From a command line, run the following commands:

git clone https://github.com/arunwagle/twilio-servicenow-aws-findagent-or-escalate-ivr.git
cd twilio-servicenow-aws-findagent-or-escalate-ivr

Setup AWS components

Create IAM Policies

Before the rest of the AWS work, you’ll need to set up Users and Groups in IAM. We’ll start work in the AWS console.

First, create a user:

  • Go to AWS Console > IAM > Users > Create User
  • Select the Security Credentials tab and create an access key and secret. Note this down, as it will be required for local AWS configuration.

Now, set up a group:

  • Go to AWS Console > IAM > User Groups > Create Group
    • Provide name = TwilioGroup
    • Add the above created user to this group
    • Add following permissions to the group
      • IAMFullAccess
      • AmazonKinesisFullAccess
      • AWSLambda_FullAccess

After the group is set up, set up your credentials locally using AWS Configure.

And finally, create a Lambda execution role:

  • Go to AWS Console > IAM > Roles > Create Role
    • Provide name = TwilioLambdaRole
    • Select AWS Service > radio button Use Case Lambda
    • Add AWSLambdaKinesisExecutionRole
    • Create Role

Deploy the AWS Lambda Function

Now we’re ready to deploy the Lambda function. I’ll walk you through the steps here.

Locally, on your command line, run the following command:

zip -r demo-ivr-aws-lambda-fns.zip

Now, create a function. You need to copy the ARN from TwilioLambdaRole in the above step.

aws lambda create-function
--function-name "findagent-or-escalate-ivr-fn"
--runtime "nodejs16.x"
--role "[Your AWSLambda Role ARN]"
--zip-file fileb://demo-ivr-aws-lambda-fns.zip
--handler "index.handler"
--environment "Variables={TWILIO_SID=[Your Twilio SID],TWILIO_AUTH_TOKEN=[Your Twilio Token],TWILIO_FUNCTION_URL=https://findagent-or-escalate-ivr-service-xxxx.twil.io/execute}"

Next, create a Kinesis trigger.

aws lambda create-event-source-mapping --function-name findagent-or-escalate-ivr-fn
--batch-size 1 --starting-position LATEST
--event-source-arn [ARN of the kinesis sink]

Now, optionally, set up a concurrency level. I highly suggest you do this – it’s best practice to set this for a demo.

aws lambda put-function-concurrency --function-name findagent-or-escalate-ivr-fn --reserved-concurrent-executions 1

Excellent! Now you can move onto the Twilio components.

Setup Twilio components

On the Twilio side, we are going to set up a Serverless function, a Studio Flow, and Event Streams.

But first, we need a phone number! If you haven’t got one yet, follow this article. It will be available in our Studio Flow below as the variable {{flow.channel.address}}.

Deploy a serverless Twilio Function

To get to a Function, we’ll start in the Twilio Console.

  1. Click Functions and Assets > Services > Create Service
  2. Provide a service name = findagent-or-escalate-ivr-service
  3. Click Add > Add Asset
    • Provide a name = agent_list.json. Change the scope to public
    • Copy the contents of twilio-servicenow-aws-findagent-or-escalate-ivr/assets/agent_list.json to the editor
    • Modify the phone numbers to use for agent and escalation list to match the test phone numbers to be used for the demo.
    • Save
  4. Click Add > Add Function
    • Provide function name = /execute. Change the function scope to public
    • Copy the contents of twilio-servicenow-aws-findagent-or-escalate-ivr/demo-ivr-twilio-fns/execute.js to the editor.
    • Modify AGENT_LIST_URL in execute.js to point to the url of the asset deployed above
    • Save
  5. Click Add > Add Function
    • Provide function name = /update_incident. Change function scope to public
    • Copy the contents of twilio-servicenow-aws-findagent-or-escalate-ivr/demo-ivr-twilio-fns/update_incident.js to the editor
    • Modify AGENT_LIST_URL in execute.js to to point to the url of the asset deployed above
    • Save
  6. Click Settings > Dependencies
    • Add axios dependencies
  7. Click Settings > Environment variables. Set the following variables:
    • SN_API_TABLE_ROOT - Your ServiceNow developer instance "Table" url https://[URL].service-now.com/api/now/table/
    • SN_USER - ServiceNow username(twilio_demo_user)
    • SN_PASSWORD - Password of the above user.
    • FROM - Phone number provisioned in Step2 above
    • FLOW_SID - Flow Sid. This is available from the Studio > Flows
  8. Deploy All

Create a Studio Flow

Now, let’s move onto the Studio flow. You’ll be able to import most of this step – I’ll walk you through it. Again, we’ll start in the Twilio Console.

  1. Click Studio > + to create a new flow.
  2. Provide a flow name = Find Agent or Escalate IVR
  3. Select the Import from JSON template > Next > Copy the json from twilio-servicenow-aws-findagent-or-escalate-ivr/demo-twilio-studio-flows/findagent-or-escalate-ivr.json
  4. The Studio Flow is created!

Find an agent or escalate IVR diagram

Set up Event Streams

For Event Streams, we’ll be moving back to the console – and we have a dedicated post. All the code is available in twilio-servicenow-aws-findagent-or-escalate-ivr/demo-twilio-aws-kinesis/*.

Before you go through these steps, ensure your AWS CLI is set up with the proper credentials. You’ll also need the Twilio CLI running.

Awesome – now walk through the steps of the Kinesis Quickstart and I’ll meet you back here.

Back already? That’s great – we’re ready to move onto ServiceNow.

Set up a ServiceNow Developer Instance

If you haven’t yet, log into your ServiceNow Developer account. Create a ServiceNow instance in any location (we tested this demo in SanDiego). Then, click Start Building

Here’s how to set up the demo:

  1. Create a demo user – this user will be able to create the incident.
    • Click All > System Security > Users and Groups > Users
    • Click on New
      • Provide Username =`twilio_demo_user`
      • Add `email`, `Firstname` and `Lastname`
    • The user should have access to call the ServiceNow APIs
    • Once user is created setup password
    • Copy the username (twilio_demo_user) and password. This will be required while setting up Twilio Serverless Functions
  2. Create business rules – this business rule will be triggered when a high priority incident is created in ServiceNow.
    • Click All > System Definition > Business Rules
    • Click on New
      • Provide Name=`Twilio incident response`
      • Select table=`Incident`
      • Under `When to run` select `after`
      • Add filter condition
      • Select field=`priority` `is` and value=`2-high`
    • Check the advance box.
    • Click on the Advanced tab.
    • Open twilio-servicenow-aws-findagent-or-escalate-ivr/twilio_studio_new_incident.js
    • Modify the TWILIO_FUNCTION_URL value to point to the Twilio Serverless function execute.js function, deployed in the steps above
    • Copy the contents of execute.js into the Advanced Script tab in ServiceNow.
    • Submit to create the business rule

Defining business rules for a ServiceNow Twilio integration

Great! You’ve got all the pieces in place – you’re ready to run the demo.

Run the demo

If you logged out, you’ll need to log back into the ServiceNow Developer Instance.

  1. Go to Menu All > Incidents > New
  2. Fill in all the required details, including a short description of the incident
  3. Select Urgency = High and Submit. This will invoke the business rule to trigger the Twilio IVR Flow
  4. The IVR flow will call the phone number configured as a part of Twilio Serverless Functions setup. (Refer to the Section above on setting up Twilio)
  5. Listen to the options: Press 1 to accept the call, 2 to transfer to the next agent or 3 to escalate.
  6. Try out all the options and check the status of the incident in ServiceNow. The Additional comments (Customer visible) section will be populated with the appropriate comments.

And that’s it! Hopefully you saw everything working – great work.

Integrating Twilio and ServiceNow to escalate high-priority incidents

Congratulations! You now have a deep integration with ServiceNow that lets you quickly respond to high-priority incidents. I want to encourage you to customize this IVR demo to you needs. A few areas that can be customized or I’d like your feedback include:

  1. Try the demo and provide feedback, if any.
  2. Customize the IVR flow to try more widgets like Send SMS messages and Wait to send messages to the agent in case he is unable to pick up the call. Refer to the rich Twilio Studio Widget Library
  3. It is quite common that our customers make extensive use of Webhooks with Twilio. Customize Twilio Event Streams to integrate with Webhook Sink. Webhook Quickstart
  4. Currently the demo uses a hard-coded agent-list and escalation list using Twilio Serverless Functions. This can be easily extended to connect to your own databases or CRM systems. An example to integrate with Airtable can be found here. Twilio Serverless Function integration with Airtable

Arun Wagle is a Solutions Architect - Enterprise ISV

Pathik Soni is a Solutions Architect helping Enterprise ISV partners reimagine their customer engagement experience using Twilio