Business Modeling in Hyperledger
October 10, 2019Enterprise Blockchain Use cases and implementaions
November 10, 2020WhatsApp bot for Auto replying & sending images via Python and Selenium
Author : Anubhav Chaturvedi
Are you the one who wants to send Lots of Quotes or Shayaris to your Ex or to a whatsApp friends or colleague group or random emojis to your wife or Latest gf? This blog will help you out in programming a bot to impress them.
Here comes the demo:
For implementing this you will need Jupyter Python notebooks installed .You can use this procedure illustrated over here for installation on windows or Linux :
How To Set Up Jupyter Notebook with Python 3 on Ubuntu 18.04 | DigitalOceanThis tutorial will walk you through setting up Jupyter Notebook to run from an Ubuntu 18.04 server, as well as teach…www.digitalocean.comAlso 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
In order to open Google Chrome from Jupyter notebook automatically we will use “chrome” element as an object from webdriver package and give it relevant URL to open (Web.whatsapp.com for WhatsApp web)
chrome = webdriver.Chrome(ChromeDriverManager().install())
chrome.get
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 do “inspect element”
Inspect element
Element class name for Search bar
We can see that 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