-
Notifications
You must be signed in to change notification settings - Fork 12
/
main4.py
52 lines (48 loc) · 2.1 KB
/
main4.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
import os, sys, time, json
from openwa import WhatsAPIDriver
print("Environment", os.environ)
try:
os.environ["CHROMEDRIVER_PATH"]
except KeyError:
print("Please set the environment variable SELENIUM to Selenium URL")
sys.exit(1)
##Save session on "/firefox_cache/localStorage.json".
##Create the directory "/firefox_cache", it's on .gitignore
##The "app" directory is internal to docker, it corresponds to the root of the project.
##The profile parameter requires a directory not a file.
driver = WhatsAPIDriver( client='chrome', command_executor=os.environ["CHROMEDRIVER_PATH"],executable_path=os.environ["GOOGLE_CHROME_BIN"])
print("Waiting for QR")
driver.wait_for_login()
print("Saving session")
driver.save_firefox_profile(remove_old=False)
print("Bot started")
while True:
time.sleep(3)
print('Checking for more messages, status', driver.get_status())
for contact in driver.get_unread():
for message in contact.messages:
print(json.dumps(message.get_js_obj(), indent=4))
print('class', message.__class__.__name__)
print('message', message)
print('id', message.id)
print('type', message.type)
print('timestamp', message.timestamp)
print('chat_id', message.chat_id)
print('sender', message.sender)
print('sender.id', message.sender.id)
print('sender.safe_name', message.sender.get_safe_name())
if message.type == 'chat':
print('-- Chat')
print('safe_content', message.safe_content)
print('content', message.content)
# contact.chat.send_message(message.safe_content)
elif message.type == 'image' or message.type == 'video':
print('-- Image or Video')
print('filename', message.filename)
print('size', message.size)
print('mime', message.mime)
print('caption', message.caption)
print('client_url', message.client_url)
message.save_media('./')
else:
print('-- Other')