2
2
from selenium import webdriver
3
3
import json
4
4
import pickle
5
- from time import sleep
5
+ from time import sleep , time
6
6
import datetime
7
7
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"
9
12
continuetomessagexpath = r'//*[@id="action-button"]'
10
13
messagexpath = r'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]'
11
14
sendbtnxpath = r'//*[@id="main"]/footer/div[1]/div[3]'
12
- options = webdriver .ChromeOptions ();
15
+ options = webdriver .ChromeOptions ()
13
16
options .add_argument ('--user-data-dir=./User_Data' )
14
17
driver = webdriver .Chrome (executable_path = chromedriver ,chrome_options = options )
18
+ driver .get ("https://web.whatsapp.com/" )
15
19
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]" )))
16
30
app = Flask (__name__ )
17
31
18
32
@app .route ("/" )
@@ -21,7 +35,8 @@ def login():
21
35
return "sucess"
22
36
@app .route ("/api/<id_>/<api_key>/<no>/msg" ,methods = ["GET" ,"POST" ])
23
37
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 :
25
40
my_json = json .load (read )
26
41
if id_ in my_json .keys ():
27
42
if my_json [id_ ]== api_key :
@@ -30,28 +45,63 @@ def api(id_,api_key,no):
30
45
mymsg = request .args .get ('q' )
31
46
msg = mymsg
32
47
print (mymsg )
33
- url = f"https://web.whatsapp.com/send?phone={ no } &text&source&data&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 } &text&source&data&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 } &text&source&data&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)
34
88
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"]'
37
90
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 )))
45
93
# print("finded input box")
46
94
sleep (2 )
47
95
# print("Sending key")
48
96
input_box .send_keys (mymsg + Keys .ENTER )
97
+ t2 = time ()
49
98
with open ('chat_history.pkl' , 'rb' ) as fr :
50
99
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 )} })
52
101
with open ('chat_history.pkl' , 'wb' ) as fw :
53
102
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 )}]}
55
105
#{"status":"success","data":"sent"}
56
106
except Exception as e :
57
107
return {"status" :"error" ,"data" :"Failed " + str (e )}
@@ -61,12 +111,25 @@ def api(id_,api_key,no):
61
111
return {"status" :"error" ,"data" :"ID and KEY not Mached" }
62
112
@app .route ("/msghistory/<id_>/<api_key>/" )
63
113
def ChatHistory (id_ ,api_key ):
64
- with open ("api_json.json" ,"r" ) as read :
114
+ with open (r "api_json.json" ,"r" ) as read :
65
115
my_json = json .load (read )
66
116
if id_ in my_json .keys ():
67
117
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 :
69
119
chatlist = pickle .load (f )
70
120
return {"ChatHistory" :chatlist }
71
121
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"
72
135
app .run ()
0 commit comments