Written by 2:40 pm Marketing Automation, Tech

Formatting Phone Numbers in Zapier with Python

Automate lead generation with Zapier and Python.

In today’s fast-paced world, automating tasks can save you time and energy. With Code by Zapier, you can streamline your lead generation process and pre-qualify leads based on the incoming stage of your pipeline. One of the challenges you may face is formatting phone numbers in Zapier to the international E.164 standard format. This is especially true when dealing with phone numbers that contain special characters or are in a format that doesn’t meet the E.164 standard, such as (+971) 0123456789. Doing this manually can be time-consuming, but in this article, we’ll show you how to use Python 3 and Code by Zapier to quickly format incoming phone numbers without the need for manual formatting. With our solution, you’ll be able to easily integrate with WhatsApp and send personalized messages to your leads using their first name, saving you time and helping you close deals faster.
Formatting Phone Numbers in Zapier and Python 3

 

The lead form we’re using only contains the Full Name, Country Code, Phone Number, number of beds, and the best time to reach out, making it simple for prospects to fill out. However, the challenge is that when automating the process and feeding the information into your CRM, the automation only takes the first name. This means that we need Code by Zapier to help us extract the first name and personalize our outreach, so our messages feel more natural and less automated. This makes it easier to connect with leads and nurture them towards a sale.

Sitting up Code by Zapier using Python 3

To begin, we’ll assume that you have a landing page with a form that collects a lead’s full name, email address, and phone number with a separate country code similar to above’s screenshot. When a new lead submits the form, Zapier will receive the data via a webhooks trigger event called “Catch Hook.” We’ll then use Code by Zapier to format the phone number in the E.164 standard format and extract the lead’s first name from the full name.

Webhooks - Formatting Phone Numbers in Zapier

The first step is to create an event in called “Run Python” using the app “Code by Zapier” that responds to data from the “Catch Hook” trigger.

Code by Zapier

We assign key names (left) : country_code, phone_number, and full_name. Then, we map values (right) from previous steps, as shown in the screenshot below.

Python Code - Formatting Phone Numbers in Zapier

 

After defining the input fields, we remove the parentheses from the country code and the leading “0” from the phone number. We then combine the country code and phone number into one string and define the regular expression pattern to match the phone number. If a match is found, we extract the phone number and format it according to the E.164 standard. Finally, we extract the lead’s first name from the full name and add it to the output.

 

Formatting Phone Numbers in Zapier

 

Understanding the Python Code

import re

country_code = input_data['country_code'].replace('(', '').replace(')', '')
phone_number = input_data['phone_number'].lstrip('0')
full_name = input_data['full_name']
input_string = f"{country_code}{phone_number}"
pattern = r'\+?(\d{3})\s?(\d{2,3})\s?(\d{3})\s?(\d{3})'
match = re.search(pattern, input_string)
if match:
formatted_phone_number = f"+{match.group(1)} {match.group(2)} {match.group(3)} {match.group(4)}"
output = {'phone_number': formatted_phone_number}
else:
output = {'phone_number': 'No phone number found'}
first_name = full_name.split()[0]
output['first_name'] = first_name
print(output)

The code starts by importing the ‘re’ module which is used for regular expressions. Then it defines three variables ‘country_code’, ‘phone_number’, and ‘full_name’ that are assigned the values of corresponding fields from the input data, which is received from the Zapier webhook. The input_data variable is an object that contains the data received from the previous step of the Zapier workflow.

Next, the script combines the country code and phone number into a single string variable named ‘input_string’. It then defines a regular expression pattern, which is used to search for the phone number within the ‘input_string’. The pattern is designed to match phone numbers in the international E.164 standard format.

After that, the script searches for a match in the ‘input_string’ variable using the regular expression pattern defined earlier. If a match is found, it extracts the matched groups using the ‘group’ method and formats the phone number according to the E.164 standard. It then creates an ‘output’ variable that contains the formatted phone number.

In case a match is not found, the script creates an ‘output’ variable containing a message ‘No phone number found’.

Finally, the script extracts the first name from the ‘full_name’ variable, adds it to the ‘output’ variable, and prints the output.

With the formatted phone number and the lead’s first name, we can now feed the information into your CRM tool.

By formatting phone numbers in Zapier using Code by Zapier and Python 3, you can automate the lead generation process and pre-qualify leads based on the incoming stage of your pipeline. While there could be easier ways to resolve the formatting issues, this solution provides an efficient and effective way to pre-qualify leads and automate your CRM tool and send automated messages regarding the lead’s inquiry using the “Contact Name: First Name” field and these vary with CRM. With this automation, you can save time and focus on more important tasks, such as closing deals and growing your business. Now, you can streamline your lead generation process and focus on nurturing your leads and closing deals.

Disclaimer: The examples provided in this article demonstrate the use of Code by Zapier and Python 3 for formatting phone numbers in Zapier. The input and output data used in the examples are for demonstration purposes only and do not represent real-world data. The phone numbers used in the examples are blurred and tampered with to protect privacy. Please note that the author of this article is not affiliated with Zapier or any other companies mentioned above.

(Visited 198 times, 1 visits today)
Subscribe to my email list and stay up-to-date!
Tags: , , , , , , , , Last modified: February 17, 2023
Close Search Window
Close