
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
WhatsApp Using Python
In this section we are going to create a Whatsapp chatbots, but unlike few other chatbots for twitter or facebook, whatsapp chatbots don’t run on the platform directly because of whatsapp’s policies.
But there is a way to get is done, using selenium, a very smart package in python with which developer’s can automate the browser’s activity. With this we can make use of whatsapp-web through the browser.
Requirements
We need three basic things to get things done: Selenium.
We can install selenium very easily using pip, just run below command on your terminal −
$pip install selenium
-
Chrome/firefox or any other webdriver.
As I using chrome webdriver, below is a link to download chrome webdriver as per your os.
https://chromedriver.storage.googleapis.com/index.html?path=2.46/
-
Whatsapp account.
Need to have Whatsapp account if not create one.
Below is a simple program to send whatsapp message using python to specific contacts.
Example
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By import time import sys # Replace below path with the absolute path of the \ #chromedriver in your computer driver = webdriver.Chrome(r'c:\users\rajesh\Desktop\chromedriver') driver.get("https://web.whatsapp.com/") # time.sleep() wait = WebDriverWait(driver, 600) # Replace 'My Bsnl' with the name of your friend or group name target = '"My Bsnl"' # Replace the below string with your own message string = sys.argv[1] x_arg = '//span[contains(@title,' + target + ')]' group_title = wait.until(EC.presence_of_element_located(( By.XPATH, x_arg))) print (group_title) print ("Wait for few seconds") group_title.click() message = driver.find_elements_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')[0] message.send_keys(string) sendbutton = driver.find_elements_by_xpath('//*[@id="main"]/footer/div[1]/div[3]/button')[0] sendbutton.click() driver.close()
Let’s run the above scripts on command prompt, passing message as argument to the whatsapp contact−
>python whatsppPython.py "Hello" DevTools listening on ws://127.0.0.1:12954/devtools/browser/a5bb04bd-66a3-4002-999f-6a0824f591da <selenium.webdriver.remote.webelement.WebElement (session="83e7034b9a6f6b49e9e422e655f270d3", element="0.30994636046479007-1")> after wait …. …..
chrome browser will open, with a screen something like −
On your mobile device, Choose whatsapp web from the top bar in whatsapp. Scan the QR code that appears on the screen.
There we can see the message is send to a specific contact (“My Bsnl”) in our case.