This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy patheh_wechat_slave.py
1054 lines (960 loc) · 42.9 KB
/
eh_wechat_slave.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import base64
import logging
import mimetypes
import os
import io
import re
import threading
import time
import html
from binascii import crc32
import itchat
import magic
import xmltodict
from pyqrcode import QRCode
from PIL import Image
import config
from channel import EFBChannel, EFBMsg, MsgType, MsgSource, TargetType, ChannelType
from channelExceptions import EFBMessageTypeNotSupported, EFBMessageError, EFBChatNotFound
from utils import extra
def wechat_msg_meta(func):
def wrap_func(self, msg, *args, **kwargs):
isGroupChat = str(msg['FromUserName']).startswith("@@")
logger = logging.getLogger("plugins.%s.wechat_msg_meta" % self.channel_id)
logger.debug("Raw message: %s" % repr(msg))
mobj = func(self, msg, *args, **kwargs)
if mobj is None:
return
mobj.uid = msg.get("NewMsgId", time.time())
me = msg['FromUserName'] == self.itchat.loginInfo['User']['UserName']
logger.debug("me, %s", me)
if me:
msg['FromUserName'], msg['ToUserName'] = msg['ToUserName'], msg['FromUserName']
FromUser = self.search_user(UserName=msg['FromUserName']) or \
self.search_user(UserName=msg['FromUserName'], refresh=True) or \
[{"NickName": "Chat not found. (UE01)", "RemarkName": "Chat not found. (UE01)", "Uin": 0}]
FromUser = FromUser[0]
logger.debug("From user, %s", FromUser)
if isGroupChat:
logger.debug("Group chat")
if me:
msg['ActualUserName'] = msg['ToUserName']
member = {"NickName": self._wechat_html_unescape(self.itchat.loginInfo['User']['NickName']),
"DisplayName": "You",
"Uin": self.itchat.loginInfo['User']['Uin']}
else:
logger.debug("search_user")
member = self.search_user(UserName=msg['FromUserName'], ActualUserName=msg['ActualUserName'])[0]['MemberList'][0]
logger.debug("search_user.done")
mobj.source = MsgSource.Group
logger.debug("Group. member: %s", member)
mobj.origin = {
'name': html.unescape(FromUser['NickName']),
'alias': html.unescape(FromUser['RemarkName'] or FromUser['NickName']),
'uid': self.get_uid(UserName=msg.get('FromUserName', None),
NickName=self._wechat_html_unescape(FromUser.get('NickName', "s")),
alias=self._wechat_html_unescape(FromUser.get('RemarkName', "s")),
Uin=FromUser.get('Uin', None))
}
mobj.member = {
'name': self._wechat_html_unescape(member['NickName']),
'alias': self._wechat_html_unescape(member['DisplayName']),
'uid': self.get_uid(UserName=msg.get('ActualUserName', None),
NickName=self._wechat_html_unescape(member.get('NickName', "")),
alias=self._wechat_html_unescape(member.get('DisplayName', "")),
Uin=member.get('Uin', None))
}
logger.debug("origin: %s\nmember: %s\n", mobj.origin, mobj.member)
else:
if me:
mobj.text = mobj.text or ""
mobj.text = "You: " + mobj.text
mobj.source = MsgSource.User
mobj.origin = {
'name': self._wechat_html_unescape(FromUser['NickName']),
'alias': self._wechat_html_unescape(FromUser['RemarkName'] or FromUser['NickName']),
'uid': self.get_uid(UserName=msg.get('FromUserName', None),
NickName=self._wechat_html_unescape(FromUser.get('NickName', "")),
alias=self._wechat_html_unescape(FromUser.get('RemarkName', "")),
Uin=FromUser.get('Uin', None))
}
if msg['Type'] == 'Note':
logger.debug("System message. Overriding field")
if isGroupChat:
mobj.member["name"] = "系统消息"
else:
mobj.origin["name"] = "系统消息"
mobj.destination = {
'name': self._wechat_html_unescape(self.itchat.loginInfo['User']['NickName']),
'alias': self._wechat_html_unescape(self.itchat.loginInfo['User']['NickName']),
'uid': self.get_uid(UserName=msg['ToUserName'])
}
logger.debug("dest: %s", mobj.destination)
logger.info("WeChat incoming message:\nType: %s\nText: %s\nUserName: %s\nuid: %s\nname: %s" %
(mobj.type, msg['Text'], msg['FromUserName'], mobj.origin['uid'], mobj.origin['name']))
self.queue.put(mobj)
return wrap_func
class WeChatChannel(EFBChannel):
"""
EFB Channel - WeChat (slave)
Based on itchat (modified by Eana Hufwe)
Author: Eana Hufwe <https://github.com/blueset>
"""
channel_name = "WeChat Slave"
channel_emoji = "💬"
channel_id = "eh_wechat_slave"
channel_type = ChannelType.Slave
supported_message_types = {MsgType.Text, MsgType.Sticker, MsgType.Image,
MsgType.File, MsgType.Video, MsgType.Link, MsgType.Audio}
logger = logging.getLogger("plugins.%s.WeChatChannel" % channel_id)
qr_uuid = ""
done_reauth = threading.Event()
_stop_polling = False
def __init__(self, queue, mutex):
super().__init__(queue, mutex)
#self.itchat = itchat.new_instance()
self.itchat = itchat.load_sync_itchat()
itchat.set_logging(loggingLevel=logging.getLogger().level, showOnCmd=False)
self.itchat_msg_register()
with mutex:
self.itchat.auto_login(enableCmdQR=2,
hotReload=True,
statusStorageDir="storage/%s.pkl" % self.channel_id,
exitCallback=self.exit_callback,
qrCallback=self.console_qr_code)
mimetypes.init(files=["mimetypes"])
self.logger.info("EWS Inited!!!\n---")
#
# Utilities
#
def console_qr_code(self, uuid, status, qrcode):
status = int(status)
if status == 201:
QR = 'Tap "Confirm" to continue.'
time.sleep(0.5)
return self.logger.critical(QR)
elif status == 200:
QR = "Successfully authorized."
return self.logger.critical(QR)
elif uuid != self.qr_uuid:
# 0: First QR code
# 408: Updated QR code
QR = "WeChat: Scan QR code with WeChat to continue. (%s, %s)\n" % (uuid, status)
if status == 408:
QR += "Previous code expired. Please scan the new one.\n"
QR += "\n"
qr_url = "https://login.weixin.qq.com/l/" + uuid
qr_obj = QRCode(qr_url)
if self._flag("imgcat_qr", False):
qr_file = io.BytesIO()
qr_obj.png(qr_file, scale=10)
QR += self.imgcat(qr_file, "%s_QR_%s.png" % (self.channel_id, uuid))
else:
QR += qr_obj.terminal()
QR += "\nIf you cannot read the QR code above, " \
"please visit the following URL:\n" \
"https://login.weixin.qq.com/qrcode/" + uuid
return self.logger.critical(QR)
self.qr_uuid = uuid
def master_qr_code(self, uuid, status, qrcode):
status = int(status)
msg = EFBMsg(self)
msg.type = MsgType.Text
msg.source = MsgSource.System
msg.origin = {
'name': '%s Auth' % self.channel_name,
'alias': '%s Auth' % self.channel_name,
'uid': -1
}
if status == 201:
msg.type = MsgType.Text
msg.text = 'Tap "Confirm" to continue.'
time.sleep(0.5)
elif status == 200:
msg.type = MsgType.Text
msg.text = "Successfully authenticated."
elif uuid != self.qr_uuid:
msg.type = MsgType.Image
path = os.path.join("storage", self.channel_id)
if not os.path.exists(path):
os.makedirs(path)
path = os.path.join(path, 'QR-%s.jpg' % int(time.time()))
self.logger.debug("master_qr_code file path: %s", path)
qr_url = "https://login.weixin.qq.com/l/" + uuid
QRCode(qr_url).png(path, scale=10)
msg.text = 'Scan this QR Code with WeChat to continue.'
msg.path = path
msg.file = open(path, 'rb')
msg.mime = 'image/jpeg'
if status in (200, 201) or uuid != self.qr_uuid:
self.queue.put(msg)
self.qr_uuid = uuid
def exit_callback(self):
if self.stop_polling:
return
msg = EFBMsg(self)
msg.source = MsgSource.System
msg.origin = {
'name': '%s System' % self.channel_name,
'alias': '%s System' % self.channel_name,
'uid': -1
}
msg.text = "WeChat server logged out the user."
msg.type = MsgType.Text
on_log_out = self._flag("on_log_out", "command")
on_log_out = on_log_out if on_log_out in ("command", "idle", "reauth") else "command"
if on_log_out == "command":
msg.type = MsgType.Command
msg.attributes = {
"commands": [
{
"name": "Log in",
"callable": "reauth",
"args": [],
"kwargs": {"command": True}
}
]
}
elif on_log_out == "reauth":
if self._flag("qr_reload", "master_qr_code") == "console_qr_code":
msg.text += "\nPlease visit your console or log for QR code and further instructions."
self.reauth()
self.queue.put(msg)
def get_uid(self, UserName=None, NickName=None, alias=None, Uin=None):
"""
Get unique identifier of a chat, by UserName or NickName.
Fill in `UserName` or `NickName`.
Args:
UserName (str): WeChat `UserName` of the chat.
NickName (str): Name (`NickName`) of the chat.
alias (str): Alias ("Display Name" or "Remark Name") of the user
Uin: WeChat Unique Identifier.
Returns:
str|bool: Unique ID of the chat. `False` if not found.
"""
if UserName and not str(UserName).startswith("@"):
return UserName
if not (UserName or NickName or alias or Uin):
self.logger.error('No name provided.')
return False
data = {"nickname": NickName, "alias": alias, "uin": Uin}
if UserName and not (NickName or alias or Uin):
r = self.search_user(UserName=UserName)
if not r:
self.logger.debug("get_uid, return False")
return False
data = {"nickname": r[0].get("NickName", ""), "alias": r[0].get("RemarkName", ""),
"uin": r[0].get("Uin", "")}
return self.encode_uid(data)
def encode_uid(self, data):
"""
Encode uid by a predefined order in configuration.
Args:
data (dict): a dict with keys `{"nickname", "alias", "uin"}`
Returns:
str: Encoded uid
"""
fallback_order = self._flag("uid_order", ["NickName"])
uid = data[fallback_order[-1].lower()]
for i in fallback_order:
if data[i.lower()]:
uid = data[i.lower()]
break
return str(crc32(str(uid).encode("utf-8")))
def get_UserName(self, uid, refresh=False):
"""
Get WeChat `UserName` of a chat by UID.
Args:
uid (str): UID of the chat.
refresh (bool): Refresh the chat list from WeChat, `False` by default.
Returns:
str|bool: `UserName` of the chosen chat. `False` if not found.
"""
if uid and str(uid).isalpha():
return uid
r = self.search_user(uid=uid, refresh=refresh)
if r:
return r[0]['UserName']
return None
def search_user(self, UserName=None, uid=None, uin=None, name=None, ActualUserName=None, refresh=False):
"""
Search for a WeChat "User" (a user, a group/chat room or an MP account,
by `UserName`, unique ID, WeChat ID, name/alias, and/or group member `UserName`.
At least one of `UserName`, `uid`, `wid`, or `name` should be provided.
When matching for a group, all of `UserName`, `uid`, `wid`, and `name` will be used
to match for group and member.
Args:
UserName (str): UserName of a "User"
uid (str): Unique ID generated by the channel
uin (str): WeChat Unique Identifier.
name (str): Name or Alias
ActualUserName (str): UserName of a group member, used only when a group is matched.
refresh (bool): Refresh the user list, False by default.
Returns:
list of dict: A list of matching users in ItChat user dict format.
"""
result = []
UserName = None if UserName is None else str(UserName)
uid = None if uid is None else str(uid)
uin = None if uin is None else str(uin)
name = None if name is None else str(name)
ActualUserName = None if ActualUserName is None else str(ActualUserName)
if all(i is None for i in [UserName, uid, uin, name]):
raise ValueError("At least one of [UserName, uid, uin, name] should be provided.")
if (UserName and not str(UserName).startswith("@")) or (uin and str(uin).isalpha()):
sys_chat_id = UserName or uin
return [{"UserName": sys_chat_id,
"NickName": "System (%s)" % sys_chat_id,
"RemarkName": "System (%s)" % sys_chat_id,
"Uin": sys_chat_id}]
for i in self.itchat.get_friends(refresh) + self.itchat.get_mps(refresh):
data = {"nickname": self._wechat_html_unescape(i.get('NickName', None)),
"alias": self._wechat_html_unescape(i.get("RemarkName", None)),
"uin": i.get("Uin", None)}
if self.encode_uid(data) == uid or \
str(i.get('UserName', '')) == UserName or \
str(i.get('AttrStatus', '')) == uid or \
str(i.get('Uin', '')) == uin or \
str(i.get('NickName', '')) == name or \
str(i.get('DisplayName', '')) == name:
i['NickName'] = self._wechat_html_unescape(i.get('NickName', ''))
i['DisplayName'] = self._wechat_html_unescape(i.get('DisplayName', ''))
result.append(i.copy())
for i in self.itchat.get_chatrooms(refresh):
if not i['UserName'].startswith('@@'):
continue
if not i.get('MemberList', ''):
i = self.itchat.update_chatroom(i.get('UserName', ''))
data = {"nickname": self._wechat_html_unescape(i.get('NickName', None)),
"alias": self._wechat_html_unescape(i.get("RemarkName", None)),
"uin": i.get("Uin", None)}
if self.encode_uid(data) == uid or \
str(i.get('Uin', '')) == uin or \
str(i.get('NickName', '')) == name or \
str(i.get('DisplayName', '')) == name or \
str(i.get('UserName', '')) == UserName:
i['NickName'] = self._wechat_html_unescape(i.get('NickName', ''))
i['DisplayName'] = self._wechat_html_unescape(i.get('DisplayName', ''))
result.append(i.copy())
result[-1]['MemberList'] = []
if ActualUserName:
for j in i['MemberList']:
if str(j['UserName']) == ActualUserName:
j['NickName'] = self._wechat_html_unescape(j.get('NickName', ''))
j['DisplayName'] = self._wechat_html_unescape(j.get('DisplayName', ''))
result[-1]['MemberList'].append(j)
if not result and not refresh:
return self.search_user(UserName, uid, uin, name, ActualUserName, refresh=True)
return result
def poll(self):
while not self.stop_polling:
if self.itchat.alive:
self.itchat.configured_reply()
else:
self.done_reauth.wait()
self.done_reauth.clear()
if self.itchat.useHotReload:
self.itchat.dump_login_status("storage/%s.pkl" % self.channel_id)
self.itchat.alive = False
self.logger.debug("%s (%s) gracefully stopped.", self.channel_name, self.channel_id)
def itchat_msg_register(self):
self.itchat.msg_register(['Text'], isFriendChat=True, isMpChat=True, isGroupChat=True)(self.wechat_text_msg)
self.itchat.msg_register(['Sharing'], isFriendChat=True, isMpChat=True, isGroupChat=True)(self.wechat_link_msg)
self.itchat.msg_register(['Picture'], isFriendChat=True, isMpChat=True, isGroupChat=True)(self.wechat_picture_msg)
self.itchat.msg_register(['Attachment'], isFriendChat=True, isMpChat=True, isGroupChat=True)(self.wechat_file_msg)
self.itchat.msg_register(['Recording'], isFriendChat=True, isMpChat=True, isGroupChat=True)(self.wechat_voice_msg)
self.itchat.msg_register(['Map'], isFriendChat=True, isMpChat=True, isGroupChat=True)(self.wechat_location_msg)
self.itchat.msg_register(['Video'], isFriendChat=True, isMpChat=True, isGroupChat=True)(self.wechat_video_msg)
self.itchat.msg_register(['Card'], isFriendChat=True, isMpChat=True, isGroupChat=True)(self.wechat_card_msg)
self.itchat.msg_register(['Friends'], isFriendChat=True, isMpChat=True, isGroupChat=True)(self.wechat_friend_msg)
self.itchat.msg_register(['Useless', 'Note'], isFriendChat=True, isMpChat=True, isGroupChat=True)(self.wechat_system_msg)
@self.itchat.msg_register(["System"], isFriendChat=True, isMpChat=True, isGroupChat=True)
def wc_msg_system_log(msg):
self.logger.debug("WeChat \"System\" message:\n%s", repr(msg))
@wechat_msg_meta
def wechat_text_msg(self, msg):
if msg['FromUserName'] == "newsapp" and msg['Content'].startswith("<mmreader>"):
self.wechat_newsapp_msg(msg)
return
if msg['Text'].startswith("http://weixin.qq.com/cgi-bin/redirectforward?args="):
self.wechat_location_msg(msg)
return
mobj = EFBMsg(self)
mobj.text = msg['Text']
mobj.type = MsgType.Text
return mobj
@wechat_msg_meta
def wechat_system_msg(self, msg):
mobj = EFBMsg(self)
mobj.text = "System message: %s" % msg['Text']
mobj.type = MsgType.Text
return mobj
@wechat_msg_meta
def wechat_location_msg(self, msg):
mobj = EFBMsg(self)
mobj.text = msg['Content'].split('\n')[0][:-1]
loc = re.search("=-?([0-9.]+),-?([0-9.]+)", msg['Url']).groups()
mobj.attributes = {"longitude": float(loc[1]), "latitude": float(loc[0])}
mobj.type = MsgType.Location
return mobj
@wechat_msg_meta
def wechat_link_msg(self, msg):
# self.logger.info("---\nNew Link msg, %s", msg)
# # initiate object
# mobj = EFBMsg(self)
# parse XML
itchat.utils.emoji_formatter(msg, 'Content')
xml_data = msg['Content']
data = xmltodict.parse(xml_data)
# # set attributes
base_data = [
data.get('msg', {}).get('appmsg', {}).get('title', None),
data.get('msg', {}).get('appmsg', {}).get('des', None),
data.get('msg', {}).get('appmsg', {}).get('thumburl', None),
data.get('msg', {}).get('appmsg', {}).get('url', None)
]
# if mobj.attributes['url'] is None:
# txt = mobj.attributes['title'] or ''
# txt += mobj.attributes['description'] or ''
# msg['Text'] = txt
# return self.wechat_text_msg(msg)
# format text
# mobj.text = ""
extra_link = data.get('msg', {}).get('appmsg', {}).get('mmreader', {}).get('category', {}).get('item', [])
if not isinstance(extra_link, list):
extra_link = [extra_link]
if self._flag("first_link_only", False):
extra_link = extra_link[:1]
for i in extra_link:
self.wechat_raw_link_msg(msg, i['title'], i['digest'], i['cover'], i['url'])
if not extra_link:
self.wechat_raw_link_msg(msg, *base_data)
return
@wechat_msg_meta
def wechat_raw_link_msg(self, msg, title, description, image, url):
mobj = EFBMsg(self)
if url:
mobj.type = MsgType.Link
mobj.attributes = {
"title": title,
"description": description,
"image": image,
"url": url
}
else:
mobj.type = MsgType.Text
mobj.text = "%s\n%s" % (title, description)
if image:
mobj.text += "\n\n%s" % image
return mobj
def wechat_newsapp_msg(self, msg):
data = xmltodict.parse(msg['Content'])
news = data.get('mmreader', {}).get('category', {}).get('newitem', [])
if news:
self.wechat_raw_link_msg(msg, news[0]['title'], news[0]['digest'], news[0]['cover'], news[0]['shorturl'])
if self._flag("extra_links_on_message", False):
for i in news[1:]:
self.wechat_raw_link_msg(msg, i['title'], i['digest'], i['cover'], i['shorturl'])
@wechat_msg_meta
def wechat_picture_msg(self, msg):
mobj = EFBMsg(self)
mobj.type = MsgType.Image if msg['MsgType'] == 3 else MsgType.Sticker
mobj.path, mime = self.save_file(msg, mobj.type)
mobj.text = None
mobj.file = open(mobj.path, "rb")
mobj.mime = mime
return mobj
@wechat_msg_meta
def wechat_file_msg(self, msg):
mobj = EFBMsg(self)
mobj.type = MsgType.File
mobj.path, mobj.mime = self.save_file(msg, mobj.type)
mobj.text = msg['FileName']
mobj.filename = msg['FileName'] or None
mobj.file = open(mobj.path, "rb")
return mobj
@wechat_msg_meta
def wechat_voice_msg(self, msg):
mobj = EFBMsg(self)
mobj.type = MsgType.Audio
mobj.path, mobj.mime = self.save_file(msg, mobj.type)
mobj.text = None
mobj.file = open(mobj.path, "rb")
return mobj
@wechat_msg_meta
def wechat_video_msg(self, msg):
mobj = EFBMsg(self)
mobj.path, mobj.mime = self.save_file(msg, MsgType.Video)
mobj.type = MsgType.Video
mobj.text = None
mobj.file = open(mobj.path, "rb")
return mobj
@wechat_msg_meta
def wechat_card_msg(self, msg):
mobj = EFBMsg(self)
txt = ("Name card: {NickName}\n"
"From: {Province}, {City}\n"
"QQ: {QQNum}\n"
"ID: {Alias}\n"
"Signature: {Signature}\n"
"Gender: {Sex}")
txt = txt.format(**msg['Text'])
mobj.text = txt
mobj.type = MsgType.Command
mobj.attributes = {
"commands": [
{
"name": "Send friend request",
"callable": "add_friend",
"args": [],
"kwargs": {
"userName": msg['Text']['UserName'],
"status": 2,
"ticket": ""
}
}
]
}
return mobj
@wechat_msg_meta
def wechat_friend_msg(self, msg):
mobj = EFBMsg(self)
txt = ("Friend request: {NickName}\n"
"From: {Province}, {City}\n"
"QQ: {QQNum}\n"
"ID: {Alias}\n"
"Signature: {Signature}\n"
"Gender: {Sex}")
tdict = msg['Text'].copy()
tdict.update(msg['Text']['userInfo'])
txt = txt.format(**tdict)
mobj.text = txt
mobj.type = MsgType.Command
mobj.attributes = {
"commands": [
{
"name": "Send friend request",
"callable": "add_friend",
"args": [],
"kwargs": {
"userName": msg['Text']['userInfo']['UserName'],
"status": 3,
"ticket": msg['Ticket']
}
}
]
}
return mobj
def save_file(self, msg, msg_type):
path = os.path.join("storage", self.channel_id)
if not os.path.exists(path):
os.makedirs(path)
filename = "%s_%s_%s" % (msg_type, msg['NewMsgId'], int(time.time()))
fullpath = os.path.join(path, filename)
msg['Text'](fullpath)
mime = magic.from_file(fullpath, mime=True)
if isinstance(mime, bytes):
mime = mime.decode()
guess_ext = mimetypes.guess_extension(mime) or ".unknown"
if guess_ext == ".unknown":
self.logger.warning("File %s with mime %s has no matching extensions.", fullpath, mime)
ext = ".jpeg" if mime == "image/jpeg" else guess_ext
os.rename(fullpath, "%s%s" % (fullpath, ext))
fullpath = "%s%s" % (fullpath, ext)
self.logger.info("File saved from WeChat\nFull path: %s\nMIME: %s", fullpath, mime)
return fullpath, mime
def send_message(self, msg):
"""Send a message to WeChat.
Supports text, image, sticker, and file.
Args:
msg (channel.EFBMsg): Message Object to be sent.
Returns:
This method returns nothing.
Raises:
EFBMessageTypeNotSupported: Raised when message type is not supported by the channel.
"""
UserName = self.get_UserName(msg.destination['uid'])
if UserName is None or UserName is False:
raise EFBChatNotFound
r = None
self.logger.info("Sending message to WeChat:\n"
"Target-------\n"
"uid: %s\n"
"UserName: %s\n"
"NickName: %s\n"
"Type: %s\n"
"Text: %s"
% (msg.destination['uid'], UserName, msg.destination['name'], msg.type, msg.text))
if msg.type in [MsgType.Text, MsgType.Link]:
if msg.target:
if msg.target['type'] == TargetType.Member:
msg.text = "@%s\u2005 %s" % (msg.target['target'].member['alias'], msg.text)
elif msg.target['type'] == TargetType.Message:
maxl = self._flag("max_quote_length", -1)
qt_txt = "%s" % msg.target['target'].text
if maxl > 0:
tgt_text = qt_txt[:maxl]
if len(qt_txt) >= maxl:
tgt_text += "…"
tgt_text = "「%s」" % tgt_text
elif maxl < 0:
tgt_text = "「%s」" % qt_txt
else:
tgt_text = ""
if UserName.startswith("@@") and msg.target['target'].member:
tgt_alias = "@%s\u2005 " % msg.target['target'].member['alias']
else:
tgt_alias = ""
msg.text = "%s%s\n\n%s" % (tgt_alias, tgt_text, msg.text)
r = self._itchat_send_msg(msg.text, UserName)
elif msg.type in [MsgType.Image, MsgType.Sticker]:
self.logger.info("Image/Sticker %s, %s", msg.type, msg.path)
if msg.mime in ["image/gif", "image/jpeg"]:
try:
if os.path.getsize(msg.path) > 5 * 2 ** 20:
raise EFBMessageError("Image sent is too large. (IS01)")
self.logger.debug("Sending %s (image) to ItChat.", msg.path)
r = self._itchat_send_image(msg.path, UserName)
os.remove(msg.path)
except FileNotFoundError:
pass
else: # Convert Image format
img = Image.open(msg.path)
try:
alpha = img.split()[3]
mask = Image.eval(alpha, lambda a: 255 if a <= 128 else 0)
except IndexError:
mask = Image.eval(img.split()[0], lambda a: 0)
img = img.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255)
img.paste(255, mask)
img.save("%s.gif" % msg.path, transparency=255)
msg.path = "%s.gif" % msg.path
self.logger.info('Image converted to GIF: %s', msg.path)
self.logger.debug("Sending %s (image) to ItChat.", msg.path)
r = self._itchat_send_image(msg.path, UserName)
os.remove(msg.path)
if msg.text:
self._itchat_send_msg(msg.text, UserName)
self.logger.info('Image sent with result %s', r)
if not msg.mime == "image/gif":
try:
os.remove('.'.join(msg.path.split('.')[:-1]))
except FileNotFoundError:
pass
elif msg.type in (MsgType.File, MsgType.Audio):
self.logger.info("Sending %s to WeChat\nCaption: %s\nPath: %s\nFilename: %s", msg.type, msg.text, msg.path,
msg.filename)
r = self._itchat_send_file(msg.path, toUserName=UserName, filename=msg.filename)
if msg.text:
self._itchat_send_msg(msg.text, toUserName=UserName)
os.remove(msg.path)
elif msg.type == MsgType.Video:
self.logger.info("Sending video to WeChat\nCaption: %s\nPath: %s", msg.text, msg.path)
r = self._itchat_send_video(msg.path, UserName)
if msg.text:
self._itchat_send_msg(msg.text, UserName)
os.remove(msg.path)
else:
raise EFBMessageTypeNotSupported()
if isinstance(r, dict) and r.get('BaseResponse', dict()).get('Ret', -1) != 0:
if r.get('BaseResponse', dict()).get('Ret', -1) == 1101:
self.itchat.logout()
raise EFBMessageError(str(r))
else:
msg.uid = r.get("MsgID", None)
return msg
# Extra functions
@extra(name="Show chat list",
desc="Get a list of chat from WeChat.\n"
"Usage:\n {function_name} [-r]\n"
" -r: Force refresh")
def get_chat_list(self, param=""):
refresh = False
if param:
if param == "-r":
refresh = True
else:
return "Invalid command: %s." % param
l = []
for i in self.itchat.get_friends(refresh):
l.append(i)
l[-1]['Type'] = "User"
for i in self.itchat.get_chatrooms(refresh):
if not i['UserName'].startswith('@@'):
continue
l.append(i)
l[-1]['Type'] = "Group"
for i in self.itchat.get_mps(refresh):
l.append(i)
l[-1]['Type'] = "MP"
msg = "List of chats:\n"
for n, i in enumerate(l):
alias = self._wechat_html_unescape(i.get('RemarkName', '') or i.get('DisplayName', ''))
name = self._wechat_html_unescape(i.get('NickName', ''))
x = "%s (%s)" % (alias, name) if alias else name
msg += "\n%s: [%s] %s" % (n, x, i['Type'])
return msg
@extra(name="Set alias",
desc="Set alias for a contact in WeChat. You may not set alias to a group or a MP contact.\n"
"Usage:\n"
" {function_name} [-r] id [alias]\n"
" id: Chat ID (You may obtain it from \"Show chat list\" function.\n"
" alias: Alias to be set. Omit to remove.\n"
" -r: Force refresh")
def set_alias(self, param=""):
refresh = False
if param:
if param.startswith("-r "):
refresh = True
param = param[2:]
param = param.split(maxsplit=1)
if len(param) == 1:
cid = param[0]
alias = ""
else:
cid, alias = param
else:
return self.set_alias.desc
if not cid.isdecimal():
return "ID must be integer, \"%s\" given." % cid
else:
cid = int(cid)
l = self.itchat.get_friends(refresh)
if cid < 0:
return "ID must between 0 and %s inclusive, %s given." % (len(l) - 1, cid)
if cid >= len(l):
return "You may not set alias to a group or a MP contact."
self.itchat.set_alias(l[cid]['UserName'], alias)
if alias:
return "Chat \"%s\" is set with alias \"%s\"." % (l[cid]["NickName"], alias)
else:
return "Chat \"%s\" has removed its alias." % l[cid]["NickName"]
@extra(name="\"Uin\" rate.",
desc="For debug purpose only.\n"
"Usage: {function_name}")
def uin_rate(self, param=""):
groups_uin = 0
groups_all = 0
members_uin = 0
members_all = 0
for i in self.itchat.get_chatrooms(True):
groups_all += 1
if i.get("Uin", None):
groups_uin += 1
if not i.get('MemberList', ''):
i = self.itchat.update_chatroom(i.get('UserName', ''))
for j in i['MemberList']:
members_all += 1
if j.get("Uin", None):
members_uin += 1
users = self.itchat.get_friends(True) + self.itchat.get_mps(True)
users_uin = len([i for i in users if i.get("Uin", None)])
users_all = len(users) or 1
groups_all = groups_all or 1
members_all = members_all or 1
return "`Uin` rate checkup.\n\n" \
"Users + MP: %s/%s (%.2f%%)\n" \
"Groups: %s/%s (%.2f%%)\n" \
"Group Members: %s/%s (%.2f%%)" % \
(users_uin, users_all, 100 * users_uin / users_all,
groups_uin, groups_all, 100 * groups_uin / groups_all,
members_uin, members_all, 100 * members_uin / members_all)
@extra(name="Force log out",
desc="Force log out WeChat session.\n"
"Usage: {function_name}")
def force_log_out(self, param=""):
self.itchat.logout()
return "Done."
# Command functions
def reauth(self, command=False):
msg = "Starting authentication..."
qr_reload = self._flag("qr_reload", "master_qr_code")
if command and qr_reload == "console_qr_code":
msg += "\nPlease visit your console or log for QR code and further instructions."
def reauth_thread(self, qr_reload):
qr_callback = getattr(self, qr_reload, self.master_qr_code)
with self.mutex:
self.itchat.auto_login(enableCmdQR=2,
hotReload=True,
statusStorageDir="storage/%s.pkl" % self.channel_id,
exitCallback=self.exit_callback,
qrCallback=qr_callback)
self.done_reauth.set()
threading.Thread(target=reauth_thread, args=(self, qr_reload)).start()
return msg
def add_friend(self, UserName=None, status=2, ticket="", UserInfo={}):
if not UserName:
return "Username is empty. (UE02)"
try:
self.itchat.add_friend(UserName, status, ticket, UserInfo)
return "Success."
except:
return "Error occurred during the process. (AF01)"
def get_chats(self):
group = True
user = True
refresh = self._flag("refresh_friends", False)
r = []
if user:
t = self.itchat.get_friends(refresh) + self.itchat.get_mps(refresh)
t = [dict()] + t
t[0]['NickName'] = "File Helper"
t[0]['UserName'] = "filehelper"
t[0]['RemarkName'] = ""
t[0]['Uin'] = "filehelper"
for i in t:
if i['UserName'] == self.itchat.loginInfo['User']['UserName']:
continue
r.append({
'channel_name': self.channel_name,
'channel_id': self.channel_id,
'name': self._wechat_html_unescape(i['NickName']),
'alias': self._wechat_html_unescape(i['RemarkName'] or i['NickName']),
'uid': self.get_uid(UserName=i['UserName'],
NickName=self._wechat_html_unescape(i['NickName']),
alias=self._wechat_html_unescape(i.get("RemarkName", None)),
Uin=i.get("Uin", None)),
'type': MsgSource.User
})
if group:
t = self.itchat.get_chatrooms(refresh)
for i in t:
if not i['UserName'].startswith('@@'):
continue
r.append({
'channel_name': self.channel_name,
'channel_id': self.channel_id,
'name': self._wechat_html_unescape(i['NickName']),
'alias': self._wechat_html_unescape(i['RemarkName'] or i['NickName']),
'uid': self.get_uid(UserName=i['UserName'],
NickName=self._wechat_html_unescape(i['NickName']),
alias=self._wechat_html_unescape(i.get("RemarkName", None)),
Uin=i.get("Uin", None)),
'type': MsgSource.Group
})
return r
def get_chat(self, chat_id):
i = self.search_user(uid=chat_id)
if i:
i = i[0]
else:
raise KeyError("Chat not found.")
return {
'channel_name': self.channel_name,
'channel_id': self.channel_id,
'name': self._wechat_html_unescape(i['NickName']),
'alias': self._wechat_html_unescape(i['RemarkName'] or i['NickName']),
'uid': self.get_uid(UserName=i['UserName'],
NickName=self._wechat_html_unescape(i['NickName']),
alias=self._wechat_html_unescape(i.get("RemarkName", None)),
Uin=i.get("Uin", None)),
'type': MsgSource.Group if i['UserName'].startswith("@@") else MsgSource.User
}
def get_itchat(self):
return self.itchat
def _flag(self, key, value):
"""
Retrieve value for experimental flags.
Args:
key: Key of the flag.
value: Default/fallback value.
Returns:
Value for the flag.
"""
return getattr(config, self.channel_id, dict()).get('flags', dict()).get(key, value)
@property
def stop_polling(self):
return self._stop_polling
@stop_polling.setter
def stop_polling(self, val):
val = bool(val)
if val and not self.itchat.alive:
self.done_reauth.set()
self._stop_polling = val
def _itchat_send_msg(self, *args, **kwargs):
try:
return self.itchat.send_msg(*args, **kwargs)
except Exception as e:
raise EFBMessageError(repr(e))
def _itchat_send_file(self, *args, **kwargs):
def _itchat_send_fn(self, fileDir, toUserName=None, mediaId=None, filename=None):
from itchat.returnvalues import ReturnValue
from itchat import config
import os, time, json
if toUserName is None: toUserName = self.storageClass.userName
if mediaId is None:
r = self.upload_file(fileDir)
if r:
mediaId = r['MediaId']
else:
return r
fn = filename or os.path.basename(fileDir)
url = '%s/webwxsendappmsg?fun=async&f=json' % self.loginInfo['url']
data = {
'BaseRequest': self.loginInfo['BaseRequest'],
'Msg': {
'Type': 6,
'Content': (
"<appmsg appid='wxeb7ec651dd0aefa9' sdkver=''><title>%s</title>" % fn +
"<des></des><action></action><type>6</type><content></content><url></url><lowurl></lowurl>" +
"<appattach><totallen>%s</totallen><attachid>%s</attachid>" % (
str(os.path.getsize(fileDir)), mediaId) +
"<fileext>%s</fileext></appattach><extinfo></extinfo></appmsg>" %
os.path.splitext(fn)[1].replace('.', '')
),
'FromUserName': self.storageClass.userName,
'ToUserName': toUserName,
'LocalID': int(time.time() * 1e4),
'ClientMsgId': int(time.time() * 1e4), },
'Scene': 0, }
headers = {
'User-Agent': config.USER_AGENT,
'Content-Type': 'application/json;charset=UTF-8', }
r = self.s.post(url, headers=headers,