This repository was archived by the owner on Apr 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreceiver_message.py
More file actions
295 lines (262 loc) · 11.9 KB
/
receiver_message.py
File metadata and controls
295 lines (262 loc) · 11.9 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""It can receive all messages.
It only focus on Type 26 and don't do any response.
"""
from json import loads
from linepy import *
from colors import *
from secret_class import *
def receive_message(operation): # Type: 26
try:
msg = operation.message
text = msg.text
msg_id = msg.id
receiver = msg.to
# noinspection PyProtectedMember
sender = msg._from
sender_name = line.getContact(sender).displayName
file_name = str(operation.revision)
content_list = [0, 1, 2, 3, 6, 7, 12, 13, 14, 16, 18]
if msg.toType == 0:
# line.sendChatChecked(sender, msg_id)
if msg.contentType not in content_list:
print("contentType: " + str(msg.contentType))
print(operation)
else:
action(msg, text, sender_name, msg_id, file_name, 'ME')
elif msg.toType == 2:
# line.sendChatChecked(receiver, msg_id)
group_name = line.getGroup(receiver).name
if msg.contentType not in content_list:
print("contentType: " + str(msg.contentType))
print(operation)
else:
action(msg, text, sender_name, msg_id, file_name, group_name)
except Exception as e:
line_print(COLOR.FAIL + "[RECEIVE_MESSAGE]" + COLOR.END + " ERROR : " + str(e))
def action(msg, text, sender_name, msg_id, file_name, group_name):
try:
if msg.contentType == 0:
context(msg, text, sender_name, group_name)
elif msg.contentType == 1:
photo(msg, sender_name, msg_id, file_name, group_name)
elif msg.contentType == 2:
video(sender_name, msg_id, file_name, group_name)
elif msg.contentType == 3:
audio(sender_name, msg_id, file_name, group_name)
elif msg.contentType == 4:
events()
elif msg.contentType == 6:
call()
elif msg.contentType == 7:
sticker(msg, sender_name, group_name)
elif msg.contentType == 12:
event()
elif msg.contentType == 13:
contact(msg, sender_name, group_name)
elif msg.contentType == 14:
file(msg, sender_name, msg_id, group_name)
elif msg.contentType == 16:
notification(msg, sender_name, group_name)
elif msg.contentType == 18:
delete_photo_in_album(msg, sender_name, group_name)
else:
line_print("[UNKNOWN_CONTENT_TYPE]")
except Exception as e:
line_print(COLOR.FAIL + "[ACTION]" + COLOR.END + " ERROR : " + str(e))
def context(msg, text, sender_name, group_name):
try:
if msg.location is not None:
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '-> %s' % group_name + COLOR.END +
' sent a location: %s' % str(msg.location))
line_log_to_file(
'[%s] ' % sender_name + '-> %s' % group_name +
' sent a location: %s' % str(msg.location))
elif msg.relatedMessageId is not None:
line_print(COLOR.FAIL + '[HANDLE_MESSAGE_ERROR]: %s' % msg + COLOR.END)
line_log_to_file('[HANDLE_MESSAGE_ERROR]: %s' % msg)
else:
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '-> %s' % group_name + COLOR.END +
' %s' % text)
line_log_to_file(
'[%s] ' % sender_name + '-> %s' % group_name +
' %s' % text)
except Exception as e:
line_print(COLOR.FAIL + "[CONTEXT]" + COLOR.END + " ERROR : " + str(e))
def photo(msg, sender_name, msg_id, file_name, group_name):
try:
if 'MEDIA_CONTENT_INFO' in msg.contentMetadata:
extension = loads(msg.contentMetadata['MEDIA_CONTENT_INFO'])['extension']
if extension == 'png':
line.downloadObjectMsg(msg_id, saveAs=save_path + file_name + '.png')
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '-> %s' % group_name + COLOR.END +
' sent a PNG: %s' % file_name + '.png')
line_log_to_file(
'[%s] ' % sender_name + '-> %s' % group_name +
' sent a PNG: %s' % file_name + '.png')
elif extension == 'JPEG':
line.downloadObjectMsg(msg_id, saveAs=save_path + file_name + '.jpg')
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '-> %s' % group_name + COLOR.END +
' sent a JPEG: %s' % file_name + '.jpg')
line_log_to_file(
'[%s] ' % sender_name + '-> %s' % group_name +
' sent a JPEG: %s' % file_name + '.jpg')
elif extension == 'gif':
line.downloadObjectMsg(msg_id, saveAs=save_path + file_name + '.gif')
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '-> %s' % group_name + COLOR.END +
' sent a GIF: %s' % file_name + '.gif')
line_log_to_file(
'[%s] ' % sender_name + '-> %s' % group_name +
' sent a GIF: %s' % file_name + '.gif')
else:
print(msg)
else:
line.downloadObjectMsg(msg_id, saveAs=save_path + file_name + '.jpg')
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '-> %s' % group_name + COLOR.END +
' sent a picture: %s' % file_name + '.jpg')
line_log_to_file(
'[%s] ' % sender_name + '-> %s' % group_name +
' sent a picture: %s' % file_name + '.jpg')
except Exception as e:
line_print(COLOR.FAIL + "[PHOTO]" + COLOR.END + " ERROR : " + str(e))
def video(sender_name, msg_id, file_name, group_name):
try:
line.downloadObjectMsg(msg_id, saveAs=save_path + file_name + '.mp4')
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '-> %s' % group_name + COLOR.END +
' sent a video: %s' % file_name)
line_log_to_file(
'[%s] ' % sender_name + '-> %s' % group_name +
' sent a video: %s' % file_name)
except Exception as e:
line_print(COLOR.FAIL + "[VIDEO]" + COLOR.END + " ERROR : " + str(e) + "\nMessage ID is: " + msg_id)
def audio(sender_name, msg_id, file_name, group_name):
try:
line.downloadObjectMsg(msg_id, saveAs=save_path + file_name + '.mp4')
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '-> %s' % group_name + COLOR.END +
' sent an audio message: %s' % file_name + '.mp4')
line_log_to_file(
'[%s] ' % sender_name + '-> %s' % group_name +
' sent an audio message: %s' % file_name + '.mp4')
except Exception as e:
line_print(COLOR.FAIL + "[AUDIO]" + COLOR.END + " ERROR : " + str(e))
def events():
try:
print('event')
except Exception as e:
line_print(COLOR.FAIL + "[EVENTS]" + COLOR.END + " ERROR : " + str(e))
def call():
try:
print('call')
except Exception as e:
line_print(COLOR.FAIL + "[CALL]" + COLOR.END + " ERROR : " + str(e))
# noinspection SpellCheckingInspection
def sticker(msg, sender_name, group_name):
try:
txt = 'send a sticker\n' + \
' Sticker Package ID: %s\n' % str(msg.contentMetadata['STKPKGID']) + \
' Sticker ID: %s' % str(msg.contentMetadata['STKID'])
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '-> %s' % group_name + COLOR.END +
' %s' % txt)
line_log_to_file(
'[%s] ' % sender_name + '-> %s' % group_name +
' %s' % txt)
except Exception as e:
line_print(COLOR.FAIL + "[STICKER]" + COLOR.END + " ERROR : " + str(e))
def event():
try:
print('event')
except Exception as e:
line_print(COLOR.FAIL + "[EVENT]" + COLOR.END + " ERROR : " + str(e))
def contact(msg, sender_name, group_name):
try:
contact_id = msg.contentMetadata['mid']
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '-> %s' % group_name + COLOR.END +
' sent contact: %s' % contact_id)
line_log_to_file(
'[%s] ' % sender_name + '-> %s' % group_name +
' sent contact: %s' % contact_id)
except Exception as e:
line_print(COLOR.FAIL + "[CONTACT]" + COLOR.END + " ERROR : " + str(e))
def file(msg, sender_name, msg_id, group_name):
try:
file_name = str(msg.contentMetadata['FILE_NAME'])
line.downloadObjectMsg(msg_id, saveAs=save_path + file_name)
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '-> %s' % group_name + COLOR.END +
' sent a file: %s' % file_name)
line_log_to_file(
'[%s] ' % sender_name + '-> %s' % group_name +
' sent a file: %s' % file_name)
except Exception as e:
line_print(COLOR.FAIL + "[FILE]" + COLOR.END + " ERROR : " + str(e))
# noinspection SpellCheckingInspection
def notification(msg, sender_name, group_name):
try:
lockey = msg.contentMetadata['locKey']
if lockey == 'BN':
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '@ %s' % group_name + COLOR.END +
' sent a note.')
line_log_to_file(
'[%s] ' % sender_name + '@ %s' % group_name +
' sent a note.')
elif lockey == 'BA':
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '@ %s' % group_name + COLOR.END +
' created an album: %s, photos: (%s+1)' % (
msg.contentMetadata['albumName'], msg.contentMetadata['mediaCount']))
line_log_to_file(
'[%s] ' % sender_name + '@ %s' % group_name +
' created an album: %s, photos: (%s+1)' % (
msg.contentMetadata['albumName'], msg.contentMetadata['mediaCount']))
elif lockey == 'BT':
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '@ %s' % group_name + COLOR.END +
' add photos (%s+1) in an album: %s' % (
msg.contentMetadata['mediaCount'], msg.contentMetadata['albumName']))
line_log_to_file(
'[%s] ' % sender_name + '@ %s' % group_name +
' add photos (%s+1) in an album: %s' % (
msg.contentMetadata['mediaCount'], msg.contentMetadata['albumName']))
else:
print(msg)
except Exception as e:
line_print(COLOR.FAIL + "[NOTIFICATION]" + COLOR.END + " ERROR : " + str(e))
def delete_photo_in_album(msg, sender_name, group_name):
try:
loc_key = msg.contentMetadata['LOC_KEY']
album_name = msg.contentMetadata['LOC_ARGS']
if loc_key == 'BO':
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '-> %s' % group_name + COLOR.END +
' delete photo in %s' % album_name)
line_log_to_file(
'[%s] ' % sender_name + '-> %s' % group_name +
' delete photo in %s' % album_name)
elif loc_key == 'BD':
line_print(
COLOR.SENDER + '[%s] ' % sender_name + COLOR.BOLD + '-> %s' % group_name + COLOR.END +
' delete album: %s' % album_name)
line_log_to_file(
'[%s] ' % sender_name + '-> %s' % group_name +
' delete album: %s' % album_name)
except Exception as e:
line_print(COLOR.FAIL + "[DELETE_PHOTO_IN_ALBUM]" + COLOR.END + " ERROR : " + str(e))
if __name__ == '__main__':
line = LINE(line_token)
line.log(line.authToken)
oepoll = OEPoll(line)
oepoll.addOpInterruptWithDict({
OpType.RECEIVE_MESSAGE: receive_message
})
while True:
oepoll.trace()