-
Notifications
You must be signed in to change notification settings - Fork 91
/
whatsapp.py
137 lines (120 loc) · 5.07 KB
/
whatsapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Note: For proper working of this Script Good and Uninterepted Internet Connection is Required
# Keep all contacts unique
# Can save contact with their phone Number
# Import required packages
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 datetime
import time
import openpyxl as excel
# function to read contacts from a text file
def readContacts(fileName):
lst = []
file = excel.load_workbook(fileName)
sheet = file.active
firstCol = sheet['A']
for cell in range(len(firstCol)):
contact = str(firstCol[cell].value)
contact = "\"" + contact + "\""
lst.append(contact)
return lst
# Target Contacts, keep them in double colons
# Not tested on Broadcast
targets = readContacts("contacts.xlsx")
# can comment out below line
print(targets)
# Driver to open a browser
driver = webdriver.Firefox()
#link to open a site
driver.get("https://web.whatsapp.com/")
# 10 sec wait time to load, if good internet connection is not good then increase the time
# units in seconds
# note this time is being used below also
wait = WebDriverWait(driver, 10)
wait5 = WebDriverWait(driver, 5)
input("Scan the QR code and then press Enter")
# Message to send list
# 1st Parameter: Hours in 0-23
# 2nd Parameter: Minutes
# 3rd Parameter: Seconds (Keep it Zero)
# 4th Parameter: Message to send at a particular time
# Put '\n' at the end of the message, it is identified as Enter Key
# Else uncomment Keys.Enter in the last step if you dont want to use '\n'
# Keep a nice gap between successive messages
# Use Keys.SHIFT + Keys.ENTER to give a new line effect in your Message
msgToSend = [
[12, 32, 0, "Hello! This is test Msg. Please Ignore." + Keys.SHIFT + Keys.ENTER + "http://bit.ly/mogjm05"]
]
# Count variable to identify the number of messages to be sent
count = 0
while count<len(msgToSend):
# Identify time
curTime = datetime.datetime.now()
curHour = curTime.time().hour
curMin = curTime.time().minute
curSec = curTime.time().second
# if time matches then move further
if msgToSend[count][0]==curHour and msgToSend[count][1]==curMin and msgToSend[count][2]==curSec:
# utility variables to tract count of success and fails
success = 0
sNo = 1
failList = []
# Iterate over selected contacts
for target in targets:
print(sNo, ". Target is: " + target)
sNo+=1
try:
# Select the target
x_arg = '//span[contains(@title,' + target + ')]'
try:
wait5.until(EC.presence_of_element_located((
By.XPATH, x_arg
)))
except:
# If contact not found, then search for it
searBoxPath = '//*[@id="input-chatlist-search"]'
wait5.until(EC.presence_of_element_located((
By.ID, "input-chatlist-search"
)))
inputSearchBox = driver.find_element_by_id("input-chatlist-search")
time.sleep(0.5)
# click the search button
driver.find_element_by_xpath('/html/body/div/div/div/div[2]/div/div[2]/div/button').click()
time.sleep(1)
inputSearchBox.clear()
inputSearchBox.send_keys(target[1:len(target) - 1])
print('Target Searched')
# Increase the time if searching a contact is taking a long time
time.sleep(4)
# Select the target
driver.find_element_by_xpath(x_arg).click()
print("Target Successfully Selected")
time.sleep(2)
# Select the Input Box
inp_xpath = "//div[@contenteditable='true']"
input_box = wait.until(EC.presence_of_element_located((
By.XPATH, inp_xpath)))
time.sleep(1)
# Send message
# target is your target Name and msgToSend is you message
input_box.send_keys("Hello, " + target + "."+ Keys.SHIFT + Keys.ENTER + msgToSend[count][3] + Keys.SPACE) # + Keys.ENTER (Uncomment it if your msg doesnt contain '\n')
# Link Preview Time, Reduce this time, if internet connection is Good
time.sleep(10)
input_box.send_keys(Keys.ENTER)
print("Successfully Send Message to : "+ target + '\n')
success+=1
time.sleep(0.5)
except:
# If target Not found Add it to the failed List
print("Cannot find Target: " + target)
failList.append(target)
pass
print("\nSuccessfully Sent to: ", success)
print("Failed to Sent to: ", len(failList))
print(failList)
print('\n\n')
count+=1
driver.quit()