Thought leadership from the most innovative tech companies, all in one place.

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:

image Bot performing auto replying and login

image

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

image 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 :

image 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:

image WhatsApp web after scanned

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

image

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

image Inspect element

image 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)

image 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:

image

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

image Code for sending automatic Hello's ,emotions and images

image

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

image Emotions

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

image Attachment symbol

image Selecting the relevant things to attach

Once you get the xPath of icon photo attachment:

image Script to attach the pics:

image 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