综合编程

Identifying a Phone Number's Line Type with Twilio Lookup and Python

微信扫一扫,分享到朋友圈

Identifying a Phone Number's Line Type with Twilio Lookup and Python
0

While the Twilio Messaging API won’t charge you for attempting to send an SMS to a landline number, identifying the line type of a phone number can be useful for other reasons. A lot of businesses won’t let users sign up with VoIP numbers, for example, because those numbers are more correlated with fraud.

This post will show you how to identify a phone number’s line type with Twilio Lookup and Python.

Set Up

In order to code along with this post you’ll want to start with the following:

  1. Create a Twilio account
  2. Install Python 3
  3. Install the twilio-python helper library

Create a new file called lookup.py
and add the following code.

# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = 'your_account_sid'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

phone_number = client.lookups.phone_numbers('+15108675310').fetch(type='carrier')

print(phone_number.carrier)

Replace the placeholder credential values

Swap the placeholder values for your_account_sid
and your_auth_token
with your personal Twilio credentials. Go to https://www.twilio.com/console
and log in. On this page, you’ll find your unique Account SID and Auth Token, which you’ll need any time you send messages through the Twilio Client like this. You can reveal your auth token by clicking on the ‘view’ link:

Please note: it’s okay to hardcode your credentials when getting started, but you should use environment variables to keep them secret before deploying to production. Check out how to set environment variables
for more information.

From the command line, run the code with:

python lookup.py

You should see output like this:

{ 
    'mobile_country_code': '310',
    'mobile_network_code': '120',
    'name': 'Sprint Spectrum, L.P.',
    'type': 'mobile',
    'error_code': None
}

How to tell if a phone number is mobile or landline or VoIP

This code gave us some useful information, but let’s create a reusable function so our application can be used as a handy script. In lookup.py
, add the following function:

def line_type(phone_number):
   """
   Args:
       phone_number (string): phone number in e.164 format

   Returns:
       string: line type associated with the phone number
               possible values are mobile, landline, or voip
   """
   phone_number_type = client 
       .lookups 
       .phone_numbers(phone_number) 
       .fetch(type='carrier') 
       .carrier 
       .get('type')
  
   return phone_number_type

Thepossible valuesfor line type are mobile
, landline
, or voip
. Next, replace the original phone_number = …
and print
statements in lookup.py
with the following:

import sys
if len(sys.argv) < 2:
   print("""
   Please provide a phone number in E.164 format like this:
   $ python lookup.py +15108675310
   """)
   sys.exit(0)

input_number = sys.argv[1]
print(line_type(input_number))

This includes a bit of error handling to make sure we provide a phone number when we run the script. Now our program can take a phone number as an argument and return its line type!

Try running it again with a new phone number, maybe your personal cell phone or the landline of your favorite pizza delivery shop. Don’t forget to put the number inE.164 format.

python lookup.py +12127363100
// landline

Ta da! Now we have a handy script we can use to determine the line type of a phone number.

What’s Next

Maybe you want to prompt customers to add SMS (or voice!) based 2FA,
clean up your database, or segment your users. With this simple code, you have a lot more information about what type of phone number is in your system.

You may also be interested in:

Have you built something that takes advantage of Twilio Lookup? Let me know in the comments or on Twitter @kelleyrobinson
.

Authors

阅读原文...


微信扫一扫,分享到朋友圈

Identifying a Phone Number's Line Type with Twilio Lookup and Python
0

Twilio

Spot The Discrepancies with Dialyzer for Erlang

上一篇

Visual Studio code - Java Classpath is incomplete&period; Only syntax errors will be reported

下一篇

评论已经被关闭。

插入图片

热门分类

往期推荐

Identifying a Phone Number's Line Type with Twilio Lookup and Python

长按储存图像,分享给朋友