AWS Messaging & Targeting Blog
Retrying Undelivered Voice Messages with Amazon Pinpoint
Note: This post was written by Murat Balkan, an AWS Senior Solutions Architect.
Many of our customers use voice notifications to deliver mission-critical and time-sensitive messages to their users. Customers often configure their systems to retry delivery when these voice messages aren’t delivered the first time around. Other customers set up their systems to fall back to another channel in this situation.
This blog post shows you how to retry the delivery of a voice message if the initial attempt wasn’t successful.
Architecture
By completing the steps in this post, you can create a system that uses the architecture illustrated in the following image:
First, a Lambda function calls the SendMessage operation in the Amazon Pinpoint API. The SendMessage
operation then initiates a phone call to the recipient and generates a unique message ID, which is returned to the Lambda function. The Lambda function then adds this message ID to a DynamoDB table.
While Amazon Pinpoint attempts to deliver a message to a recipient, it emits several event records. These records indicate when the call is initiated, when the phone is ringing, when the call is answered, and so forth. Amazon Pinpoint publishes these events to an Amazon SNS topic. In this example, we’re only interested in the BUSY
, FAILED
, and NO_ANSWER
event types, so we add some filtering criteria.
An Amazon SQS queue then subscribes to the Amazon SNS topic and monitors the incoming events. The Delivery Delay attribute of this queue is also set at the queue level. This configuration provides a back-off retry mechanism for failed voice messages.
When the Delivery Delay timer is reached, another Lambda function polls the queue and extracts the MessageId
attribute from the polled message. It uses this attribute to locate the DynamoDB record for the original call. This record also tells us how many times Amazon Pinpoint has attempted to deliver the message.
The Lambda function compares the number of retries to a MAX_RETRY
environment variable to determine whether it should attempt to send the message again.
Prerequisites
To start sending transactional voice messages, create an Amazon Pinpoint project and then request a phone number. Next, clone this GitHub repository to your local machine.
After you add a long code to your account, use AWS SAM to deploy the remaining parts of this serverless architecture. You provide the long number as an input parameter to this template.
The AWS SAM template creates the following resources:
- A Lambda function (
CallGenerator
) that initiates a voice call using the Amazon Pinpoint API. - An Amazon SNS topic that collects state change events from Amazon Pinpoint.
- An Amazon SQS queue that queues the messages.
- A Lambda function (
RetryCallGenerator
) that polls the Amazon SQS queue and re-initiates the previously failed call attempt by calling theCallGenerator
function. - A DynamoDB table that contains information about previous attempts to deliver the call.
The template also defines a custom Lambda resource, CustomResource
, which creates a configuration set in Amazon Pinpoint. This configuration set specifies the events to send to Amazon SNS. A Lambda environment variable, CONFIG_SET_NAME
, contains the name of the configuration set.
This architecture consists of two Lambda functions, which are represented as two different apps in the AWS SAM template. These functions are named CallGenerator
and RetryCallGenerator
. The CallGenerator
function initiates the voice message with Amazon Pinpoint. The SendMessage API in Amazon Pinpoint returns a MessageId
. The architecture uses this ID as a key to connect messages to the various events that they generate. The CallGenerator
function also retains this ID in a DynamoDB table called call_attempts
. The RetryCallGenerator
function looks up the MessageId
in the call_attempts table. If necessary, the function tries to send the message again by invoking the CallGenerator
function.
Deploying and Testing
Start by downloading the template from the GitHub repository. AWS SAM requires you to specify an Amazon Simple Storage Service (Amazon S3) bucket to hold the deployment artifacts. If you haven’t already created a bucket for this purpose, create one now. The bucket should be reachable by a AWS Identity and Access Management (IAM) user.
At the command line, enter the following command to package the application:
sam package --template template.yaml --output-template-file output_template.yaml --s3-bucket BUCKET_NAME_HERE
In the preceding command, replace BUCKET_NAME_HERE
with the name of the Amazon S3 bucket that should hold the deployment artifacts.
AWS SAM packages the application and copies it into the Amazon S3 bucket. This AWS SAM template requires you to specify three parameters: longCode
, the phone number that’s used to make the outbound calls; maxRetry
, which is used to set the MAX_RETRY environment variable for the RetryCallGenerator
application; and retryDelaySeconds
, which sets the delivery delay time for the Amazon SQS queue.
When the AWS SAM package command finishes running, enter the following command to deploy the package:
sam deploy --template-file output_template.yaml --stack-name blogstack --capabilities CAPABILITY_IAM --parameter-overrides maxRetry=2 longCode=LONG_CODE retryDelaySeconds=60
In the preceding command, replace LONG_CODE
with the dedicated phone number that you acquired earlier.
When you run this command, AWS SAM shows the progress of the deployment. When the deployment finishes, you can test it by sending a sample event to the CallGenerator
Lambda function. Use the following sample event to test the Lambda function:
{
"Message": "<speak>Thank you for visiting the AWS <emphasis>Messaging and Targeting Blog</emphasis>.</speak>",
"PhoneNumber": "DESTINATION_PHONE_NUMBER",
"RetryCount": 0
}
In the preceding event, replace DESTINATION_PHONE_NUMBER
with the phone number to which you want to send a test message.
Important: Telecommunication providers take several steps to limit unsolicited voice messages. For example, providers in the United States only deliver a certain number of automated voice messages to each recipient per day. For this reason, you can only use Amazon Pinpoint to send 10 calls per day to each recipient. Keep this limit in mind during the testing process.
Conclusion
This architecture shows how Amazon Pinpoint can deliver state change events to Amazon SNS and how a serverless application can use it. You can adapt this architecture to apply to other use cases, such as call auditing, advanced call analytics and more.