-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnewMessageReply.py
92 lines (85 loc) · 2.92 KB
/
newMessageReply.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
# The following script has been developed by Amartya Mondal.
# For more details visit https://atm1504.in
# Follow me at https://github.com/atm1504
import json
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 time
from datetime import datetime
driver = webdriver.Chrome('driver/chromedriver')
driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 600)
# Bot Info
url = 'url of your chat bot'
headers = {
'Authorization': 'Authentication key',
'Content-type': 'application/json',
}
# This function fetches data from the bot.
def getAnswer(query):
try:
print(query)
data = "{'question':'" + query + "'}"
response = requests.post(url, headers=headers, data=data)
ans_text = json.loads(response.text)
entire_response = ans_text["answers"]
ans = entire_response[0]["answer"]
print(ans)
return ans
except:
return query
def getMessage():
base_mssg_path = '//*[contains(concat( " ", @class, " " ), concat( " ", "message-in", " " ))]'
all_mssgs = driver.find_elements_by_xpath(base_mssg_path)
lm = all_mssgs[-1]
mssg_text = lm.text.split("\n")
try:
replied = lm.find_element_by_class_name("_3FXB1")
if str(replied.text) != "You · Status":
raise Exception("Not status")
lm_text = "Thank you for replying to my status."
print("Is status")
except:
print("Is not status")
print(mssg_text)
lm_text = ""
if len(mssg_text) > 2:
if mssg_text[0] != "You":
lm_text += "Hey " + mssg_text[0]
lm_text += mssg_text[2]
else:
lm_text += mssg_text[0]
# Get answer of the query
ans = getAnswer(lm_text)
sendMessage(ans)
def sendMessage(mssg):
inp_xpath = '//div[@class="_2S1VP copyable-text selectable-text"][@dir="ltr"][@data-tab="1"]'
input_box = wait.until(EC.presence_of_element_located((By.XPATH, inp_xpath)))
# mssg = "Thank you for texting. I will reach u soon."
input_box.send_keys(mssg + Keys.ENTER)
print(mssg)
def replyTarget(target):
x_arg = '//span[contains(@title,' + target + ')]'
print(x_arg)
group_title = wait.until(EC.presence_of_element_located((By.XPATH, x_arg)))
group_title.click()
getMessage()
def getTarget():
path = '//div[@class="_3j7s9"]'
all_chats = driver.find_elements_by_xpath(path)
for chat in all_chats:
try:
chat.find_element_by_class_name("OUeyt")
target_element = chat.find_element_by_class_name("_3TEwt")
target = "'" + target_element.text+"'"
print(target)
replyTarget(target)
except:
continue
time.sleep(1)
getTarget()
time.sleep(5)
getTarget()