circuit

WhatsApp Bot for Auto Replying & Sending Images via Python and Selenium


Are you someone who wants to send lots of quotes or shayaris (poetry) to your ex or to a WhatsApp friends or colleague group? Or maybe random emojis to your wife or latest girlfriend? This blog will help you out in programming a bot to impress them.

Here comes the demo:

Bot performing auto replying and login

For implementing this, you will need Jupyter Python notebook installed. You can use this procedure illustrated over here for installation on Windows or Linux.

Also, we will be using Selenium, which is an automatic testing tool compatible with Java, Ruby, Python, and many other programming languages.

In order to install the selenium dependencies we will use pip (package installer for Python):

!pip install selenium!pip install webdriver_manager

For every block to run, we will use Shift+Enter to execute the relevant command.

Along with this, you will import the files like this :

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import random
from selenium.webdriver.common.keys import Keys

Python Dependencies installation

In order to open Google Chrome from Jupyter notebook automatically, we will use “chrome” element as an object from webdriver package and give it a relevant URL to open (Web.whatsapp.com for WhatsApp web).

chrome = webdriver.Chrome(ChromeDriverManager().install())
chrome.get(“https://web.whatsapp.com")

Once you execute this on Jupyter Notebook, you will open this :

WhatsAPP web opened from Jupyter

Once you have the QR code in front of you, scan your WhatsApp web and access your WhatsApp account like this:

WhatsApp web after scanned

You will need to access the search contact bar to find contacts for starting a new chat.

You can right-click upon it, and select “inspect element”.

Inspect element

Element class name for Search bar

We can see that the class name for the search bar is:”_3FRCZ”

Now we will extract the element using the Python Selenium packages and search for the group named Banglore :

search_box = chrome.find_element_by_class_name(“_3FRCZ”)
search_box.send_keys(“Banglore”)
search_box.send_keys(Keys.ENTER)

Banglore searched and appears on the top

Once “Banglore” appears on the top, we will find its ID again with xpath since Calss and ID's might create ambiguity:

We will fetch and write a Hello message 10 times using loop in the group:

Code for sending automatic Hello's ,emotions and images

Similarly , we can give 10 randomly generated emotions in the group

Emotions

Now we can also send attachments as pictures,videos,camera pics or contacts ,by accessing the attachment symbol:

Attachment symbol

Selecting the relevant things to attach

Once you get the xPath of icon photo attachment:

Script to attach the pics:

Sending pics via bot

These are simple packages to autotest the modules created, if you want to implement it kindly refer to the code repository.

For any further queries or anything related to Blockchain or Devops, you can DM me on LinkedIn or on my Instagram (id=acanubhav94).




Continue Learning