-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhatsapp_wrapper.py
49 lines (39 loc) · 1.43 KB
/
whatsapp_wrapper.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
from webwhatsapi import WhatsAPIDriver
class WhatsappWrapper:
def __init__(self, profile_path, username="kautilya", headless_flag=True):
self.username = username
self.profile_path = profile_path
self.headless_flag = headless_flag
self.driver = self.open()
self.chats = []
def get_all_chats(self):
if len(self.chats) > 0:
return self.chats
self.chats = self.process_chats(self.driver.get_all_chats())
return self.chats
@staticmethod
def process_chats(chats):
chats = [Chat(chat) for chat in chats]
return chats
def open(self):
driver = WhatsAPIDriver(username=self.username, profile=self.profile_path, headless=self.headless_flag,
loadstyles=True)
driver.wait_for_login()
return driver
def close(self):
return self.driver.close()
class Chat:
def __init__(self, p_chat):
split_text = p_chat.id.split("@")
self.phone_number = split_text[0]
self.chat_type = split_text[1]
self.parent = p_chat
self.loaded_messages = False
def send_message(self, text):
return self.parent.send_message(text)
def get_messages(self):
if self.loaded_messages:
return self.parent.get_messages()
self.parent.load_earlier_messages()
self.loaded_messages = True
return self.parent.get_messages()