AWS Contact Center
Creating dynamic, personalized experiences in Amazon Connect
Amazon Connect’s native integrations with other AWS services like AWS Lambda allow you to create dynamic, personalized experienced for your callers. In this blog post we will use AWS Lambda and Amazon DynamoDB to:
- Create an AWS Lambda function from the sample provided.
- Integrate your Lambda function with your Amazon Connect instance, letting you perform a data dip for name and membership level (Silver, Gold, or Platinum) based on the caller’s phone number.
- Route the call to the corresponding queue within your contact flow.
Preparation
For our AWS Lambda function to execute successfully, AWS Lambda requires an AWS Identity and Access Management (IAM) role and policy with the appropriate permissions. Use the following steps in this post to create a sample role and policy for AWS Lambda functions to access all Amazon DynamoDB tables.
Creating an IAM Policy
- In your AWS console, open AWS Identity and Access Management here.
- In the navigation pane, choose Policies, Create policy.
- Choose the JSON tab and paste the following text into the editor:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "lambda:*", "dynamodb:*" ], "Resource": "*" } ] }
- Name this policy lambdaDynamo and choose Create Policy.
Creating an IAM role
- In the navigation pane, choose Roles, Create new role.
- Choose Lambda as the AWS service that will use this role and choose Next.
- In the Filter search bar, type the name of the policy created earlier: lambdaDynamo.
- Choose the lambdaPolicy policy check box, choose Next:Tags, Next:Review.
- Name your role lambdaDynamoRole and choose Create role.
Amazon Connect Instance creation
In this post, you will require full administrative access to your Amazon Connect instance. For more information about setting up your instance, see any of the following:
- Amazon Connect Administrator Guide
- Amazon Connect User Guide
- Getting started with Amazon Connect (video)
Amazon DynamoDB integration
Amazon DynamoDB is a fast and flexible NoSQL database service for all applications that need consistent, single-digit millisecond latency at any scale. It is a fully managed cloud database and supports both document and key-value store models. Its flexible data model, reliable performance, and automatic scaling of throughput capacity, makes it a great fit for storing and referencing caller information from Amazon Connect.
- In your AWS console, open Amazon DynamoDB and choose Create table here.
- On the Create DynamoDB Table page, set your Table name to membershipTable and set the Primary key to phoneNumber.
- Choose Add sort key, and label it membershipLevel.
- Verify that all entries on the page are accurate, and choose Create.
- Wait for the table creation to complete, and for your table status to change to Active.
- Choose membershipTable, Indexes tab, and Create index.
- Enter phoneNumber for the Primary key, and choose Create index.
- Wait for the index creation to complete, and for your table status to change to Active.
- Choose membershipTable, the Items tab, and Create item.
- In the phoneNumber field, enter the phone number that you’ll use to test your completed contact flow, make sure to include the “+” country code, enter Platinum for the membershipLevel, and choose Save.
- Verify that the entry is correct. A phoneNumber mismatch causes the membershipLevel lookup to fail. As a result, you can’t determine the membershipLevel of the caller.
You can repeat steps 9 through 11 for as many phone numbers as you need.
For more information about Amazon DynamoDB, see the DynamoDB documentation here.
AWS Lambda integration
AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time that you consume. There is no charge when your code is not running. Upload your code and Lambda takes care of everything required to run and scale your code with high availability. With AWS Lambda and Amazon Connect, you can pass and return custom key-value pairs allowing for multiple integration points within your contact flows.
- In your AWS console, open AWS Lambda and choose Create Lambda function here.
- Choose Author from scratch.
- Name your function getMembership, choose Node.js 8.10 as the Runtime, and choose Create function.
- For your Lambda configuration, choose lambdaDynamoRole for Execution role, increase Timeout to 8 seconds, verify the settings, and choose Save.
- Replace the text in Lambda function code with the following sample code and choose Save:
var AWS = require("aws-sdk"); var docClient = new AWS.DynamoDB.DocumentClient(); exports.handler = (event, context, callback) => { var phoneNumber = event.Details.ContactData.CustomerEndpoint.Address; var paramsQuery = { TableName: 'membershipTable', KeyConditionExpression: "phoneNumber = :varNumber", IndexName: "phoneNumber-index", ExpressionAttributeValues: { ":varNumber": phoneNumber } }; docClient.query(paramsQuery, function(err, data) { if (err) { console.log(err); // an error occurred context.fail(buildResponse(false)); } else { console.log("DynamoDB Query Results:" + JSON.stringify(data)); if (data.Items.length === 1) { console.log(data.Items[0].membershipLevel); var membershipLevel = data.Items[0].membershipLevel; callback(null, buildResponse(true, membershipLevel)); } else { console.log("PhoneNumber not found"); callback(null, buildResponse(true, "none")); } } }); }; function buildResponse(isSuccess, membershipLevel) { if (isSuccess) { return { membershipLevel: membershipLevel, lambdaResult: "Success" }; } else { console.log("Lambda returned error to Connect"); return { lambdaResult: "Error" }; } }
- Choose Select a test event and Configure test events.
- Copy the test payload below, edit the +14805551212 value, and change it to the same phoneNumber value that you entered in your membershipLevel DynamoDB table:
{ "Name": "ContactFlowExecution", "Details": { "Parameters": { "key1": "value1", "key2": "value2" }, "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": { "key1": "value", "key2": "value" } } } }
- In the Configure test event window, replace the text with the text that you edited, and choose Create.
- Choose Test to execute your Lambda function.
- Make sure that your output displays Platinum for the membershipLevel, and Success for the lambdaResult
Troubleshooting tips: If you experience an error, it may have occurred because of insufficient IAM permissions, incorrect DynamoDB configuration, or a missing DynamoDB table index. If you fail to look up the membershipLevel for the phoneNumber and get none for the membershipLevel, your test event phoneNumber and DynamoDB table phoneNumber might be mismatched. The Log output section of your results normally indicates why the failure occurred.
Grant Amazon Connect permission to execute your Lambda function
- Grant your Amazon Connect instance to invoke your Lambda function by following the instructions in the Amazon Connect Administrator guide here.
- Verify that the permissions have been added correctly and you can see the Lambda function added to your Connect instance. If your Connect instance doesn’t have the proper permissions, your getMembership lookup will fail. As a result, you won’t be able determine the membershipLevel of the caller.
Creating your queues, routing profiles, and configuring your users
- In your Amazon Connect instance, choose Routing from the navigation bar, and choose Queues.
- Choose Add new queue, enter Silver for the Name.
- Choose your Hours of operation, Outbound caller ID number, Outbound caller ID name, and Add new queue.
- Repeat steps 2 and 3 for the Gold and Platinum queues.
- In your Amazon Connect instance, choose Users from the navigation bar, and choose Routing profiles.
- Choose Add new profile, enter Silver for the Name and Silver profile for the Description.
- Choose the Silver queue for Routing profile queues, Silver queue for Default outbound queue and Add new profile.
- Repeat steps 6 and 7 to create Gold and Platinum routing profiles.
- In your Amazon Connect instance, choose Users from the navigation bar, and choose User management.
- Choose the check boxes next to the users that you want to add to the Silver queue, and Edit.
- Under Routing Profile, choose the Silver routing profile, and Save.
- Repeat steps 10 and 11 for the Gold and Platinum users.
Creating your dynamic personalized contact flow in Amazon Connect
- In your Amazon Connect instance, select the Routing icon from the navigation bar, and choose Contact flows.
- Choose Create contact flow and drag a Play prompt block from the Interact section of the navigation bar. Connect the arrows, as shown in the following diagram:
- Choose the heading of the Play Prompt block to edit it, choose Text to speech (Ad hoc), enter the text Welcome to Amazon Connect, and choose Save.
- Drag an Invoke AWS Lambda function block from the Integrate section of the navigation bar. Connect the arrows, as shown in the following diagram:
- Choose the heading of the Invoke AWS Lambda block to edit it, choose your getMembership Lambda function, and Save.
- Drag a Check contact attributes block from the Branch section of the navigation bar. Connect the arrows, as shown in the following diagram:
- Choose the heading of the Check contact attributes block to edit it, choose External for Attribute to check, membershipLevel for the value, choose Add another condition, add a Equals condition for each membershipLevel, and Save.
- Drag a Set queue block from the Set section of the navigation bar and choose the heading of the Set queue block to edit it.
- Choose the Silver queue, connect this block to the Silver value from your Check contact attributes block, and repeat this step for the Gold and Platinum queues.
- Drag an arrow from No match to Set queue: Silver block for a default route, as shown in the following diagram:
- Drag a Transfer to queue block from the Terminate/Transfer section of the navigation bar. Connect the arrows, as shown in the following diagram:
- Enter a name for your contact flow, choose the drop-down arrow next to Save, and choose Save & Publish.
Associate a phone number with your contact flow
- In your Amazon Connect instance, choose the Routing icon from the navigation bar, and choose Phone numbers.
- Choose the phone number to associate with your new contact flow to edit it. Choose the name of your contact flow from the Contact flow/ IVR drop-down menu, and choose Save.
- Log in users and call the phone number you claimed from the phoneNumber you set in your DynamoDB membershipTable to test.
Conclusion
Creating dynamic, personalized experiences with Amazon Connect contact flows allows you to increase the quality of experience for your callers, decrease hold times, and offer self-service opportunities or custom messages. For example, you can use the same methods outlined in this post to identify that a caller is late on their payment and offer to collect it securely using the method outlined in the blog post here.