AWS Contact Center

Redirecting voice contacts to chat on Amazon Connect

Erin Hall, Solutions Architect, Amazon Connect

This past November, Amazon Connect, our omnichannel cloud contact center service, launched a new channel for chat. The addition of chat provides businesses with more flexibility in how to engage with and delight their customers. One benefit of chat is the ability to decrease a customer’s time in queue. With voice calls, an agent can only talk to one customer at a time, but with chat, agents can help up to 5 customers at once. During this season of unprecedented call volumes, reducing a customer’s time in queue is crucial. If you have not migrated to chat yet, check out my colleague Randall’s blog about how to get started. Once you do have your chat offering available, the next step is to decide how to make your customers aware of this offering.

There are several ways to provide visibility for this new channel. First, you can make it easier for customers to start a chat from your website or mobile app by adding chat icons to your support or contact us pages. Second, you can tell your customers while they are in a voice queue that chat may be a faster option and let them know where on your website to find this option. Finally, when voice queues exceed desired service levels, you can proactively prompt customers to move to chat by sending them a direct link to your chat offering.

Several customers have asked how to proactively redirect their customers to chat. We are sharing one way you can accomplish this by using Amazon Connect and Amazon Pinpoint. This solution enables you to send customers in a voice queue an SMS with a link to begin chatting.

Overview of solution

The following diagram shows the general flow of how to send a customer a text message with a link to your chat offering. When a customer calls the business, the contact flow tells them the expected wait time and offers the option to switch to chat. If the customer selects this option, an AWS Lambda function sends the customer a text message through Amazon Pinpoint linking to your chat implementation.


redirect chat to voice flow

Walkthrough

This solution uses Amazon Connect, AWS Lambda, and Amazon Pinpoint. The high-level steps are as follows:

  1. Create an Amazon Pinpoint application.
  2. Create an AWS Identity and Access Management (IAM) policy.
  3. Create an IAM role.
  4. Create an AWS Lambda function.
  5. Test the Lambda function.
  6. Add the Lambda function to Amazon Connect.
  7. Create the contact flows in Amazon Connect.
  8. Claim a number and test.

Prerequisites

For this walkthrough, you should have the following prerequisites:

  • An AWS account
  • An Amazon Connect instance
  • Permissions to create IAM roles for Lambda functions
  • Permission to create Lambda functions and Amazon Pinpoint applications
  • Administrative access to Amazon Connect
  • A URL with your existing customer chat implementation. (If you haven’t created a website for your chat offering yet, you can follow my colleague Randall’s blog that describes how to deploy a CloudFormation template to get an example website up and running.)

Take note of what AWS Region you created your Amazon Connect instance in as you need that in a later step. Each of the resources created below should be in the same region as your instance.

Create an Amazon Pinpoint project

First, create a project in Amazon Pinpoint. Amazon Pinpoint enables businesses to understand and engage their customers by providing them personal, relevant, and timely communications through multiple channels. Amazon Pinpoint has a wide set of functionalities, but for this example we are just using it to send text messages.

  1. Open the Amazon Pinpoint console and choose Create a project.
  2. For Project name specify the name of your project, choose Create.
  3. In the Project Features section, under SMS and voice, choose Configure.
  4. Select Enable the SMS channel for this project.
  5. Review the default Account-level settings and make any adjustments necessary for your business.
  6. Choose Save changes.
  7. create pinpoint project

  8. Now you can test your configured Amazon Pinpoint project.
    1. In the navigation pane, choose Test messaging.
    2. For Channel, select SMS.
    3. Under Destinations, for Destination type select Phone numbers and specify the number you want to message.
    4. Under Message, for Message type choose Transactional, for Message content choose Create a new message, and for Message enter the message you want to send.
    5. Choose Send message.

test pinpoint

Once your project is created, navigate back to the main page of the Amazon Pinpoint console by choosing All Projects. You will see the project name and project ID in the table. Copy the project ID as you will need it in the next step.


amazon pinpoint project

Create an IAM policy

Next, create an AWS Identity and Access Management (IAM) policy that you will use in the role you create. This policy grants access to Amazon Pinpoint to send a message.

  1. Open the IAM console, and choose Policies, Create policy.
  2. Select the JSON tab and enter the following JSON blob. Be sure to replace the region, account_id, and application_id values.
  3. {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "VisualEditor0",
                "Effect": "Allow",
                "Action": "mobiletargeting:SendMessages",
                "Resource": [
                    "arn:aws:mobiletargeting:<region>:<account_id>:apps/<application_id>/messages",
                    "arn:aws:mobiletargeting:<region>:<account_id>:apps/<application_id>"
                ]
            }
        ]
    }
  4. Select Review policy.
  5. Enter a name for your policy. This post uses PinpointSendMessagePolicy.
  6. Choose Create policy.

Create an IAM role

Next, create an IAM role that the Lambda function will use. This IAM role needs permission to be used by Lambda, to write to CloudWatch Logs, and to send an Amazon Pinpoint message.

  1. Open the IAM console, and choose Roles, Create role.
  2. For services to use this role, select Lambda.
  3. Choose Next: Permissions.
  4. Search for and select the managed policy AWSLambdaBasicExecutionRole.
  5. Search for and select the policy you created, PinpointSendMessagePolicy.
  6. Choose Next: Tags. You can optionally add any tags in the next step.
  7. Choose Next: Review.
  8. Enter a name for your role. This post uses LambdaToPinpointRole.
  9. Click Create role.


iam role lambda pinpoint

Create a Lambda function

Now that you have your Amazon Pinpoint project ready, you will create an AWS Lambda function to send a text message through your application. Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume.

Create the function

  1. Go to the Lambda console and click Create function.
  2. Enter a name for your function, select the most recent Node.js version for the runtime.
  3. Under Permissions, choose Use an existing role, and select the LambdaToPinpointRole you created in the previous step.
  4. Choose Create function.
  5. Copy the following code into the index.js file.
  6. // Lambda function to send an SMS from the Pinpoint service
    
    // Load the AWS SDK
    var AWS = require(“aws-sdk”);
    // Define the Pinpoint Service
    var pinpoint = new AWS.Pinpoint();
    
    // Define the handler
    exports.handler = (event, context, callback) => {
    
        // Extract the customer’s number from the incoming event
        var phoneNumber = event.Details.ContactData.CustomerEndpoint.Address;
        var url = process.env.URL;
        var applicationId = process.env.APP_ID;
        
        // Add logic here to pass in any data relevant to your use case
        var name = “Jane”;
        var username = “janedoe”; 
        
        // Build the message body
        var smsBody = `Go start a chat here: ${url}?username=${username}&name=${name}`;
        
        // Set the parameters for the Pinpoint API
        var params = {
            ApplicationId: applicationId,
            MessageRequest: { 
                Addresses: {
                    [phoneNumber]: {
                        ChannelType: “SMS”,
                    },
                },
                MessageConfiguration: {
                    SMSMessage: {
                        Body: smsBody,
                        MessageType: “TRANSACTIONAL”,
                        SenderId: “AWS”,
                    }
                }
            }
        };
    
        // Send the SMS
        pinpoint.sendMessages(params, function(err, data) {
            if (err) {
                console.log(err);
                context.fail(buildResponse(false));
            }
            else {   
                callback(null, buildResponse(true, “none”));
            }
        });
    };
    
    // Return Result to Connect
    function buildResponse(isSuccess) {
        if (isSuccess) {
            return { lambdaResult: “Success”};
        }
        else {
            return { lambdaResult: “Error” };
        }
    }
    

    This code pulls the customer’s number from the contact flow event and sends them a text message with the URL to your customer chat widget. The URL in this example contains two variables, name and username, which are hard-coded. You can change the logic to instead pass in any data relevant for your use case.

  7. Click Save.

Add environment variables

  1. Scroll to the section titled Environment variables and select Edit.
  2. Add a variable with the key APP_ID and value as the Amazon Pinpoint application ID you saved earlier.
  3. Add another variable with the key URL and the value as the URL to where your chat widget lives.
  4. Click Save.


lambda environment variable

Test your Lambda function

Now you can test your Lambda function and send an SMS. You should still be viewing the Lambda function you just created.

  1. Choose Select a test event from the test section in the upper right-hand corner of the page.
  2. For Event template, select connect-contact-flow-event.
  3. For Event name, enter a name for your test event.
  4. Replace the phone number field in the example event with your number. Be sure to use e164 format when entering your number. The phone number field is under $Details.ContactData.CustomerEndpoint.Address
  5. {
        "Name": "ContactFlowEvent",
        "Details": {
            "ContactData": {
                "CustomerEndpoint": {
                    "Address:": "+1XXXYYYZZZZ",
                    "Type": "TELEPHONE_NUMBER"
                }
            }
        }
    }
  6. Choose Create.
  7. Choose Test to test your Lambda function.

You should receive an SMS with a link to the URL you added as an environment variable.

Add the Lambda function to Amazon Connect

As the final step for the Lambda function configuration, you need to give Amazon Connect permission to invoke your Lambda function.

  1. Navigate to the Amazon Connect console and select the instance you want to use.
  2. Click on Contact flows on the left-hand side of the page.
  3. Search for your function under the AWS Lambda section, and select Add Lambda function.

Create a contact flow in Amazon Connect

Now that you have the Lambda function and Amazon Pinpoint project set up, the final step is to invoke the Lambda function from a contact flow. You can import a pre-configured flow, but at a high level, the customer flow is this:

  1. Set the queue that you want the customer to be transferred to.
  2. Check the metrics for that queue.
  3. If the oldest contact in queue is less than 5 minutes, send the customer to the queue. Otherwise, give the customer the option to go to chat.
  4. If the customer does want to chat, confirm that they want to receive an SMS to the number they are calling from.
  5. If the customer does want to receive a text message, trigger the Lambda function to send the text.
  6. Proceed to put the customer into the voice queue in case they change their mind and want to stay on the phone.

Create and import the contact flow

  1. Log into your Amazon Connect instance. You can find the login URL on the instance overview page in the Amazon Connect console.
  2. Hover over the icon with three arrows and select Contact flows.
  3. Choose Create contact flow.
  4. Download the contact flow from here and upload it by selecting Import contact flow. The contact flow should look like the image below.

  5. import contact flow

  6. Choose Save.
  7. Open up the Invoke AWS Lambda function block and select the Lambda function you created.
  8. Click Publish.

Claim a number and test

Now that you have all the pieces in place, the final step is to claim a number and test the experience.

  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. Select your contact flow dropdown and choose Save.

Now you can call into your flow and get a text message directing your customers to your chat implementation.

text from pinpoint

Conclusion

In this post, you learned how to use Amazon Pinpoint to redirect customers to your Amazon Connect chat offering to save them from waiting in long voice queues. Now that you have the fundamental pieces in place, you can get creative with your contact flows and decide what other scenarios you might have in which you want to send a customer an SMS while in queue. For example, you could integrate an Amazon Lex bot into your contact flow that books an appointment for your customer and then sends them a confirmation text. You could send a text at the end of the interaction with information that the customer may need in the future, like a confirmation number. If you want to learn more about implementing chat, building personalized contact flows, or other best practices for Amazon Connect, check out our blog channel.

Suggested tags: Amazon Connect, contact center, Amazon Pinpoint, AWS Lambda, chat