{{announcement.body}}
{{announcement.title}}

How I Created a WhatsApp Bot to Talk With My Friends

DZone 's Guide to

How I Created a WhatsApp Bot to Talk With My Friends

See how one person created a bot in WhatsApp to talk to his friends.

· AI Zone ·
Free Resource

Here is the full code if you want to jump right to it.

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. If you don’t reply or send a few “good morning” messages in a few groups, people would think you are actually dead or 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. Let’s consider posting on, 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 a solution to automate this scenario, which will save my screen time.

How?

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 a matter of fact, I found a 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 anyway for other feature. I will write about it in another post; stay tuned).

Just like a production issue solved before the deadline, I found one repo that gives programmable access to WhatsApp, which works the way I want it to.

Enough...show me the code

Here's the gist of it. I have added a link to the repo as well.

  1. First, I created a puppeteer browser window and opened Whatsapp like this:

2. Then, I created a JSON file to manage messages and it’s equivalent reply and also to add some configs.

3. Next, I added a function inside inject.js , which would be called every time I get a new message.

Final results

With some garnishing with libraries like Ora, Cli-progress, and qrcode-terminal on the top,

I got this:

Here is the full code:

View it on GitHub

Conclusion

This is what technology is all about. Identify the problem and fix it with the right tools/tech. There are so many possibilities with the combination of tools like Google Puppeteer and NodeJS. We just need to be self-aware enough to look for a solution to the problem we have in our day-to-day lives.

Reward me with your comments below...you are awesome!

PS: 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.

Topics:
javascript ,bot ,puppeteer ,web automation ,artificial intelligence ,whatsapp

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}