22/03/2021
WhatsApp is one of the most popular cross-platform messaging tools that is used by a vast majority of people. Even though its recent privacy policy update came as an unpleasant surprise for many people, the user base WhatsApp still holds good and nearly 2 billion people use WhatsApp today. This huge user base has attracted many medium and large-scale businesses to use WhatsApp to interact with their customers. While WhatsApp itself provides a business version called “Whatstapp Business”, the features in it are simply not enough when you want to automate a lot of stuff using it. For example, Telegram provides bot feature which can be used to automate a lot of stuff, we have also previously used Telegram bot with Raspberry Pi and have built some simple project like controlling Pi GPIO pins from telegram to turn on or off few LEDs. You can check those projects if you are interested.
In this tutorial we will be building a Raspberry pi WhatsApp Bot, using Python pyautogui package. We will be launching WhatsApp web on our Pi and reading messages from our chat and responding back to those chats with appropriate answers. For demonstration purposes, I have programmed the Pi to control an LED using chat. But you can be creative and exchange any type of messages between your Pi and the people who chat with your Pi. So, let's get started.
Using WhatsApp on Raspberry Pi
If you are just installing WhatsApp on Raspberry Pi, you can check out the linked tutorial where we have used the Yowsup library. But this method has many limitations and the Yowsup package also suffers from many bugs. So in this tutorial, we will be exploring other options to do that.
Why not use the official WhatsApp Business API for Automation?
It is true that WhatsApp has its own API for business purposes using which you can automate WhatsApp messages using python. You might have also come across various businesses like OYO, MakeMyTrip, etc. using this feature to communicate with its customer. But for a maker or small-scale project, getting permission to access the API is painstakingly expensive.
To get around this, we can put together a small hack using which we can access contents on WhatsApp web to read and respond to message for free. In this tutorial, we are going to do exactly that and for this project, you just need a Raspberry Pi and an Internet connection.
Selenium vs pyautogui to Automate WhatsApp on Raspberry Pi
Now that we have decided to follow the less professional and low-cost way to automate WhatsApp, we have to decide between selenium and Pyautogui.
For those who are new, Selenium is a browser automation tool that is mainly intended to test web applications. But it can also be used to automate anything you need over the web, like sending a tweet, posting on Facebook, etc. basically, anything that you can do on a browser can be automated through selenium. We might also post another project with Raspberry Pi and selenium, so let me know in the comment section if you have any idea for that.
The other alternatives to selenium for WhatsApp browser automation are pyautoGUI and OpenCV. This package basically helps us to read what is on screen and navigate across the screen to perform any desired action. So most things that you can do with a keyboard and mouse on your raspberry pi can be done using pyautogui and paperclip package on python. We will look into the details on how to do it later.