-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauto_reply.py
304 lines (221 loc) · 7.55 KB
/
auto_reply.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import csv
import random
import smtplib
import time as t
from difflib import get_close_matches
from bs4 import BeautifulSoup as soup
from selenium import webdriver
def sort_replies():
data = []
with open('replies_data.csv') as data_file:
reader = csv.reader(data_file)
for row in reader:
data.append(row)
with open('replies_data.csv', 'w') as data_file:
writer = csv.writer(data_file, delimiter=',')
for row in data:
writer.writerow(sorted(filter(lambda x: x != '', row)))
def read_msgs():
whatsapp_ps = driver.page_source
page_soup = soup(whatsapp_ps, "html.parser")
right_frame = page_soup.findAll("div", {"class": "_3HZor _2rI9W"})[-1]
messages = right_frame.findAll("div", {"class": "_1ays2"})[-1]
all_msgs_container = messages.findAll("div", recursive=False)
msgs = []
for msg_container in all_msgs_container[::-1]:
# print('msg_container : ', msg_container["class"])
if msg_container["class"][0] == "_3Xx0y":
continue
if msg_container["class"][1] == "message-out":
break
last_msg_container = msg_container.findAll("div", {"class": "_12pGw EopGb"})
try:
msg = last_msg_container[0].span.span.text
if msg == '':
msg = last_msg_container[0].span.span.img["alt"]
except IndexError:
msg = "Sorry! I can only understand text"
# print('msg : ', msg)
msgs.append(msg)
if len(msgs) == 0:
return ['']
return msgs
def send_reply(msg):
msg_box = driver.find_element_by_class_name('_3u328')
msg_box.send_keys(msg)
while True:
try:
button = driver.find_element_by_class_name('_3M-N-')
button.click()
break
except:
t.sleep(0.01)
def wait_typing(name):
flag = False
while True:
print(name, 'is typing')
whatsapp_ps = driver.page_source
page_soup = soup(whatsapp_ps, "html.parser")
typing = page_soup.findAll("span", {"class": "_315-i"})
try:
if typing[0].text == 'typing…':
flag = False
else:
if flag:
return
flag = True
t.sleep(3)
except IndexError:
return
t.sleep(1)
def wait_new_msgs():
page = driver.page_source
while page == driver.page_source:
t.sleep(0.1)
def check_new_msgs():
whatsapp_ps = driver.page_source
page_soup = soup(whatsapp_ps, "html.parser")
frnds = []
left_frame = page_soup.findAll("div", {"class": "X7YrQ"})
for container in left_frame:
frnd_container = container.findAll("span", {"class": "P6z4j"})
if frnd_container:
name_container = container.find("span", {"class": "_19RFN _1ovWX"})
name = name_container["title"]
frnds.append(name)
return frnds
def convert_data():
inputs = []
outputs = []
with open('replies_data.csv') as data_file:
reader = csv.reader(data_file)
flag = True
for row in reader:
if flag:
inputs.append(set(row))
flag = False
else:
outputs.append(row)
flag = True
return inputs, outputs
def send_mail(name, msg):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('abhijeetarabhi@gmail.com', 'vlkwpprmfrdmkrfb')
subject = 'Unknown message from ' + name
body = 'Message is ' + msg
message = f'Subject : {subject}\n\n{body}\n'
server.sendmail('abhijeetarabhi@whatsapp.com', ['abhijeet_abhi@live.co.uk'],
message)
print('email sent')
def prev_sent():
whatsapp_ps = driver.page_source
page_soup = soup(whatsapp_ps, "html.parser")
right_frame = page_soup.findAll("div", {"class": "_3HZor _2rI9W"})[-1]
messages = right_frame.findAll("div", {"class": "_1ays2"})[-1]
last_msgs_day_container = messages.findAll("div", {"class": "FTBzM message-out"})
last_msg_container = last_msgs_day_container[-1].findAll("div", {"class": "_12pGw EopGb"})
last_msg = last_msg_container[0].span.span.text
print(last_msg)
return last_msg
'''
def gen_reply(name, msg, inputs, outputs):
temp_msg = ''
for m in msg.lower().strip():
if m.isalpha() or m == ' ':
temp_msg += m
msg = temp_msg
print('msg', msg)
if msg in inputs[0]:
prev = prev_sent()
print(prev)
if prev.lower() in inputs[0]:
return 'ssup?'
for i in range(len(inputs)):
if msg in inputs[i]:
rand_ind = random.randrange(0, len(outputs[i]))
reply = outputs[i][rand_ind]
# print('returning reply')
return reply
print('Unknown message: ' + msg + ' from: ' + name)
send_mail(name, msg)
return
'''
def gen_reply(name, msg):
with open('replies_data.csv') as data_file:
msg = msg.lower()
reader = csv.reader(data_file)
ans = False
flag = True
reps = []
for row in reader:
if flag:
temp = get_close_matches(msg, row)
# print(temp, row)
if temp:
print('matches : ', temp, ' ', row)
reps.append([temp[-1][0], temp[-1]])
# print(reps)
ans = True
flag = False
else:
if ans:
rand_ind = random.randrange(0, len(row))
reply = row[rand_ind]
reps[-1].append(reply)
flag = True
max_match = [0, '']
for r in reps:
if r[1] == msg:
return r[2]
if max_match[0] < r[0]:
max_match = [r[0], r[2]]
return max_match[1]
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=./User_Data')
driver = webdriver.Chrome('/Users/AR/Documents/Programming/Python/Pycharm/whatsapp_bot/chromedriver',
options=options)
driver.get('https://web.whatsapp.com/')
sort_replies()
# inputs, outputs = convert_data()
best_frnds = ['Banda', 'Ainesh', 'Dad', 'Mom', 'Indu', 'Amitha']
t.sleep(2)
try:
while True:
frnds = check_new_msgs()
# print(list(filter(lambda f: f in best_frnds, frnds)))
if list(filter(lambda f: f in best_frnds, frnds)) == []:
# print('waiting for new messges')
wait_new_msgs()
for name in filter(lambda f: f in best_frnds, frnds):
while True:
try:
frnd = driver.find_element_by_xpath('//span[@title = "{}"]'.format(name))
frnd.click()
break
except:
print('loading')
t.sleep(1)
t.sleep(0.05)
wait_typing(name)
while True:
try:
curr_msgs = read_msgs()
break
except IndexError:
t.sleep(1)
print('Messages: ', curr_msgs)
if curr_msgs[0] == '':
t.sleep(1)
continue
for msg in curr_msgs[::-1]:
# print(msg)
reply = gen_reply(name, msg)
if reply:
# print(reply)
send_reply(reply)
print('Reply : ', reply)
except KeyboardInterrupt:
driver.close()