Ryan Kauffman
Published © CC BY-NC-ND

Sorry, New Phone... Who's This??

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?"

IntermediateFull instructions provided2 hours2,256
Sorry, New Phone... Who's This??

Things used in this project

Software apps and online services

SMS Messaging API
Twilio SMS Messaging API
AWS Lambda
Amazon Web Services AWS Lambda
Twilio Lookup

Story

Read more

Code

Code snippet #1

Plain text
#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
}

Code snippet #4

Plain text
from dotenv import load_dotenv
from twilio.rest import Client
load_dotenv()
import os
import re


# Your Account Sid and Auth Token from twilio.com/console
account_sid = os.getenv("ACCOUNT_SID")
auth_token = os.getenv("AUTH_TOKEN")
client = Client(account_sid, auth_token)

Code snippet #5

Plain text
from dotenv import load_dotenv
from twilio.rest import Client
load_dotenv()
import os
import re


# Your Account Sid and Auth Token from twilio.com/console
account_sid = os.getenv("ACCOUNT_SID")
auth_token = os.getenv("AUTH_TOKEN")
client = Client(account_sid, auth_token)

def parse_number(message):
  # Check for at least two numbers
 countryCode = '+1'
  if bool(re.search(r'\d{2}', message)):
    # Replace %2B with + and assign to variable because strings are immutable in Python
    number = message.replace('%2B', '+', 1)
    # Clean up number
    cleanedUpNumber = re.sub(r'([-() ])', "", number)
    # Check if it's less than 10 digits
    if len(cleanedUpNumber) < 10:
      return False
    elif len(cleanedUpNumber) == 10:
      return countryCode + cleanedUpNumber
    elif len(cleanedUpNumber) > 10:
      # Check for countryCode
      countryCode = '+1'
      if (cleanedUpNumber[0:2] == countryCode):
        return cleanedUpNumber
    else:
      return False
  else:
    return False

Code snippet #6

Plain text
from dotenv import load_dotenv
from twilio.rest import Client
load_dotenv()
import os
import re


# Your Account Sid and Auth Token from twilio.com/console
account_sid = os.getenv("ACCOUNT_SID")
auth_token = os.getenv("AUTH_TOKEN")
client = Client(account_sid, auth_token)

def parse_number(message):
  # Check for at least two numbers
 countryCode = '+1'
  if bool(re.search(r'\d{2}', message)):
    # Replace %2B with + and assign to variable because strings are immutable in Python
    number = message.replace('%2B', '+', 1)
    # Clean up number
    cleanedUpNumber = re.sub(r'([-() ])', "", number)
    # Check if it's less than 10 digits
    if len(cleanedUpNumber) < 10:
      return False
    elif len(cleanedUpNumber) == 10:
      return countryCode + cleanedUpNumber
    elif len(cleanedUpNumber) > 10:
      # Check for countryCode
      countryCode = '+1'
      if (cleanedUpNumber[0:2] == countryCode):
        return cleanedUpNumber
    else:
      return False
  else:
    return False

def send_message(message, to_number):
  client.messages.create(
        to=to_number,
        from_=os.getenv("TWILIO_NUMBER"),
        body=message
)

Code snippet #7

Plain text
from dotenv import load_dotenv
from twilio.rest import Client
load_dotenv()
import os
import re


# Your Account Sid and Auth Token from twilio.com/console
account_sid = os.getenv("ACCOUNT_SID")
auth_token = os.getenv("AUTH_TOKEN")
client = Client(account_sid, auth_token)

def parse_number(message):
  # Check for at least two numbers
 countryCode = '+1'
  if bool(re.search(r'\d{2}', message)):
    # Replace %2B with + and assign to variable because strings are immutable in Python
    number = message.replace('%2B', '+', 1)
    # Clean up number
    cleanedUpNumber = re.sub(r'([-() ])', "", number)
    # Check if it's less than 10 digits
    if len(cleanedUpNumber) < 10:
      return False
    elif len(cleanedUpNumber) == 10:
      return countryCode + cleanedUpNumber
    elif len(cleanedUpNumber) > 10:
      # Check for countryCode
      countryCode = '+1'
      if (cleanedUpNumber[0:2] == countryCode):
        return cleanedUpNumber
    else:
      return False
  else:
    return False

def send_message(message, to_number):
  client.messages.create(
        to=to_number,
        from_=os.getenv("TWILIO_NUMBER"),
        body=message
)

def number_lookup(number_to_lookup):
name = client.lookups.phone_numbers(number_to_lookup).fetch(type='caller-name')
  if name:
    return (name.caller_name)['caller_name']
  else:
    return False

Code snippet #8

Plain text
from dotenv import load_dotenv
from twilio.rest import Client
load_dotenv()
import os
import re


# Your Account Sid and Auth Token from twilio.com/console
account_sid = os.getenv("ACCOUNT_SID")
auth_token = os.getenv("AUTH_TOKEN")
client = Client(account_sid, auth_token)

def parse_number(message):
  # Check for at least two numbers
  countryCode = '+1'
  if bool(re.search(r'\d{2}', message)):
    # Replace %2B with + and assign to variable because strings are immutable in Python
    number = message.replace('%2B', '+', 1)
    # Clean up number
    cleanedUpNumber = re.sub(r'([-() ])', "", number)
    # Check if it's less than 10 digits
    if len(cleanedUpNumber) < 10:
      return False
    elif len(cleanedUpNumber) == 10:
      return countryCode + cleanedUpNumber
    elif len(cleanedUpNumber) > 10:
      # Check for countryCode
      countryCode = '+1'
      if (cleanedUpNumber[0:2] == countryCode):
        return cleanedUpNumber
    else:
      return False
  else:
    return False

def lookup_number(number_to_lookup):
  name = client.lookups.phone_numbers(number_to_lookup).fetch(type='caller-name')
  if name:
    return (name.caller_name)['caller_name']
  else:
    return False

def send_message(message, to_number):
  client.messages.create(
    to=to_number,
    from_=os.getenv("TWILIO_NUMBER"),
    body=message
  )

Code snippet #9

Plain text
from __future__ import print_function
from helpers import *


def lambda_handler(event, context):
    # Obtain the number from the incoming text
    number = parse_number(event['Body'])
    message = ''
    # Check for number
    if number:
      # Find out who the number belongs to
      callerName = lookup_number(number)
      if callerName:
        message = 'This number appears to be registered to: ' + callerName
      else:
        message = 'Aw. Sorry to let you down but we can\'t associate a name with that number.'
    else:
      message = 'Oops! The number you sent doesn\'t appear to be correct. Please make sure it has ten digits. Can you try sending the number again?'

    to_number = event['From']
    # Since the to_number is not formatted corrected, we add a + and slice off the first three chars and add a +
    # For example, the number looks like %2B16025551234, so we slice off %2B and add a plus.
    send_message(message, '+' + to_number[3:])

    print("Received event: " + str(event))
    return '<?xml version=\"1.0\" encoding=\"UTF-8\"?>'\
           '<Response><Message>All done!</Message></Response>'

Credits

Ryan Kauffman

Ryan Kauffman

3 projects • 9 followers

Comments

Add projectSign up / Login