Python Automated Bulk WhatsApp Messages

Ramesh Kumar
2 min readAug 1, 2020

About

It is a python script that sends WhatsApp message automatically from the WhatsApp web application. It can be configured to send advertising messages to customers. It read data from an excel sheet and sends a configured message to people.

Prerequisites

In order to run the python script, your system must have the following programs/packages installed and the contact number should be saved in your phone (You can use bulk contact number saving procedure of email). There is a way without saving the contact number but has the limitation to send the attachment.

  • Python
  • Selenium Web Driver
  • Google Chrome
  • Pandas
  • Xlrd

Approach

  • The user scans web QR code to log in to the WhatsApp web application.
  • The script reads a customized message from an excel sheet.
  • The script reads rows one by one and searches that contact number in the web search box if the contact number found on WhatsApp then it will send a configured message otherwise It reads the next row.
  • Loop executes until and unless all rows completely.

Note: If you wish to send an image instead of text you can write attachment selection python code.

Demo

Code

# Program to send bulk customized message through WhatsApp web application
# Author @inforkgodara
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
import pandas
import time
# Load the chrome driver
driver = webdriver.Chrome()
count = 0
# Open WhatsApp URL in chrome browser
driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 20)
# Read data from excel
excel_data = pandas.read_excel('Customer bulk email data.xlsx', sheet_name='Customers')
message = excel_data['Message'][0]
# Iterate excel rows till to finish
for column in excel_data['Name'].tolist():
# Locate search box through x_path
search_box = '//*[@id="side"]/div[1]/div/label/div/div[2]'
person_title = wait.until(lambda driver:driver.find_element_by_xpath(search_box))
# Clear search box if any contact number is written in it
person_title.clear()
# Send contact number in search box
person_title.send_keys(str(excel_data['Contact'][count]))
count = count + 1
# Wait for 3 seconds to search contact number
time.sleep(3)
try:
# Load error message in case unavailability of contact number
element = driver.find_element_by_xpath('//*[@id="pane-side"]/div[1]/div/span')
except NoSuchElementException:
# Format the message from excel sheet
message = message.replace('{customer_name}', column)
person_title.send_keys(Keys.ENTER)
actions = ActionChains(driver)
actions.send_keys(message)
actions.send_keys(Keys.ENTER)
actions.perform()
# Close Chrome browser
driver.quit()

--

--