AWS Contact Center

Using Amazon Pinpoint to send text messages in Amazon Connect

Taking advantage of SMS text capabilities in your contact center provides another medium to improve your customers’ experience. Amazon Pinpoint lets you manage all facets of an SMS/email campaign, and now lets you send text messages from Amazon Connect. The instructions in this post help you enable text messages from your Amazon Connect contact flows by using Amazon Pinpoint and AWS Lambda.

Integrating Amazon Connect and Amazon Pinpoint with AWS Lambda

First, configure Amazon Pinpoint by doing the following:

  1. Open the Amazon Pinpoint console here.
  2. Create a project by choosing Create a Project.
  3. Name your project, for example, ConnectPinpoint, then choose Next.
  4. Choose Configure under SMS and Voice.
  5. Choose the checkbox for Enable the SMS channel for this project and choose Save.
  6. Open the Amazon Pinpoint console here and take note of the ID of your new project. You need this later.
  7. Review the default service limits for Amazon Pinpoint here to ensure they are applicable for your use case. To request a service limit increase, you can open a support case in the AWS Management Console here.

Next, write your AWS Lambda function by doing the following:

  1. Make sure that your AWS Lambda function has a role with the appropriate permission for both “mobileanalytics:* and “mobiletargeting:*” to send SMS messages through Amazon Pinpoint. We also recommend granting permission to Amazon CloudWatch for logging. For more information, please see the AWS Lambda Permissions documentation here.
  2. Open the AWS Lambda console and choose Create Lambda function here.
  3. Choose Author from scratch, name your function sendPinpointSMS, choose Node.js 10.x as the Runtime, and Create function.
  4. Edit the following sample code to change PASTE_YOUR_APPLICATION_ID_HERE to match the Amazon Pinpoint project ID you gathered earlier:
    var AWS = require("aws-sdk");
    var pinpoint = new AWS.Pinpoint();
    
    //main entry 
    exports.handler = (event, context, callback) => {
        console.log("Incoming Event: " + JSON.stringify(event));
    
        //You must have these attributes set in your Contact Flow prior to invoking lambda function
        var destinationNumber = event.Details.ContactData.Attributes.destinationNumber;
        var messageContent = event.Details.ContactData.Attributes.messageContent;
    
        var params = {
            //ApplicationId must match the ID of the application you created in AWS Mobile Hub
            ApplicationId: "PASTE_YOUR_APPLICATION_ID_HERE",
            MessageRequest: {
                Addresses: {
                    [destinationNumber]: {
                        ChannelType: "SMS",
                    },
                },
                MessageConfiguration: {
                    SMSMessage: {
                        Body: messageContent,
                        MessageType: "TRANSACTIONAL",
                        SenderId: "AWS",
                    }
                },
            }
        };
    
        // Send the SMS
        pinpoint.sendMessages(params, function(err, data) {
            if (err) {
                console.log(err);
                context.fail(buildResponse(false));
            } else {
                console.log("Great Success");
                callback(null, buildResponse(true, "none"));
            }
        });
    };
    
    // Return Result to Connect
    function buildResponse(isSuccess) {
        if (isSuccess) {
            return {
                lambdaResult: "Success"
            };
        } else {
            return {
                lambdaResult: "Error"
            };
        }
    }
  5. Set the Timeout value under Basic settings to 8 seconds, ensure the IAM policy selected for the Lambda function, and choose Save.
  6. Edit the Lambda sample test event below to change the destinationNumber of +14805551212  to match the number you’d like to send the message to:
         {  
               "Name":"ContactFlowExecution",
               "Details":{  
                  "Parameters":{},
                  "ContactData":{  
                     "ContactId":"ASDAcxcasDFSSDFs",
                     "InitialContactId":"Acxsada-asdasdaxA",
                     "PreviousContactId":"Acxsada-asdasdaxA",
                     "Channel":"Voice",
                     "InstanceARN":"",
                     "InitiationMethod":"INBOUND/OUTBOUND/TRANSFER/CALLBACK",
                     "SystemEndpoint":{  
                        "Type":"TELEPHONE_NUMBER",
                        "Address":"01234567"
                     },
                     "CustomerEndpoint":{  
                        "Type":"TELEPHONE_NUMBER",
                        "Address":"+14805551212"
                     },
                     "Queue":{  
                        "Name":"PrimaryPhoneQueue",
                        "ARN":""
                     },
                     "Attributes":{  
                        "destinationNumber":"+14805551212",
                        "messageContent":"testing 123."
                     }
                  }
               }
            }
    
  7. Paste your edited text into your Lambda test event. Execute your Lambda function by choosing Test. You should receive a text message on the number set for destinationNumber. If you do not receive a text, verify that the number is correct and in a valid E.164 format.
  8. Grant your Connect instance permission to invoke your Lambda function by following the instructions in the Amazon Connect Administrator guide here.

Configuring your contact flow to send SMS texts

  1. Create a new contact flow in your Amazon Connect instance, drag a Set contact attributes block into your contact flow editor, and choose the header of the Set contact attributes block to edit it.
  2. Choose Use attribute, enter destinationNumber as the Destination key value, choose System as the Type, and Customer Number as the Attribute.
  3. Choose Add another attribute, enter Pinpoint text from Connect! Great Success! as the value.
  4. In your contact flow editor, under Integration, drag an Invoke AWS Lambda function block, choose the header of the block to edit it, select your sendPinpointSMS Lambda function, enter seconds for the Timeout value, and choose Save.
  5. In your contact flow editor, under Interact, drag a Play prompt block, choose the header of the block to edit it, choose Text to speech (adhoc), enter Text message sent. Have a nice day! for your prompt message, and choose Save.
  6. In your contact flow editor, under Terminate/Transfer, drag a Disconnect / Hang up block to end the flow, and choose Save & Publish.
  7. In Amazon Connect, under Phone Numbers, choose Claim a Number to claim any DID or Toll-Free number that fits your use case. Under Contact flow / IVR, choose the contact flow you saved and published, and choose Save.
  8. Test by placing a call to the phone number you claimed and associated with your contact flow. You should receive a text message containing the value set for messageContent in your Set contact attributes block. If you did not receive a text, review the steps covered in the Lambda section of this post and make sure that you are calling from a device that can receive SMS text messages.

Conclusion

Using Amazon Pinpoint to send SMS text messages from Amazon Connect is a great way to send a recap of the call, links to helpful information, or as a way to follow up the call with a post-call survey. Allowing your callers multiple ways to access information that’s important to them is a great way to improve quality of experience and reduce the need to call in for repeat information. You can use this method in combination with other blog posts like this to identify repeat callers and send a text notification.