How I Created Whatsapp bot to talk with my friends?
š¤ Why?
The title seems kinda rude and lazy. Right? But if you think about the situation I am in then youāll understand. Here in India itās important to be socially active than being alive. If you donāt reply or send a few āgood morningā messages in a few groups, People would think you are actually dead. and taunt you for being busy and not spending time with them.š¤
Now, the thing with me and social media is, I want to use it on my time not when they send me a notification. So, there are situations Where I reply to a post after 4ā5 days and by that time my reply would not matter, as that moment would have been gone. Letās consider about posting. For example birthdays anniversaries etcā¦ But the thing is I donāt want to be that guy who replies after 4ā5 days. We all have 24 hours and I try my best to allocate it to all the things and priorities I have at my disposal. Iād get disappointed if someone replies to me after a week so I donāt want to be that guy for them.
So, I have come up with my programming skills to solve/automate this scenario which will save my screen time.š¤©
š¤Ø How?
How did I got inspired?
I was looking to do an automation on the web and just like Googleās top result, Selenium popped up in my mind but, as a JavaScript enthusiast, I choose JS as my ally. As an matter of fact, I found few modules in npm for Selenium but wasnāt really impressed by thatš.
While searching for that I stumbled upon this video from google IO 2018 and then this one which got me thinking like
How I did it?
First, I learned about a few tricks from this post
then, I realized that relying on DOM is not a good ideaš¤Ø. What if classes or selectors I have used got updated in the pageš¤? So I decided not to use DOM. (But I ended up using it any way for other featureš¤·āāļø. Will write about it in another post. Stay Tunedā¦ā¦.š)
Just like a production issue solved before the deadlineš, I found one repo which gives programmable access to the WhatsApp which works the same way as I want it.
Enough, Show me the code
Here are the overall steps and gist. I have added a link to the repo as well.
-
First I created a puppeteer browser window and opened Whatsapp like this
-
Then, I created a json file to manage messages and itās equivalent reply and also to add some configs.
-
Next, I added a function inside inject.js which would be called every time I get a new message
Final results
With the few garnishing with libraries like Ora, Cli-progress and qrcode-terminal on the top
I got this
ā Conclusion
This is what technology is all about. Identify the problem and fix the problem with the tools/tech. Isnāt it?
There are so many possibilities with the combination of the tools like Google Puppeteer and NodeJS. We just need to be self aware enough to look for solution to the problem we have in day to day life.
!! Reward me with your comments belowšā¦.. You are awesome !!
P.S: This is the first time I am sharing/writing over the internet. Feel free to point out mistakes so that I can improve that for my next post.
Originally posted on dev.to
I am getting notification about comments on this post but I am unable to see them here. If you have any questions feel free to reach out to me on twitter or telegram.
Thanks ā