I had a problem up my sleeve. I had to find a way to wish people on festivals on their birthdays on the most popular messaging platform WhatsApp. On the weekend I managed to write something for this.
Firstly I would like to apologise for accidentally spamming my entire contacts box during testing. I had removed an if statement to wish only a single contact, forgot about it and inadvertently ended up sending messages to everyone.
There are several components necessary to run this script which include.
Someone has already written code to automated sending/receiving messages from Whatsapp. You can get the code here. This is a Selenium wrapper around WhatsApp Web. This will allow us to get info from WhatsApp and send messages.
I also used ChromeDriver instead of the Firefox web driver as it performed better in my testing.
Google Sheets/Drive API
We’ll use this to store the list of birthdays and festivals on different sheets. The reason why I’m using this instead of a database is because I can easily edit this.
You will have to get service credentials for Google using this. Also enable Google Drive and Google Sheets API from your Google developer console. Then go ahead and create two separate sheets – one for birthdays and other for festivals and share it with the email given in the JSON file shared by Google.
The sheets should look something like this:
Datebase to store KV pairs
I’m using PickleDB as a simple key value store to check if the message was sent to the individual contact.
Making everything work together
You can find my code here.
You will have to change the link to credentials and sheets referred to the gspread module. Then simply run the script. You will have to scan the QR code. If successful, you should see a message like this:
You do however have to run this everytime you need to send messages. This program looks at birthdays/festivals upto 7 days back and sends backdated wishes incase you missed to send them on the particular date. There should be a way to feeding the Excel file for all the birthdays. I’m looking at ways for that.
I thought you would show us the code!
You can find the code here: https://github.com/arhue/whatsapp-festival-bday-wisher