forked from atm1504/WhatsappBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessageReply.py
81 lines (74 loc) · 2.51 KB
/
messageReply.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
# 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
import requests
# Bot Info
url = 'url of your chat bot'
headers = {
'Authorization': 'Authentication key',
'Content-type': 'application/json',
}
driver = webdriver.Chrome('driver/chromedriver')
driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 600)
# Target user
target = "_amartya_"
time.sleep(10) # time to load the page
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()
# 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 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)))
input_box.send_keys(mssg + Keys.ENTER)
def getMessage(last_mssg):
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")
print(mssg_text)
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."
except:
n = len(mssg_text)
if n ==4:
lm_text = mssg_text[2]
elif n == 3 or n == 1:
lm_text = "Sorry emoji not supported"
else:
lm_text = mssg_text[0]
if lm_text == last_mssg:
time.sleep(1)
getMessage(last_mssg)
ans = getAnswer(lm_text)
sendMessage(ans)
time.sleep(1)
getMessage(lm_text)
print("ON")
time.sleep(5) # time to load contents
getMessage("")