-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathlog_all.py
152 lines (139 loc) · 6.34 KB
/
log_all.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os, sys, time, json, datetime
from src import WhatsAPIDriver
from src.objects.message import Message, MediaMessage
# EXAMPLE OF LOGGER BOT
# =====================
# Logs everything that passes through the phone, and saves media content too.
# Perfect companion for those who hate the ones who use to "erase message".
# The bot interacts with a remote phone, called "master".
# This phone can send simple commands and receives periodic notifications
# in order to know the bot is still running
# Global Const
# cc = country code, e.g. for UK '44'
# ppp = mobile prefix
# nnnnnnn = mobile number
masters_number = "ccpppnnnnnnn"
# Global Vars
pinger = -1
now = datetime.datetime.now()
start_time = datetime.datetime.now()
# Procs and Funcs
def print_and_log(text):
print(text)
f=open("generallog.log", "a+", encoding="utf-8")
f.write("[{timestamp}] : {txt}\n".format(timestamp=datetime.datetime.now(), txt=text))
f.close()
def send_message_to_master(message):
phone_safe = masters_number # Phone number with country code
phone_whatsapp = "{}@c.us".format(phone_safe) # WhatsApp Chat ID
driver.chat_send_message(phone_whatsapp,message)
def process_command(command):
print_and_log("Processing command: {cmd}".format(cmd=command))
if command.lower() == '#status':
send_message_to_master("I am still alive")
elif command.lower() == '#quit':
quit()
elif command.lower() == '#ping':
send_message_to_master("The counter is now {ping}".format(ping=pinger))
elif command.lower() == '#uptime':
uptime = datetime.datetime.now() - start_time
send_message_to_master("Up since {start}, hence for a total time of {upt} by now".format(start=start_time,upt=uptime))
else:
send_message_to_master("I am sorry but I can't understand '{cmd}'".format(cmd=command))
# Main
driver = WhatsAPIDriver()
print("Waiting for QR")
while not driver.wait_for_login():
time.sleep(3)
print("Bot started")
start_time = datetime.datetime.now()
try:
while True:
time.sleep(3) # Checks for new messages every 3 secs.
pinger = pinger +1
if ((pinger%600) == 0): # Notification every 30 min. (600 * 3 sec = 1800 sec)
pinger=0
send_message_to_master("Resetting counter to {pingcount}. Driver status is '{status}'".format(pingcount=pinger, status=driver.get_status()))
print('Checking for more messages, status. Pinger={pingcount}'.format(pingcount=pinger), driver.get_status())
for contact in driver.get_unread(include_me=True, include_notifications=True):
for message in contact.messages:
print(json.dumps(message.get_js_obj(), indent = 4))
# Log full JSON to general log
f=open("generallog.log", "a+", encoding="utf-8")
f.write("\n\n==========================================================================\nMessage received at {timestamp}\n".format(timestamp=str(datetime.datetime.now())))
try:
f.write(json.dumps(message.get_js_obj(), indent = 4))
except:
f.write('ERROR!! Unprintable JSON!')
send_message_to_master("Unprintable JSON! Please check!")
f.write("\n")
f.close()
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)
# Notifications don't seem to have sender.id neither sender.getsafename()
try:
sender_id = message.sender.id
except:
sender_id = 'NONE'
print('sender.id', sender_id)
try:
sender_safe_name = message.sender.get_safe_name()
except:
sender_safe_name = 'NONE'
print('sender.safe_name', sender_safe_name)
if message.type == 'chat':
print('-- Chat')
print('safe_content', message.safe_content)
try:
print('content', message.content)
except:
print('content is unsafe! Printing safe_content instead', message.safe_content)
send_message_to_master("Unprintable MESSAGE CONTENT! Please check!")
f=open("chat_" + message.chat_id['_serialized'] + ".chat.log","a+", encoding="utf-8")
f.write("[ {sender} | {timestamp} ] ".format(sender=message.sender.get_safe_name(), timestamp=message.timestamp))
try:
f.write(message.content)
except:
f.write('(safecontent) {content}'.format(content=message.safe_content))
f.write("\n")
f.close()
f=open("safechat_" + message.chat_id['_serialized'] + ".chat.log","a+")
f.write("[ {sender} | {timestamp} ] {content}\n".format(sender=message.sender.get_safe_name(), timestamp=message.timestamp, content=message.safe_content))
f.close()
if ( (message.chat_id['user'] == masters_number) and (message.content[0:1]=="#") ) :
print_and_log("Message from master: '{cmd}'.".format(cmd=message.content))
process_command(message.content)
elif message.type == 'image' or message.type == 'video' or message.type == 'document' or message.type == 'audio' :
print('-- Media')
print('filename', message.filename)
print('size', message.size)
print('mime', message.mime)
msg_caption=''
if hasattr(message, 'caption'):
msg_caption = message.caption
print('caption', message.caption)
print('client_url', message.client_url)
f=open("chat_" + message.chat_id['_serialized'] + ".chat.log","a+")
f.write("[ {sender} | {timestamp} ] sent media chat_{id}\{filename} with caption '{caption}'\n".format(sender=message.sender.get_safe_name(), timestamp=message.timestamp, id=message.chat_id['_serialized'], filename=message.filename, caption=msg_caption))
f.close()
f=open("safechat_" + message.chat_id['_serialized'] + ".chat.log","a+")
f.write("[ {sender} | {timestamp} ] sent media chat_{id}\{filename} with caption '{caption}'\n".format(sender=message.sender.get_safe_name(), timestamp=message.timestamp, id=message.chat_id['_serialized'], filename=message.filename, caption=msg_caption))
f.close()
if not os.path.exists('chat_{id}'.format(id=message.chat_id['_serialized'])):
os.makedirs('chat_{id}'.format(id=message.chat_id['_serialized']))
message.save_media('chat_{id}'.format(id=message.chat_id['_serialized']))
else:
print('-- Other')
except Exception as e:
print('EXCEPTION:',e)
send_message_to_master("I am dying! HELP!\n")
send_message_to_master("Exception was: {exc}\n".format(exc=e))
f=open("generallog.log", "a+", encoding="utf-8")
f.write("\n\nEXCEPTION: {exc}\n".format(exc=e))
f.close
raise