-
Notifications
You must be signed in to change notification settings - Fork 2
/
whatsapp_automate_feature.py
173 lines (140 loc) · 5.56 KB
/
whatsapp_automate_feature.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from word2number import w2n
from time import sleep
from respones_data import *
import os
from prompt_toolkit import prompt
from colorama import Fore
def is_convertible(word):
try:
w2n.word_to_num(word)
return True
except ValueError:
return False
def convert_to_numbers(input_string):
for word in input_string.split():
if is_convertible(word):
return str(w2n.word_to_num(word))
return None
def whatsapp_config():
script_directory = os.path.dirname(os.path.abspath(__file__))
user_data_dir = os.path.join(script_directory,"seleniumBrowser_data")
options = Options()
options.add_argument(f"--user-data-dir={user_data_dir}")
options.add_argument(f"--profile-directory=Profile")
driver_manager = ChromeDriverManager() # *selenium and webdriver config function
chrome_service = Service(driver_manager.install())
driver = webdriver.Chrome(service=chrome_service, options=options)
driver.get("https://web.whatsapp.com")
return driver
def number_of_person(speak,ttsoutput):
done = True
while done == True:
try:
speak("how many people or group you want to send message")
num_person = ttsoutput()
num_person = int(convert_to_numbers(num_person))
done = False
except Exception:
done = True
return num_person
def send_message(speak, driver, person, message):
Exception_condi = False
try:
welement = WebDriverWait(driver, 20).until(
EC.presence_of_element_located(
(By.XPATH, '//*[@id="side"]/div[1]/div/div[2]/div[2]/div/div[1]/p')
)
)
welement.send_keys(person)
welement.send_keys(Keys.ENTER)
msg_element = driver.find_element(
By.XPATH,
'//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div/p',
)
msg_element.send_keys(message)
msg_element.send_keys(Keys.ENTER)
sleep(1)
welement.send_keys(Keys.CONTROL + "a")
welement.send_keys(Keys.DELETE)
except Exception:
Exception_condi = True
if Exception_condi == True:
welement = WebDriverWait(driver, 20).until(
EC.presence_of_element_located(
(By.XPATH, '//*[@id="side"]/div[1]/div/div[2]/div[2]/div/div[1]/p')
)
)
welement.send_keys(Keys.CONTROL + "a")
welement.send_keys(Keys.BACKSPACE)
pass
def schedule_and_send_Message(person_list, speak):
driver = whatsapp_config()
for person in dict.keys(person_list):
message = person_list[person]
send_message(speak, driver, person, message)
sleep(3)
driver.close()
def Bulk_message(speak,suggest_message,ttsoutput):
driver, num_persons = whatsapp_config(), number_of_person(speak)
Exception_condi = False
done = False
while done != True:
speak("type the message")
suggestext=suggest_message("write short message on topic")
message = prompt(f"Enter the message: ",default=suggestext)
print(Fore.CYAN+"message saved")
speak("should i confirm the message")
confirm_message = ttsoutput()
confirm_message = confirm_message.lower()
if all(word in confirm_message for word in ["confirm", "message"]) or any(
word in confirm_message for word in yes_words
):
done = True
elif all(word in confirm_message for word in ["clear", "message"]) or any(
word in confirm_message for word in no_words
):
message = ""
recivers_names = []
speak("type the reciver names")
for num_person in range(num_persons):
recivers_name = input(Fore.GREEN+f"person {num_person+1}:\t")
recivers_names.append(recivers_name)
for person in recivers_names:
try:
welement = WebDriverWait(driver, 20).until(
EC.presence_of_element_located(
(By.XPATH, '//*[@id="side"]/div[1]/div/div[2]/div[2]/div/div[1]/p')
)
)
welement.send_keys(person)
welement.send_keys(Keys.ENTER)
msg_element = driver.find_element(
By.XPATH,
'//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div/p',
)
msg_element.send_keys(message)
msg_element.send_keys(Keys.ENTER)
sleep(1)
welement.send_keys(Keys.CONTROL + "a")
welement.send_keys(Keys.DELETE)
except Exception:
Exception_condi = True
if Exception_condi == True:
print(Fore.RED+f"{person} not found")
welement = WebDriverWait(driver, 20).until(
EC.presence_of_element_located(
(By.XPATH, '//*[@id="side"]/div[1]/div/div[2]/div[2]/div/div[1]/p')
)
)
welement.send_keys(Keys.CONTROL + "a")
welement.send_keys(Keys.BACKSPACE)
sleep(3)
driver.close()