Identify Unknown Phone Numbers with Python 3, AWS Lambda, Twilio Lookup and SMS

Ever receive a text, probably from an old friend, but you don’t have their number saved? And you’re too embarrassed to ask, “Who is this?”.

Don’t worry. We’ve all been there. But today, we’re here to tell you that we have a solution to avoid this situations!Let's build a small python app that usesTwilio Lookup andSMS to help you lookup phone numbers over text on the go.

The Tech Stack of Our App

Our app will use two AWS products and two Twilio APIs:

- AWS Lambda: the serverless environment which will hold our functions

- Amazon API Gateway : our API which exposes our Lambda to our Twilio number

-Lookup: to look up the phone number and see who it belongs to

-SMS: to send us a text with the phone number.

Here’s how everything will work together:

We’ll send a text message to our Twilio phone number That will trigger a POST request to a URL provided by our Amazon API Gateway which says, “Hey, Amazon! Take this information and use it when you run my functions in my Lambda.” Our functions will parse the number you want to lookup from the text message and see if it can find a name associated with the number. Then it will send the information it found to the number who requested it usingTwilio’s SMS API

Sounds pretty cool, eh? You’ll feel like a “ serverless Python guru ” after you build this. Trust us.


Identify Unknown Phone Numbers with Python 3, AWS Lambda, Twilio Lookup and SMS
Sign Up for Twilio & Get a Number

In order to getting started, we need to do a few things. First, make sure you sign up for aTwilio account.Then get your first Twilio phone number . We’ll configure this later.


Identify Unknown Phone Numbers with Python 3, AWS Lambda, Twilio Lookup and SMS

Click the red ‘+’ icon to purchase a Twilio phone number.

Set Up Our AWS Lambda Function

To get started with AWS Lambdas, head to the AWS Lambda page and click ‘Create an Account’. Select Lambda from the AWS Management Console screen.

Click ‘create a function’ then select ‘Blueprints’.Search for ‘microservice-http-endpoint-python3’ and select the card that looks like this:


Identify Unknown Phone Numbers with Python 3, AWS Lambda, Twilio Lookup and SMS

Select ‘microservice-http-endpoint-python3’ from the Blueprints.

Give your function the name ‘twilio_lookup_function’.For Role, select ‘Create a new role from one or more templates’.

Name the role ‘myTwilioRole’.Scroll down and select ‘create function’ to finish.

Great! Our AWS Lambda Function is scaffolded out. Before we leave this section, scroll down to ‘Environment Variables’. We need to add our Account SID and Auth Token from Twilio, and our Twilio phone number. You can obtain the first two from theTwilio Consoleand the Twilio number you purchased earlier. Once you have all three ready, add them in like so:


Identify Unknown Phone Numbers with Python 3, AWS Lambda, Twilio Lookup and SMS

Add your ACCOUNT_SID, AUTH_TOKEN and TWILIO_NUMBER as environment variables.

Great! Our AWS Lambda Function is setup. Now we can move on to the Amazon API Gateway.

Set Up Amazon API Gateway

The next service we’re going to use in our project is called API Gateway from Amazon. From the API Gateway Console , click ‘Create API’. Then follow these steps to get it set up.

For the name, you can call it ‘twilio_lookup_api’, then click create.

On the next screen, click ‘Actions’ and select ‘Create Resource’.

Under Resource Name, you can use ‘Twilio Responder’ and use /message for the path.Next, select ‘Actions’ again and click ‘Create Method’ and select ‘POST’.

In the ‘Lambda Function’ field, type the name of your function (i.e. ‘twilio_lookup_function’) and click Save.

Click Integration Request, then scroll down to ‘Mapping Templates’.Click ‘Add mapping template’.Put application/x-www-form-urlencoded in the field and click the checkmark.Click yes to secure the application in the pop-up.

Scroll down to the code editor and paste the following code in:

#set($httpPost = $input.path('$').split("&")) { #foreach( $kvPair in $httpPost ) #set($kvTokenised = $kvPair.split("=")) #if( $kvTokenised.size() > 1 ) "$kvTokenised[0]" : "$kvTokenised[1]"#if( $foreach.hasNext ),#end #else "$kvTokenised[0]" : ""#if( $foreach.hasNext ),#end #end #end }

Note that this code comes from the AWS Developer Forums to help us “split our HTTP parameters into JSON key/value pairs” as mentioned in thisin-depth tutorialin the Twilio Docs.

Scroll back up to the top and select ‘Method Execution’ to return to the previous screen.Click ‘Integration Response’ thenclick the left arrow on the side (see screenshot below) to expand the Method Response.


Identify Unknown Phone Numbers with Python 3, AWS Lambda, Twilio Lookup and SMS

Click ‘Mapping Templates’ and click ‘Add mapping template’. Paste in the field application/xml and click the checkmark.In the editor on the right, paste in this code and click save.

#set($inputRoot = $input.path('$')) $inputRoot

You can return to the previous screen by scrolling to the top and clicking ‘Method Execution’.

To recap, the reason we had to make adjustments in the ‘Mapping Templates’ sections of both the Method Request and the Method Response was because of the shape of the data we need for our application to work. Twilio will be sending data in the form application/x-www-form-urlencoded and we need it in JSON for our Lambda, and then Twilioexpects a response in application/xml so in the response we convert it from application/json .

If you get stuck in this section at all, check out thisin-depth tutorialin the Twilio Docs which walks through it all in more detail.

Deploy and Use Our API Gateway Now that our API Gateway is ready to work with Twilio, we can deploy it! Select ‘Actions’ and then click ‘Deploy API’. For stage, select ‘[New Stage]’ then name it ‘prod’ for production.

On the left side, select ‘Stages’. Click on the arrow next to ‘prod’ to toggle it and then click on ‘POST’ under ‘/message’. Here, you’ll find your Invoke URL.


Identify Unknown Phone Numbers with Python 3, AWS Lambda, Twilio Lookup and SMS

Copy that URL and save it for later. We’ll hook it up to our Twilio phone number at the end.

Parsing the Phone Number From Our Text

When a text message is sent to our app, we’ll need to do a few things:

Check to make sure a US phone number was sent (area code + seven digits) Remove spaces, dashes and parentheses

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

代码区博客精选文章
分页:12
转载请注明
本文标题:Identify Unknown Phone Numbers with Python 3, AWS Lambda, Twilio Lookup and SMS
本站链接:https://www.codesec.net/view/620926.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(165)