Skip to content

Commit 57adcd2

Browse files
Main Upload
1 parent 2552b48 commit 57adcd2

File tree

1 file changed

+81
-18
lines changed

1 file changed

+81
-18
lines changed

main.py

Lines changed: 81 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,31 @@
22
from selenium import webdriver
33
import json
44
import pickle
5-
from time import sleep
5+
from time import sleep,time
66
import datetime
77
from selenium.webdriver.common.keys import Keys
8-
chromedriver = "chromedriver.exe"
8+
from selenium.webdriver.common.by import By
9+
from selenium.webdriver.support.ui import WebDriverWait
10+
from selenium.webdriver.support import expected_conditions as ec
11+
chromedriver = r"chromedriver.exe"
912
continuetomessagexpath=r'//*[@id="action-button"]'
1013
messagexpath=r'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]'
1114
sendbtnxpath=r'//*[@id="main"]/footer/div[1]/div[3]'
12-
options = webdriver.ChromeOptions();
15+
options = webdriver.ChromeOptions()
1316
options.add_argument('--user-data-dir=./User_Data')
1417
driver = webdriver.Chrome(executable_path=chromedriver,chrome_options=options)
18+
driver.get("https://web.whatsapp.com/")
1519

20+
# with open(r"time.json","r")as time_file:
21+
# time_json=json.load(time_file)
22+
# try:
23+
# s_time=int(time_json["time"])
24+
# except:
25+
# s_time=20
26+
27+
28+
print(f"Waiting while we load the Whatsapp")
29+
WebDriverWait(driver,600).until(ec.visibility_of_element_located((By.XPATH,"/html/body/div[1]/div/div/div[3]/div/div[1]/div/label/div/div[2]")))
1630
app=Flask(__name__)
1731

1832
@app.route("/")
@@ -21,7 +35,8 @@ def login():
2135
return "sucess"
2236
@app.route("/api/<id_>/<api_key>/<no>/msg",methods=["GET","POST"])
2337
def api(id_,api_key,no):
24-
with open("api_json.json","r") as read:
38+
t1=time()
39+
with open(r"api_json.json","r") as read:
2540
my_json=json.load(read)
2641
if id_ in my_json.keys():
2742
if my_json[id_]==api_key:
@@ -30,28 +45,63 @@ def api(id_,api_key,no):
3045
mymsg=request.args.get('q')
3146
msg=mymsg
3247
print(mymsg)
33-
url=f"https://web.whatsapp.com/send?phone={no}&amp;text&amp;source&amp;data&amp;app_absent"
48+
if driver.current_url=="https://web.whatsapp.com/":
49+
try:
50+
search_box = driver.find_element_by_xpath('/html/body/div[1]/div/div/div[3]/div/div[1]/div/label/div/div[2]')#_2zCfw
51+
search_box.clear()
52+
search_box.send_keys(no)
53+
54+
driver.find_element_by_class_name("_3Pwfx").click()
55+
sleep(.1)
56+
except Exception as e :
57+
print(e,"find")
58+
url=f"https://web.whatsapp.com/send?phone={no}&amp;text&amp;source&amp;data&amp;app_absent"
59+
print("Message sending to",no,"With msg :-",msg)
60+
driver.get(url)
61+
with open(r"time.json","r")as time_file:
62+
time_json=json.load(time_file)
63+
try:
64+
sleep(int(time_json["time"]))
65+
except:
66+
sleep(25)
67+
else:
68+
url=f"https://web.whatsapp.com/send?phone={no}&amp;text&amp;source&amp;data&amp;app_absent"
69+
70+
driver.get(url)
71+
try:
72+
driver.find_element_by_class_name("_2xUEC _2XHG4").click()
73+
with open('chat_history.pkl', 'rb') as fr:
74+
chatlist = pickle.load(fr)
75+
chatlist.append({"Sent_By":id_,"msg":msg,"no":no,"datetime":str(datetime.datetime.now()),"Failed":True})
76+
with open('chat_history.pkl', 'wb') as fw:
77+
pickle.dump(chatlist, fw)
78+
t2=time()
79+
return {"return":False,"request_id":f"{len(chatlist)-1}","message":["Message not sent",{"time elacped":int(t2-t1)}],"error":"Mobile no not find"}
80+
except:
81+
pass
82+
# with open(r"time.json","r")as time_file:
83+
# time_json=json.load(time_file)
84+
# try:
85+
# sleep(int(time_json["time"]))
86+
# except:
87+
# sleep(25)
3488
print("Message sending to",no,"With msg :-",msg)
35-
driver.get(url)
36-
inp_xpath = '//div[@class="_2S1VP copyable-text selectable-text"][@contenteditable="true"][@data-tab="1"]'
89+
inp_xpath = r'//div[@class="_2S1VP copyable-text selectable-text"][@contenteditable="true"][@data-tab="1"]'
3790
inp_xpath=r'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]'
38-
with open("time.json","r")as time_file:
39-
time_json=json.load(time_file)
40-
try:
41-
sleep(int(time_json["time"]))
42-
except:
43-
sleep(25)
44-
input_box = driver.find_element_by_xpath(inp_xpath)
91+
# input_box = driver.find_element_by_xpath(inp_xpath)
92+
input_box=WebDriverWait(driver,600).until(ec.visibility_of_element_located((By.XPATH,inp_xpath)))
4593
# print("finded input box")
4694
sleep(2)
4795
# print("Sending key")
4896
input_box.send_keys(mymsg + Keys.ENTER)
97+
t2=time()
4998
with open('chat_history.pkl', 'rb') as fr:
5099
chatlist = pickle.load(fr)
51-
chatlist.append({"Sent_By":id_,"msg":msg,"no":no,"datetime":str(datetime.datetime.now())})
100+
chatlist.append({"Sent_By":id_,"msg":msg,"no":no,"datetime":str(datetime.datetime.now()),"Failed":False,"other":{"time elacped":int(t2-t1)}})
52101
with open('chat_history.pkl', 'wb') as fw:
53102
pickle.dump(chatlist, fw)
54-
return {"return":True,"request_id":"sk8vrucxl3hip5t","message":["Message sent successfully to NonDND numbers"]}
103+
104+
return {"return":True,"request_id":f"{len(chatlist)-1}","message":["Message sent successfully",{"time elacped":int(t2-t1)}]}
55105
#{"status":"success","data":"sent"}
56106
except Exception as e:
57107
return {"status":"error","data":"Failed " + str(e)}
@@ -61,12 +111,25 @@ def api(id_,api_key,no):
61111
return {"status":"error","data":"ID and KEY not Mached"}
62112
@app.route("/msghistory/<id_>/<api_key>/")
63113
def ChatHistory(id_,api_key):
64-
with open("api_json.json","r") as read:
114+
with open(r"api_json.json","r") as read:
65115
my_json=json.load(read)
66116
if id_ in my_json.keys():
67117
if my_json[id_]==api_key:
68-
with open('chat_history.pkl', 'rb') as f:
118+
with open(r'chat_history.pkl', 'rb') as f:
69119
chatlist = pickle.load(f)
70120
return {"ChatHistory":chatlist}
71121
return "Some Error Occurs Cheak Your Id and API KEY"
122+
@app.route("/msghistory/<id_>/<api_key>/<msg_id>")
123+
def ChatHistory_by_id(id_,api_key,msg_id):
124+
with open(r"api_json.json","r") as read:
125+
my_json=json.load(read)
126+
if id_ in my_json.keys():
127+
if my_json[id_]==api_key:
128+
with open(r'chat_history.pkl', 'rb') as f:
129+
chatlist = pickle.load(f)
130+
try:
131+
return {f"ChatHistory for Message id {msg_id}":chatlist[int(msg_id)]}
132+
except:
133+
return f"ChatHistory for Message id {msg_id} is not avilable Cheack your id"
134+
return "Some Error Occurs Cheak Your Id and API KEY"
72135
app.run()

0 commit comments

Comments
 (0)