Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit c4f2fa2

Browse files
authored
Merge pull request #116 from Namyalg/Sending-whatsapp-messages-from-Commandline
Script to automate sending WhatsApp messages
2 parents 2eec994 + cd47c18 commit c4f2fa2

File tree

9 files changed

+161
-0
lines changed

9 files changed

+161
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Automate-sending-WhatsApp-messages
2+
3+
- This script can be used to send messages to your contacts on WhatsApp from your Command line!!
4+
5+
## Working ##
6+
- The URL, https://web.whatsapp.com/ needs to be opened and initiate.
7+
8+
![Image](qrcode.PNG)
9+
10+
- On the right side there is a QR code which needs to be scanned in order to open WhatsApp over the web.
11+
12+
![Image](search.PNG)
13+
14+
- The icon circled in the image is can be used to look for contacts/groups
15+
- The script is automated to click on the search button (circled button)
16+
17+
![Image](frequent.PNG)
18+
19+
- On clicking the button, it opens into the above image.
20+
- Further, the name can be typed into the search bar. The ENTER key will be pressed by the script.
21+
- The chat of the entered contact will be opened.
22+
23+
![Image](abcd.PNG)
24+
25+
- The script looks as shown below
26+
- Messages can be sent to more than one contact,
27+
- More than one message can be sent to the same contact
28+
29+
![Image](script.PNG)
30+
31+
![Image](confirm.png)
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python3
2+
#Import and dependencies
3+
4+
from selenium import webdriver
5+
import time
6+
from selenium.webdriver.common.keys import Keys
7+
8+
#Constants
9+
three = 3
10+
twenty-five = 25
11+
one = 1
12+
13+
#Here the process of automation is achieved by using the framework Selenium.
14+
#Selenium is a portable framework for testing and automating web applications web applications
15+
16+
#Path to chromedriver.exe
17+
chrome_path = r"C:\Users\giril\AppData\Local\Programs\Python\Python36-32\chromedriver.exe"
18+
19+
#Initiating and setting up the driver
20+
driver = webdriver.Chrome(chrome_path)
21+
22+
URL = "https://web.whatsapp.com/"
23+
driver.get(URL)
24+
25+
#The script is made to wait, to ensure that the page is loaded
26+
time.sleep(twenty-five)
27+
28+
def send_messages():
29+
30+
chats = int(input("Enter the number of personal chats/groups you would want to message "))
31+
32+
for chat in range(chats):
33+
34+
#This is used to enable the user to search for the contact
35+
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[3]/div/header/div[2]/div/span/div[2]/div').click()
36+
37+
name = input("Enter name to whom you want to send a message ")
38+
time.sleep(three)
39+
40+
#On entering the contact, the name is entered into the search bar
41+
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div[1]/span/div/span/div/div[1]/div/label/div/div[2]').send_keys(name)
42+
time.sleep(three)
43+
44+
#The contact's chat will be opened
45+
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div[1]/span/div/span/div/div[1]/div/label/div/div[2]').send_keys(Keys.ENTER)
46+
47+
#This enables the user to send more than one message to the same contact
48+
done = one
49+
while done:
50+
message = input("Enter the message you want to enter ")
51+
52+
#The cursor is positioned and the chat box is activated
53+
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div[2]/div/div[2]').send_keys(message)
54+
time.sleep(three)
55+
56+
#On clicking the send button, a message is sent to the contact
57+
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div[3]/button').click()
58+
59+
print("Would you want to send another message to the same contact ?")
60+
61+
done = int(input("Enter 1 to continue, 0 to stop "))
62+
63+
print("All messages sent successfully")
64+
65+
send_messages()
Loading
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python3
2+
#Import and dependencies
3+
4+
from selenium import webdriver
5+
import time
6+
from selenium.webdriver.common.keys import Keys
7+
8+
#Constants
9+
three = 3
10+
twenty-five = 25
11+
one = 1
12+
13+
#Here the process of automation is achieved by using the framework Selenium.
14+
#Selenium is a portable framework for testing and automating web applications web applications
15+
16+
#Path to chromedriver.exe
17+
chrome_path = r"C:\Users\giril\AppData\Local\Programs\Python\Python36-32\chromedriver.exe"
18+
19+
#Initiating and setting up the driver
20+
driver = webdriver.Chrome(chrome_path)
21+
22+
URL = "https://web.whatsapp.com/"
23+
driver.get(URL)
24+
25+
#The script is made to wait, to ensure that the page is loaded
26+
time.sleep(twenty-five)
27+
28+
def send_messages():
29+
30+
chats = int(input("Enter the number of personal chats/groups you would want to message "))
31+
32+
for chat in range(chats):
33+
34+
#This is used to enable the user to search for the contact
35+
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[3]/div/header/div[2]/div/span/div[2]/div').click()
36+
37+
name = input("Enter name to whom you want to send a message ")
38+
time.sleep(three)
39+
40+
#On entering the contact, the name is entered into the search bar
41+
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div[1]/span/div/span/div/div[1]/div/label/div/div[2]').send_keys(name)
42+
time.sleep(three)
43+
44+
#The contact's chat will be opened
45+
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div[1]/span/div/span/div/div[1]/div/label/div/div[2]').send_keys(Keys.ENTER)
46+
47+
#This enables the user to send more than one message to the same contact
48+
done = one
49+
while done:
50+
message = input("Enter the message you want to enter ")
51+
52+
#The cursor is positioned and the chat box is activated
53+
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div[2]/div/div[2]').send_keys(message)
54+
time.sleep(three)
55+
56+
#On clicking the send button, a message is sent to the contact
57+
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div[3]/button').click()
58+
59+
print("Would you want to send another message to the same contact ?")
60+
61+
done = int(input("Enter 1 to continue, 0 to stop "))
62+
63+
print("All messages sent successfully")
64+
65+
send_messages()

0 commit comments

Comments
 (0)