WhatsApp has become one of the most popular instant messaging platforms with over 2 billion active users worldwide. The ability to automatically send WhatsApp messages using Python can be very useful for developers and businesses looking to engage with their audience.
In this comprehensive guide, we will walk through the steps to programmatically send WhatsApp messages using the Python language.
Requirements
To follow along with this tutorial, you will need to have the following requirements:
- Python 3 installed on your system
- WhatsApp account and phone number you want to send messages from
- Selenium library installed
- ChromeDriver installed and added to PATH
- Chrome browser installed
We will go through how to install these requirements shortly.
Overview of the Process
At a high level, here are the steps we will follow:
- Install Python and required libraries
- Get ChromeDriver and add it to PATH
- Launch Chrome browser using Selenium
- Automate login to WhatsApp Web
- Load list of target phone numbers and messages
- Loop through list to send messages
Let’s now understand each of these steps in more detail.
Installing Python
Since we will be using Python, the first step is to have Python installed on your system. Here are quick instructions to install Python:
- Download the latest Python 3.x installer for your operating system from python.org.
- Run the installer and make sure to check the option to add Python to PATH.
- Open a new command prompt and run the command `python –version` to verify Python installed correctly.
Installing Required Libraries
We will use the following Python libraries for automating WhatsApp:
- Selenium – For automating web browser actions
- Time – For adding delays between actions
You can install these using pip by running the following commands:
“`
pip install selenium
“`
“`
pip install time
“`
This will download and install the latest versions of these libraries.
Downloading ChromeDriver
In order to control Chrome browser using Selenium, we need ChromeDriver. Here are the steps to get ChromeDriver:
- Go to https://chromedriver.chromium.org/downloads
- Download the ChromeDriver version that matches your Chrome browser version
- Unzip the ChromeDriver executable file from the download
- Add ChromeDriver to your system PATH
You can now invoke ChromeDriver from anywhere on your system.
Launching Chrome Browser
With the prerequisites installed, we can now write some Python code to launch Chrome. Here is an example:
“`python
from selenium import webdriver
driver = webdriver.Chrome()
“`
This will automatically locate ChromeDriver and launch a new Chrome browser instance that we can control programmatically.
Automating WhatsApp Login
Once we have Chrome browser launched, the next step is to automate logging in to WhatsApp Web. Here are the steps:
- Navigate browser to https://web.whatsapp.com/
- Wait for QR code to appear and scan it using your phone
- Wait for login to complete
And here is sample Python code to implement this:
“`python
driver.get(“https://web.whatsapp.com/”)
# Wait for QR code
time.sleep(15)
# Scan QR code using phone
# Wait for login
time.sleep(15)
“`
We add waits after navigating to the WhatsApp URL to provide time for the QR code to load and get scanned.
Loading Target Phone Numbers
Now that we’re logged in, the next step is to load the list of phone numbers we want to send messages to. For this tutorial, we will load them from a CSV file. Here is some sample code:
“`python
import csv
contacts = []
with open(‘contacts.csv’) as file:
csv_reader = csv.reader(file)
for row in csv_reader:
phone = row[0]
contacts.append(phone)
“`
This loads the phone numbers from the CSV into a Python list contacts that we can iterate over.
Loading Messages to Send
Besides target phone numbers, we also need the messages we want to send. For this example, we will load them from a text file:
“`python
with open(‘messages.txt’) as file:
messages = file.readlines()
“`
This will load all the lines of the text file into a list that we’ll pick randomly to send.
Sending the WhatsApp Messages
Now for the final step – actually sending the WhatsApp messages. Here is what the code will look like:
“`python
for number in contacts:
message = random.choice(messages)
# Open chat window
driver.get(f”https://web.whatsapp.com/send?phone={number}”)
# Type message
input_box = driver.find_element_by_class_name(‘_3u328’)
input_box.send_keys(message)
# Hit enter to send message
input_box.send_keys(Keys.ENTER)
# Add delay before next message
time.sleep(5)
“`
We loop through each contact number, randomly choose a message, open the chat window, type the message, and hit enter to send it. We add a delay after each message to avoid getting rate limited.
Full Python Automation Script
Here is the full Python script putting all the code together:
“`python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import csv
import random
driver = webdriver.Chrome()
driver.get(“https://web.whatsapp.com/”)
time.sleep(15)
contacts = []
with open(‘contacts.csv’) as file:
csv_reader = csv.reader(file)
for row in csv_reader:
phone = row[0]
contacts.append(phone)
with open(‘messages.txt’) as file:
messages = file.readlines()
for number in contacts:
message = random.choice(messages)
driver.get(f”https://web.whatsapp.com/send?phone={number}”)
input_box = driver.find_element_by_class_name(‘_3u328’)
input_box.send_keys(message)
input_box.send_keys(Keys.ENTER)
time.sleep(5)
“`
And that’s it! Run this Python script to send automated WhatsApp messages to a list of numbers with random messages.
Conclusion
Sending automated WhatsApp messages with Python is simple using the Selenium library. Here are some key points we covered:
- Use Selenium and ChromeDriver to control Chrome browser
- Automate logging into WhatsApp Web
- Load target phone numbers and messages to send from files
- Use a loop to iterate through numbers and send messages
The full code is available on GitHub. Some applications could include sending promotions, reminders, notifications, or campaigns. Make sure to follow WhatsApp guidelines and don’t spam people!
Let me know if you have any other questions! I’m happy to help explain or expand on any part of this tutorial.
Thank you for reading! Please let me know if you need any clarification or have additional requests.