-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
158 lines (134 loc) · 5.4 KB
/
app.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import autoit
import time
import datetime
import os
import openpyxl
import pyfiglet
choice = None
medianame = None
docChoice = None
doc_filename = None
print("\n\nDeveloped By :")
print(pyfiglet.figlet_format("mOdEL^", font = "slant" ))
unsaved_Contacts=[]
error_contacts=[]
retry=0
path_excel_file="numbers.xlsx"
wb = openpyxl.load_workbook(path_excel_file)
sheet = wb['Sheet1']
col = sheet['A']
for cell in col:
time.sleep(1)
inp = '"' + str(cell.value) + '"'
unsaved_Contacts.append(inp)
print(unsaved_Contacts)
choice = input("Would you like to send attachment(yes/no): ")
if(choice == "yes"):
# Note the media files should be present in the Media Folder
medianame = input("Enter the Media file name you want to send with extensions: ")
docChoice = input("Would you file to send a Document file(yes/no): ")
if(docChoice == "yes"):
# Note the document file should be present in the Document Folder
doc_filename = input("Enter the Document file name you want to send: ")
def send_files():
global doc_filename
# Attachment Drop Down Menu
clipButton = driver.find_element_by_xpath('//*[@id="main"]/header/div[3]/div/div[2]/div/span')
clipButton.click()
time.sleep(3)
# To send a Document(PDF, Word file, PPT)
docButton = driver.find_element_by_xpath('//*[@id="main"]/header/div[3]/div/div[2]/span/div/div/ul/li[3]/button')
docButton.click()
time.sleep(3)
docPath = os.getcwd() + "\\Documents\\" + doc_filename
autoit.control_focus("Open", "Edit1")
autoit.control_set_text("Open", "Edit1", (docPath))
autoit.control_click("Open", "Button1")
time.sleep(5)
whatsapp_send_button = driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/span/div/div/span')
whatsapp_send_button.click()
def send_attachment():
global medianame
# Attachment Drop Down Menu
clipButton = driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[1]/div[2]/div/div/span')
clipButton.click()
time.sleep(3)
# To send Videos and Images.
mediaButton = driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[1]/div[2]/div/span/div[1]/div/ul/li[1]/button')
mediaButton.click()
time.sleep(3)
image_path = os.getcwd() + "\\Media\\" + medianame
autoit.control_focus("Open", "Edit1")
autoit.control_set_text("Open", "Edit1", image_path)
autoit.control_click("Open", "Button1")
time.sleep(3)
whatsapp_send_button = driver.find_element_by_xpath('//*[@id="app"]/div[1]/div[1]/div[2]/div[2]/span/div[1]/span/div[1]/div/div[2]/span/div/div/span')
whatsapp_send_button.click()
try:
driver = webdriver.Chrome("chromedriver.exe")
def send_unsaved_contact_message():
message = input('Enter your message : ')
# message = "" #Hardcode here
try:
time.sleep(8)
input_box = driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[1]/div/div[2]')
for ch in message:
if ch == "\n":
ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.ENTER).key_up(Keys.SHIFT).key_up(Keys.BACKSPACE).perform()
else:
input_box.send_keys(ch)
input_box.send_keys(Keys.ENTER)
time.sleep(5)
driver.find_element_by_class_name("web").send_keys(Keys.ENTER)
print("Message sent successfuly")
return True
except NoSuchElementException:
print("Failed to send message no Element.")
return False
for i in unsaved_Contacts:
link = "https://web.whatsapp.com/send?phone={}&text&source&data&app_absent".format(i)
driver.get(link)
print("Sending message to", i)
time.sleep(3)
tt=send_unsaved_contact_message()
if(tt):
continue
else:
while(True):
retry +=1
print("Retry Attempt : "+str(retry))
link = "https://web.whatsapp.com/send?phone={}&text&source&data&app_absent".format(i)
driver.get(link)
print("Sending message to", i)
ttr=send_unsaved_contact_message()
if (ttr):
time.sleep(3)
break
elif(retry == 3):
retry=0
error_contacts.append(i)
break
time.sleep(3)
if(choice == "yes"):
try:
send_attachment()
except:
print('Attachment not sent.')
if(docChoice == "yes"):
try:
send_files()
except:
print('Files not sent')
time.sleep(3)
print("Error Contacts List : ",error_contacts)
time.sleep(4)
except Exception as e:
print(e)