Automate Your Customized Messages To Your Contacts With Python
It’s a beautiful brand new year! You want to send a New Year message to all your contacts via WhatsApp while specifically addressing each contact with a personal touch. Things become difficult if you have a lot of contacts and it can be frustrating. With the limited capability of WhatsApp’s broadcast feature, you will not be able to do this seamlessly to reach all your contacts and address them specifically. Well, the good news is that I got you!
All you need is a laptop, an internet connection and an Interactive Development Environment, (IDE) to run python on. And oh, a cup of coffee if you will.
Pre-requisites
In order to run this script, you’ll need to have the following packages installed on your computer.
- Python 3.8
- Selenium Web Driver
- Google Chrome
- Pandas
- Xlrd
- Selenium
You will also need an excel file that contains 3 columns- the name of your contact, their phone number and the message you want to send to all of them:
Process
Great! Now that we have all the packages installed, the next thing to do is to import them using the IDE
We will store our session details so that we do not have to be logging in to WhatsApp web every time.
After that, we need to initialize chrome and then specify the path to the location where the chrome driver is located.
ChromeDriver is a separate executable that Selenium WebDriver uses to control Chrome.
We will also add the options as we initialize chrome. We can also add a maximum wait time for an element to load on a webpage so that it does not throw an error at us all the time. I used 60 seconds
Once chrome is initialized we can now open the WhatsApp web URL
It will open a WhatsApp web interface which automatically asks you to scan the QR code
Now, we read the data from the excel file
Awesome! Now that we have read our data from the file, let’s move to our loop.
What this loop does is that it reads each row from the excel sheet and then searches for the contact number by inputting the number into the search box in WhatsApp web. If the number is found on WhatsApp, it will send a customized message to the contact associated with that number. Otherwise, it will read the next row of contact details and repeat the above.
To help understand things a little better, let me explain the code:
The search_box variable is an XPath path which is used to find the search input box.
Before a number is inputted in the WhatsApp web search box, the code above clears any number that might already be in the search box to avoid any errors.
Here, the number is inputted automatically into the search box.
This suspends the execution of the current thread for 4 seconds
The above code locates the message text box in the chat, enters the message and clicks enter automatically
This kills the instance of the chrome driver we used. If you don’t include this line, the process will keep running in your computer memory.
This simplified method of contacting your loved ones should help lift a burden off your shoulder.
I hope you enjoyed this read.
You can check out my GitHub for the entire code.
Resources Used