Skip to content

Instantly share code, notes, and snippets.

@katieli-waymark
Created February 12, 2024 21:11
Show Gist options
  • Select an option

  • Save katieli-waymark/bc912b0bcb4fd97aa2fcd58d8775ba8b to your computer and use it in GitHub Desktop.

Select an option

Save katieli-waymark/bc912b0bcb4fd97aa2fcd58d8775ba8b to your computer and use it in GitHub Desktop.
Patient Phone Numbers: Twilio Lookup API Results
import os
from twilio.rest import Client
import pandas as pd
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
client = Client(account_sid, auth_token)
N_PHONE_NUMBERS = 1486
# =================== Helper functions ====================
def read_csv_with_pandas(file_path):
data = pd.read_csv(file_path, dtype=str)
return data
# =================== Twilio helper functions ====================
def get_phone_number_info(phone_number: str):
result = client.lookups.v2.phone_numbers(phone_number).fetch(
fields="line_type_intelligence"
)
print(f"Phone number: {phone_number} is valid: {result.valid}")
validation_errors = ",".join(result.validation_errors)
line_type_intelligence = result.line_type_intelligence
return pd.Series(
dict(
phone_number=str(result.phone_number),
valid=result.valid,
validation_errors=validation_errors if validation_errors else None,
phone_type=line_type_intelligence["type"],
carrier_name=line_type_intelligence["carrier_name"],
)
)
# ================================================================#
# Use line type intelligence lookup endpoint for phone numbers
# ================================================================#
# ---------------------------------------------------------------#
# Unverified phone numbers
# ---------------------------------------------------------------#
input_file = "input_data/Patient_phone_numbers.csv"
output_file = "output/patient_phone_numbers__line_intelligence.csv"
raw_data = read_csv_with_pandas(input_file)
# input data to dataframe
df = raw_data.copy()
df.rename(
columns={"Patient Phone Number": "phone_number", "Last Name": "name"}, inplace=True
)
# clear whitespace off the phone numbers, prepare dataset
df.phone_number = df.phone_number.apply(lambda u: u.strip())
test_df = df.sample(n=N_PHONE_NUMBERS, random_state=1)
test_df = test_df.merge(
test_df.apply(
lambda row: get_phone_number_info(row["phone_number"]),
axis=1,
),
on="phone_number",
)
# write output
test_df.to_csv(output_file, index=False)
print(f"All valid numbers: {all(test_df.valid)}")
# ---------------------------------------------------------------#
# Use this for the validated phone numbers
# ---------------------------------------------------------------#
input_file = "input_data/Verified_phone_numbers.csv"
output_file = "output/verified_numbers__line_intelligence.csv"
raw_data = read_csv_with_pandas(input_file)
# input data to dataframe
df = raw_data.copy()
df.rename(
columns={"Patient phone number": "phone_number", "Last Name (Verified)": "name"},
inplace=True,
)
df = df[["phone_number", "name"]]
# clear whitespace off the phone numbers, prepare dataset
df.phone_number = df.phone_number.apply(lambda u: u.strip())
test_df = df.sample(n=N_PHONE_NUMBERS, random_state=1)
test_df = test_df.merge(
test_df.apply(
lambda row: get_phone_number_info(row["phone_number"]),
axis=1,
),
on="phone_number",
)
# write output
test_df.to_csv(output_file, index=False)
print(f"All valid numbers: {all(test_df.valid)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment