I've divided up the process of implementing SMS (text message) forwarding into three parts since there's a lot of "gotchas" in this part. In the first part, I described the general process. In this part, I'll provide the steps to implement an auto-responder, plus a walk-through video to demonstrate everything that needs to be done and to point out all the gotchas. The third post will actually forward the SMS to an email.
There are two general steps in this post: setting up the AWS Lambda function and setting up an API Gateway to call that function. All the instructions are written out below, followed by the video walkthrough.
A lot of this post is based on the Twilio blog post about creating your own IVR (interactive voice resonse) system with API Gateway and Lambda.
Setting up the Lamba function
- Log into Amazon Web Serivces and go to the Lambda console.
- Create a new function based on the 'hello-world-python' template. (Hint: filter the templates by 'python')
- Name the function and give it a description. Paste in the following code for the function:
1 2 3 4 5 6
import json def lambda_handler(event, context): print(json.dumps(event, separators=(',',':'))) return {'message': "Sorry, but I haven't set up text forwarding yet. " + "Please email xxxxxxxxxxxxx@gmail.com"}
- Create a new IAM role for the function, then save the role and the function.
Setting up the API Gateway
- Go to the API Gateway console.
- Create a new API. Under that, create a resource named 'SMS' and then within that resource, create a POST method.
- Set the POST method to use the Lambda function you created above
- Update the Integration Request with a mapping template to convert content formatted as application/x-www-form-urlencoded. Use the following mapping template:
{ "reqbody":"$input.path('$')" }
- Update the Integration Response to use a mapping template, too. Use this template:
1 2 3 4 5
#set($inputRoot = $input.path('$')) <?xml version="1.0" encoding="UTF-8"?> <Response> <Message>$inputRoot.message</Message> </Response>
- Update the Method Response to send back an XML Content-Type header (application/xml) instead of JSON.
- Test it out in AWS.
- Deploy the API. (It doesn't matter what you name the stage.)
- Test it for real with curl. Replace the URL here with your URL, plus the path to the resource you created. If you used 'SMS' in step 2, the path will be '/sms'.
curl -X POST -H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" -d"From=%2B2345678901&Body=TestSMS" "https://foobar.execute-api.us-east-1.amazonaws.com/your-api-name/sms"
Update Twilio to point to the deployed URL (plus the path of the resource, e.g. https://foobar.execute-api.us-east-1.amazonaws.com/your-api-name/sms)
Test it out by sending a text message to your Twilio number.
If you have any trouble, the CloudWatch logs from the API Gateway and from the Lambda function will guide you.
Here's a walkthrough to help you understand all the minutia. The API Gateway console has a lot of unintuitive clicks that you probably will waste time finding if you don't watch the quick walkthrough.
Next Update: SMS Forwarding Part 3
No comments:
Post a Comment