Skip to content

Commit

Permalink
Improves automation script.
Browse files Browse the repository at this point in the history
  • Loading branch information
arkisoul committed Apr 29, 2021
1 parent 46731e1 commit 60eba9e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 65 deletions.
88 changes: 27 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Python Automated Bulk WhatsApp Messages

It is a python script that sends WhatsApp message automatically from WhatsApp web application. It can be configured to send advertising messages to customers. It read data from an excel sheet and send a configured message to people.
It is a python script that sends WhatsApp message automatically from WhatsApp web application. It can be configured to send advertising messages to customers. It read data from a Google sheet and send 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.
In order to run the python script, your system must have the following programs/packages installed and the contact number either should be saved in your phone or could be a new number.
* Python >= 3.8: Download it from https://www.python.org/downloads
* Selenium Web Driver: Either you can use repo driver else you can download it https://chromedriver.chromium.org/downloads
* Google Chrome : Download it from https://www.google.com/chrome
Expand All @@ -14,69 +14,35 @@ In order to run the python script, your system must have the following programs/
* Selenium: Run in command prompt **pip install selenium**

## Approach
* User scans web QR code to log in into the WhatsApp web application.
* The script reads a customized message from 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 next row.
* User scans web QR code to log in into the WhatsApp web application for the new session.
* The script reads a customized message from the Google 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 creates url for new number. Then it reads next row.
* Loop execute until and unless all rows complete.

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

## Code
## Process
* Clone this repository
```commandline
git clone https://github.com/arkisoul/whatsapp-bulk-message-automation
```
# Program to send bulk customized message through WhatsApp web application
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()
* Change directory
```commandline
cd whatsapp-bulk-message-automation
```
* Use any virtualenv for python venv, pipenv, conda and install application dependencies
```commandline
pip install -e requirements.txt
```
* Use below command to send bulk message
* Without attachment
```commandline
python app/app.py SHEET_ID SHEET_NAME SHEET_GID
```
* With attachment
```commandline
python app/app.py --image-path='absolute/path/to/the/image' SHEET_ID SHEET_NAME SHEET_GID
```
Note: The script may not work in case if the HTML of web WhatsApp is changed.
### Inspiration
This script is inspired from @inforkgodara [repo](https://github.com/inforkgodara/python-automated-bulk-whatsapp-messages) and updated to read a Google sheet and send to any whatsapp number.
This script is inspired from @inforkgodara [repo](https://github.com/inforkgodara/python-automated-bulk-whatsapp-messages) and updated to read a Google sheet and send to any whatsapp number.
12 changes: 8 additions & 4 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def __init__(self, **kwargs):
self.sheet_id = kwargs.get('sheet_id')
self.sheet_name = kwargs.get('sheet_name')
self.image_path = kwargs.get('image_path')
self.url = f'https://docs.google.com/spreadsheets/d/{self.sheet_id}/export?format=xlsx&gid=1484715859'
self.sheet_gid = kwargs.get('sheet_gid')
self.url = f'https://docs.google.com/spreadsheets/d/{self.sheet_id}/export?format=xlsx&gid={self.sheet_gid}'
self.excel_data = None
self.driver = None
self.driver_wait = None
Expand All @@ -42,6 +43,9 @@ def load_driver(self):
# Load the chrome driver
options = webdriver.ChromeOptions()
options.add_argument(config.CHROME_PROFILE_PATH)
if config.os_name == 'Windows':
self.driver = webdriver.Chrome(executable_path=r'C:\Users\Nityam\AppData\Local\Programs\Python\Python39\chromedriver.exe',
options=options)
self.driver = webdriver.Chrome(options=options)

# Open WhatsApp URL in chrome browser
Expand Down Expand Up @@ -115,9 +119,9 @@ def close_driver(self):

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Whatsapp Bulk Message Automation with optional Attachment feature')
parser.add_argument('sheet_id', help='Google Sheet Id', default='1fB1UHWOHXGTWQJ208UEiermUZ6MfOTZpqI_Tea8Vwqw',
type=str)
parser.add_argument('sheet_name', help='Google Sheet name', default='Customers', type=str)
parser.add_argument('sheet_id', help='Google Sheet Id', type=str)
parser.add_argument('sheet_name', help='Google Sheet name', type=str)
parser.add_argument('sheet_gid', help='Google Sheet gid', type=int)
parser.add_argument('--image-path', help='Full path of image attachment', type=str, dest='image_path')
parsed_args = parser.parse_args()
args = vars(parsed_args)
Expand Down

0 comments on commit 60eba9e

Please sign in to comment.