Build the Text Bot
In this section the two way portion of the SMS/MMS text-bot will be created. While Amazon Lex could be an option in this application, this workshop uses a custom built bot powered by a python lambda function. Lambda allows for flexibility when dealing with MMS messages. Additionally, "Confirm", "Cancel", and a "selfie" are the primary expected messages and the logic for a bot is relatively simple.
An API is built in this section. The API has a predefined swagger document that will handle the xml used by Twilio. The API will be pre-depolyed and no configuration is needed outside of the CloudFormation template.
Twilio will then be integrated to AWS. This will allow for two way conversation over SMS.
Step1: CloudFormation Quick Start
A lambda function and an API Gateway will be created via CloudFormation.
To start, click on the Launch Stack button in your desired region below. US West (Oregon) is the recommended region for this workshop if at re:Invent.
Ensure that you are consistent with the selected region throughout this workshop. Deployments in multiple regions will lead to multiple AWS services not communicating together.
|US West (Oregon)|
|US East (Ohio) - Coming Soon|
|US East (N. Virginia) - Coming Soon|
|EU (Ireland) - Coming Soon|
In a new tab you should be redirected to CloudFormations Quick create stack page as shown below.
For Stack name, the default
reg-app-text-bot can be used or customized.
The default for the Parameters section will work as is.
Frontend Table Name- Must match the original DynamoDB tables created in previous section.
Backend Table Name- Must match the original DynamoDB tables created in previous section.
Text Bot Function Name- Name for the lambda function.
On the next window, scroll to the bottom and under Capabilities check I acknowledge that AWS CloudFormation might create IAM resources. and click Create stack.
AWS API URL (Required)
Step2: Get the API URL
The API URL from API Gateway is needed for Twilio Integration.
In the services tab, go to API Gateway.
On the API Gateway dashboard, click on the new API. The name will be in the format of 0123456789-APIGateway.
On the left, click on Stages.
Under Stages, click on Prod to dropdown. Then click on Post. As shown on the left of the below snapshot.
Copy the Invoke URL and paste in your notepad, as shown on the right of the below snapshot. This will be used in the next step.
Back to Twilio (Required)
The API URL needs to be integrated with the Twilio Messaging Service.
Step3: Go Back to Twilio
Head back to the Twilio Console. On the left of the Twilio site, click on Programable SMS
On the left click on SMS.
Under Messaging Services click on the messaging service created in a prior section.
Go to settings if not already directed to the page.
Under Inbound Settings select
SEND AN INCOMING_MESSAGE WEBHOOK, as shown below.
For Request Url enter the AWS API URL copied in the previous step. As shown below.
Click Save at the bottom of the Twilio page.
You now have the capability to have a two way, back and forth, conversation over SMS/MMS.
SMS Text to the Bot (Required)
Step4: Respond to the Text on Your Cell
Go to the text on your cell that was sent out in a previous section.
Respond to the message with
You should get a response as follows:
Send Selfie (Required)
When a selfie is sent. It will be captured in both Twilio and an s3 bucket on AWS. This image will be used to reocognize you via the deeplens. The image will also go to a website that is publicly hosted via CloudFront. Do Not send a selfie of yourself if you are not comfortable with the above. After the workshop, all images and supporting infrastuctor will be destroyed.
Step5: Respond to the Bot with a Selfie
Go ahead, send a selfie to the SMS Bot.
If you don't want to send a selfie of yourself, no worries. Take a picture of my selfie below or ask me in person.
You should get a response as follows:
You have now completed setting up your Text Message Bot lambda function and sending a selfie! The selfie was also added to an AWS Rekognition index.
Lambda Function (Optional)
Step6: Inspect the Lambda Function Code
In the services tab, go to Lambda.
On the left under AWS Lambda go to Functions.
Click on the lambda function created in the prior step. The default is
The first text message sent to Twilio was to Confirm for the event or Cancel. As shown below, simple lists are used to check if the received text message matched any word to confirm or cancel registration. Sending Cancel during this workshop will stop future SMS messages.
confirm_words = ["CONFIRM", "CONFIRMED", "CONFORMATION", "OK", "YES", "YEAH", "SURE", "REGISTERED"] cancel_words = ["UNSUBSCRIBE", "NO", "CANCEL", "UNCONFIRM", "STOP"]
The previous step already showed what the text will be ff the guests responds with Confirm. If Cancel is sent then no text will be sent out.
If no words match the lists
cancel_words then the below test message will be sent out.
Thanks for responding but we did not find any words like “confirm” or “cancel”. Can you please resend just a single word?