AWS Contact Center

Creating data-driven IVRs in Amazon Connect using a REST API

You can create effortless, friction-less, and personalized experiences when building contact flows in Amazon Connect. Your callers don’t have to traverse through a labyrinth of menus and prompts to reach a customer service representative and repeat their information again. The quicker you can address their immediate needs, the better your customer satisfaction.

Data is at the center of this concept. When you store customer information in your CRM platforms and databases, you can use it in your interactive voice responses (IVR). You can make the caller experience personalized and pro-actively predict their issue.

Amazon Connect lets you create effortless experiences for your callers. Whether you have a few hundred customers or a few million, you can create personalized experiences geared towards individual needs. You can access data about callers and create dynamic prompts on the fly using Amazon Connect’s text-to-speech engine. You can define business logic at the heart of IVRs and create automation that relieve your contact center representatives of mundane and redundant tasks.

When it comes to data, there are different ways you can access it. Contact center developers can run queries against databases, invoke stored procedures, or consume SOAP-based and REST-based web services. This post focuses on how you can use the data retrieved using a REST API in Amazon Connect contact flows.

Amazon Connect is an open and extensible platform. You can bring in data from disparate sources and use that in Amazon Connect contact flows. This post demonstrates how to make REST API calls to the OpenWeather website to deliver forecast information based upon a zip code. You can translate the concepts explained in this post to other real-world scenarios and business problems.

Solution

This solution uses Amazon Connect, IAM roles, Lambda functions, and Amazon CloudWatch. The high-level steps are as follows:

  1. Create an IAM role for a Lambda function.
  2. Create the Lambda function for making API calls to the OpenWeather website.
  3. Test the Function.
  4. Add the function in Amazon Connect.
  5. Create a Contact Flow that executes the function.
  6. Assign a phone number to a contact flow for testing.

Prerequisites

Before getting started, make sure that you have the following resources in place:

Create an IAM role

Grant the Lambda function permission to write execution trace logs in CloudWatch. This feature is useful when you are troubleshooting your workloads. You can create a role and add permissions for CloudWatch, and add the built-in, AWS managed policy for enabling CloudWatch logging. For more information, see AWS Lambda Execution Role.

  1. Open the IAM console, and choose Roles, Create role.
  2. For Service to use this role, select Lambda.
  3. Choose Next.
  4. Search for and select the managed policy AWSLambdaBasicExecutionRole.
  5. Choose Next, but do not add any tags in the next step.
  6. Choose Next and review.
  7. Enter a name for your role. This post uses Role_Lambda_CW.
  8. Choose Create role.
IAM Role for Lambda Function

IAM Role for Lambda Function

Create a Lambda function

Create the Lambda function in the same Region as your Amazon Connect instance. For more information, see Create a Lambda Function with the Console.

  1. Open the Lambda console, and choose Create a function, Author from Scratch.
  2. Enter the following information:
    • For Function name, use GetWeatherInfoFunction.
    • For Run time, you have a variety of options. This post uses Python 3.7.
    • Under Permissions, choose Use an Existing Role.
    • Enter Role_Lambda_CW.
  3. Choose Create function.
  4. Select the lambda_function.py file and replace the code with the following:

 

from botocore.vendored import requests
import json
import os

def lambda_handler(event, context):
    print(event)
    details = event["Details"]
    parameters = details["Parameters"]
    zipcode = parameters["zipcode"]
    #At this point, you are doing some basic validation to ensure that the zip code is in right format
    if str.isdigit(zipcode) and len(zipcode)==5:
        pass
    else:
        return{
            'Success':'False'            
            }
        
    #At this point, you are making a GET request to the REST endpoint
    url = 'http://api.openweathermap.org/data/2.5/weather'
    APPID = os.environ["APPID"]
    parameters = {
        "zip":zipcode+",us",
        "APPID":APPID,
        "units":"Imperial"
    }
    
    try:
        resp = requests.get(url,params=parameters)
        
        if resp.status_code != 200:
            # This means something went wrong.
            print("there is an error")
            
            return{
                'Success':'False'            
                }
        else:
            
            response=json.loads(resp.text)
            weather=response['weather'][0]['description']
            temperature=response['main']['temp']
            place=response['name']
            wind=response['wind']['speed']
            msg = json.loads(resp.text)
            print(msg)
            
            return{
                'Success':'True',
                'weather':weather,
                'temperature':temperature,
                'place':place,
                'wind':wind
            }
    except:
       return{
                'Success':'False'            
                }

5. For Environment variables, enter APPID and your OpenWeather API key.

Lambda Function Environment Variables

Lambda Function Environment Variables

6. Click on the Save button to Save your Lambda function.

Test the Lambda function

  1. On the same Lambda Console, Choose Select a test event.
  2. Copy the following JSON to create your test event:
{
  "Details": {
    "ContactData": {
      "Attributes": {},
      "Channel": "VOICE",
      "ContactId": "4a573372-1f28-4e26-b97b-XXXXXXXXXXX",
      "CustomerEndpoint": {
        "Address": "+1234567890",
        "Type": "TELEPHONE_NUMBER"
      },
      "InitialContactId": "4a573372-1f28-4e26-b97b-XXXXXXXXXXX",
      "InitiationMethod": "INBOUND | OUTBOUND | TRANSFER | CALLBACK",
      "InstanceARN": "arn:aws:connect:aws-region:1234567890:instance/c8c0e68d-2200-4265-82c0-XXXXXXXXXX",
      "PreviousContactId": "4a573372-1f28-4e26-b97b-XXXXXXXXXX",
      "Queue": "QueueName",
      "SystemEndpoint": {
        "Address": "+1234567890",
        "Type": "TELEPHONE_NUMBER"
      }
    },
    "Parameters": {
      "zipcode": "98101"
    }
  },
  "Name": "ContactFlowEvent"
}

3. Test the Lambda function execution with this test event by clicking on the Test Button on Lambda Console.

The weather information for the provided zip code is in the execution result:

{
  "Success": "True",
  "weather": "clear sky",
  "temperature": 70.92,
  "place": "Seattle",
  "wind": 3.36
}

Add the Lambda function in Amazon Connect

Add your new Lambda function in your Amazon Connect instance. For more information, see Invoke Lambda Functions.

  1. On AWS Management Console search for Amazon Connect or find Amazon Connect in Customer Engagement. Please make sure that you have selected the right AWS region on top right corner of the screen where your Amazon Connect instance is located.
  2. Click on Amazon Connect. This should take you to Amazon Connect Virtual Contact Center instances landing page.
  3. Click on the Alias for your Amazon Connect Instance. This will take you to Amazon Connect Console.
  4. Choose Contact flows and scroll down to see AWS Lambda.
  5. In AWS Lambda block use the Function drop down, select your new Lambda function, GetWeatherInfoFunction.
  6. Choose Add Lambda Function.
  7. Select Enable contact flow logs.

Create a contact flow to execute the Lambda function

The next step is creating a contact flow that greets your callers, asks them for the zip code for which they want weather information, and reads back that weather information. For more information, see Create Amazon Connect Contact Flows.

  1. Continuing from your previous screen, you can scroll up to find “Overview” and click on that.
  2. Then log into Amazon Connect administrator console by clicking “Login as administrator”
  3. In the Amazon Connect console, choose Routing by clicking on three arrow heads on the left bar and then click Contact flows.
  4. Click on  Create contact flow button.
  5. For the name for your contact flow, enter WeatherInfo. You can also download this sample contact flow and import it in your environment by selecting it from the dropdown menu next to Save button.

    Amazon Connect Sample Contact Flow

    Amazon Connect Sample Contact Flow

  6. For Invoke AWS Lambda function block, select the function that you created.
  7. Choose Use attribute and configure the following fields:
    • For Destination Key, enter zipcode.
    • For Type, choose System
    • For Attribute, choose Stored customer input.
  8. Choose Save.

In this IVR, when a caller provides a zip code, you make a REST API call to OpenWeather to get weather information for that zip code. Read back that weather information to the caller by using Amazon Connect’s text-to-speech capabilities. You can also create dynamic prompts to provide unique real-time information for any zip code entered by the caller. For more information about contact flow prompts, see Create Prompts.

 

Assign a phone number to a contact flow

To assign a phone number to your contact flow, complete the following steps:

  1. In the Amazon Connect console, click on the 3 arrow heads on the left bar to choose Routing and then click on Phone Numbers.
  2. Under Manage Phone Numbers, claim a phone number or edit an existing phone number by clicking on it.
  3. Assign that phone number to your contact flow and choose Save.

At this point you can call that phone number and enter zip code to retrieve and listen to real time weather information returned from Open Weather Map.

Summary

This post outlined how to deploy a solution for calling a phone number, entering any city zip code, and getting weather information. You can also look at trace logs of your contact flows and Lambda function logs in CloudWatch.

This solution is one example of how you can use REST APIs from Amazon Connect IVRs to create dynamic, personalized experiences for your callers. You can customize this solution to offer personalized experiences for your callers and create automations that can relieve your agents of routine, repetitive tasks.