1+ '''This module driver.py contains the WappDriver class, which is responsible for driving the core
2+ features of the application.
3+ You have to create an instance of the WappDriver class, to do any meaningful activity such as
4+ sending a text message or media(Image/GIF/Video) or PDF document
5+ '''
6+
17from .util import first_time_set_up , convey
2- from . import update as up
8+ from .update import chrome_driver_path , local_varVer_val , var , update_cdp
39
410from selenium import webdriver
511from selenium .webdriver .chrome .options import Options
612from selenium .webdriver .support .ui import WebDriverWait
713from selenium .webdriver .common .by import By
8- from selenium .webdriver .support import expected_conditions as expCond
14+ from selenium .webdriver .support import expected_conditions
915from selenium .webdriver .common .keys import Keys
1016
1117import yaml
1218
1319
1420class WappDriver ():
21+ ''' The WappDriver class serves as an unofficial API to WhatsApp.
22+ It interacts with the webdriver to send messages
23+ '''
1524
1625 def __init__ (self , session = 'wappDefaultSession' , timeout = 100 ):
1726
18- if up . local_varVer_val == 0.0 :
19- first_time_set_up (up )
27+ if local_varVer_val == 0.0 :
28+ first_time_set_up ()
2029
21- self .chrome_driver_path = open (up . chrome_driver_path ).readline ()
22- with open (up . var ) as file :
30+ self .chrome_driver_path = open (chrome_driver_path ).readline ()
31+ with open (var ) as file :
2332 _var = yaml .full_load (file )
2433
2534 self .whatsapp_web_url = _var ['whatsapp_web_url' ]
26- self .mainScreenLOaded = _var ['mainScreenLOaded ' ]
35+ self .mainScreenLoaded = _var ['mainScreenLoaded ' ]
2736 self .searchSelector = _var ['searchSelector' ]
2837 self .mBox = _var ['mBox' ]
2938
@@ -40,73 +49,119 @@ def __init__(self, session='wappDefaultSession', timeout=100):
4049 self .driver .quit ()
4150
4251 def load_chrome_driver (self , session , tried = 0 ):
43- while tried <= 3 :
44- try :
45- chrome_options = Options ()
46- chrome_options .add_argument (f'--user-data-dir={ session } ' )
47- self .driver = webdriver .Chrome (
48- options = chrome_options , executable_path = self .chrome_driver_path )
49- return True
50- except Exception as error :
51- tried += 1
52- message = f""" Chrome Driver could not be successfuly loaded
53- Make sure that you have latest and matching versions of Chrome and Chrome Driver
52+
53+ try :
54+ chrome_options = Options ()
55+ chrome_options .add_argument (f'--user-data-dir={ session } ' )
56+ self .driver = webdriver .Chrome (
57+ options = chrome_options , executable_path = self .chrome_driver_path )
58+ return True
59+
60+ except Exception as error :
61+ message = f''' Chrome Driver could not be successfuly loaded
62+ Make sure that you have latest and matching versions of Chrome and Chrome Driver
5463 CHROME DRIVER INSTALLATION PATH IS INVALID !!
55- """
56- convey (error , message )
57- up . update_cdp ()
64+ '''
65+ convey (error , message )
66+ update_cdp ()
5867
5968 return False
6069
6170 def load_main_screen (self ):
6271 try :
6372 self .driver .get (self .whatsapp_web_url )
6473 WebDriverWait (self .driver , self .timeout ).until (
65- expCond .presence_of_element_located ((By .CSS_SELECTOR , self .mainScreenLOaded )))
74+ expected_conditions .presence_of_element_located ((By .CSS_SELECTOR , self .mainScreenLoaded )))
6675 return True
76+
6777 except Exception as error :
68- message = " Could not load main screen of WhatsApp Web because of some errors, make sure to Scan QR"
78+ message = ' Could not load main screen of WhatsApp Web because of some errors, make sure to Scan QR'
6979 convey (error , message )
70-
7180 return False
7281
7382 # selecting a person after searching contacts
74- def load_selected_person (self , name ):
75-
83+ def load_person (self , name ):
7684 search_box = self .driver .find_element_by_css_selector (
7785 self .searchSelector )
7886
7987 # we will send the name to the input key box
8088 search_box .send_keys (name )
8189
8290 try :
83- person = WebDriverWait (self .driver , self .timeout ).until (expCond .presence_of_element_located (
91+ person = WebDriverWait (self .driver , self .timeout ).until (expected_conditions .presence_of_element_located (
8492 (By .XPATH , f'//*[@title="{ name } "]' )))
8593 person .click ()
8694 return True
8795
8896 except Exception as error :
89- message = f""" { name } not loaded, MAY BE NOT IN YOUR CONTACTS ,
97+ message = f''' { name } not loaded, MAY BE NOT IN YOUR CONTACTS ,
9098 If you are sure { name } is in your contacts, Try checking internet connection
9199
92- OR May be some other problem ... """
100+ OR May be some other problem ... '''
93101
94102 convey (error , message )
95103
96104 search_box .send_keys ((Keys .BACKSPACE )* len (name ))
97105 # clearing the search bar by backspace, so that searching the next person does'nt have any issue
98106 return False
99107
100- def send_message (self , to , msg = '' ):
108+ def send_message (self , to , msg ):
109+ '''Method to send a text message to a contact.
110+ Requires two arguments: to and msg
101111
102- if self .load_selected_person (to ):
112+ Example Use :
113+ bot.send_message(to='contact',msg='hi')
114+ or
115+ bot.send_message('contact','hi')
116+ where bot is an object of WappDriver class
117+ '''
103118
119+ if self .load_person (to ):
104120 msg_box = WebDriverWait (self .driver , self .timeout ).until (
105- expCond .presence_of_element_located ((By .XPATH , self .mBox )))
121+ expected_conditions .presence_of_element_located ((By .XPATH , self .mBox )))
106122 lines = msg .split ('\n ' )
107123
108124 for line in lines :
109125 msg_box .send_keys (line ) # write a line
110126 msg_box .send_keys (Keys .SHIFT + Keys .ENTER ) # go to next line
111127
112128 msg_box .send_keys (Keys .ENTER ) # send message
129+
130+ def send_media (self , to , path , caption = None ):
131+ '''Method to send a media object to a contact.
132+ Supports: Image, GIF, Video
133+ Requires two arguments: to and path
134+ Optional argument: caption
135+
136+ Example Use :
137+ bot.send_media(to='contact',path='path/to/media',caption='wow')
138+ or
139+ bot.send_media('contact','path/to/media','wow')
140+ where bot is an object of WappDriver class
141+
142+ Not giving the caption is allowed
143+ '''
144+
145+ if self .load_person (to ):
146+ pass
147+
148+ def send_file (self ,to ,path ):
149+ '''Method to send any kind of file to a contact.
150+ Supports: ALl formats
151+ Requires two arguments: to and path
152+
153+ Example Use :
154+ bot.send_file(to='contact',path='path/to/file')
155+ or
156+ bot.send_file('contact','path/to/file')
157+ where bot is an object of WappDriver class
158+
159+ Not giving the caption is allowed
160+ '''
161+
162+ def send_contact (self ,to ,contact ):
163+ pass
164+
165+ def send_url (self ,to ,url ):
166+ pass
167+
0 commit comments