-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_groupchat_info.py
120 lines (89 loc) · 3.69 KB
/
get_groupchat_info.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
# coding:utf8
import io
import sys
import itchat
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')
# itchat.auto_login(hotReload=True)
# #itchat.auto_login(hotReload=True)
# itchat.run()
# mpsList=itchat.get_chatrooms(update=True, contactOnly=True)
# #mpsList=itchat.get_chatrooms(update=True)[1:]
# total=0
# for it in mpsList:
# print(it['NickName'])
# total=total+1
# print('群聊的数目是:%d'%len(mpsList))
# 自动回复文本等类别消息
# isGroupChat=False表示非群聊消息
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING], isGroupChat=False)
def text_reply(msg):
itchat.send('这是我的小号,暂无调戏功能,有事请加我大号:Honlann', msg['FromUserName'])
# 自动回复图片等类别消息
# isGroupChat=False表示非群聊消息
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO], isGroupChat=False)
def download_files(msg):
itchat.send('这是我的小号,暂无调戏功能,有事请加我大号:Honlann', msg['FromUserName'])
# 自动处理添加好友申请
@itchat.msg_register(FRIENDS)
def add_friend(msg):
itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入,不需要重载通讯录
itchat.send_msg(u'你好哇', msg['RecommendInfo']['UserName'])
# 自动回复文本等类别的群聊消息
# isGroupChat=True表示为群聊消息
@itchat.msg_register([TEXT, SHARING], isGroupChat=True)
def group_reply_text(msg):
# 消息来自于哪个群聊
chatroom_id = msg['FromUserName']
# 发送者的昵称
username = msg['ActualNickName']
# 消息并不是来自于需要同步的群
if not chatroom_id in chatroom_ids:
return
if msg['Type'] == TEXT:
content = msg['Content']
elif msg['Type'] == SHARING:
content = msg['Text']
# 根据消息类型转发至其他需要同步消息的群聊
if msg['Type'] == TEXT:
for item in chatrooms:
if not item['UserName'] == chatroom_id:
itchat.send('%s\n%s' %
(username, msg['Content']), item['UserName'])
elif msg['Type'] == SHARING:
for item in chatrooms:
if not item['UserName'] == chatroom_id:
itchat.send('%s\n%s\n%s' % (
username, msg['Text'], msg['Url']), item['UserName'])
# 自动回复图片等类别的群聊消息
# isGroupChat=True表示为群聊消息
@itchat.msg_register([PICTURE, ATTACHMENT, VIDEO], isGroupChat=True)
def group_reply_media(msg):
# 消息来自于哪个群聊
chatroom_id = msg['FromUserName']
# 发送者的昵称
username = msg['ActualNickName']
# 消息并不是来自于需要同步的群
if not chatroom_id in chatroom_ids:
return
# 如果为gif图片则不转发
if msg['FileName'][-4:] == '.gif':
return
# 下载图片等文件
msg['Text'](msg['FileName'])
# 转发至其他需要同步消息的群聊
for item in chatrooms:
if not item['UserName'] == chatroom_id:
itchat.send('@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(
msg['Type'], 'fil'), msg['FileName']), item['UserName'])
# 扫二维码登录
itchat.auto_login(hotReload=True)
# 获取所有通讯录中的群聊
# 需要在微信中将需要同步的群聊都保存至通讯录
chatrooms = itchat.get_chatrooms(update=True, contactOnly=True)
chatroom_ids = [c['UserName'] for c in chatrooms]
print('正在监测的群聊:', len(chatrooms)), '个'
print(' '.join([item['NickName'] for item in chatrooms]))
# 开始监测
itchat.run()
# 显示所有的群聊,包括未保存在通讯录中的,如果去掉则只是显示在通讯录中保存的
itchat.dump_login_status()