Skip to content

Commit 8779eab

Browse files
committed
feat: itchat support picture msg
1 parent 3174b11 commit 8779eab

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

bridge/context.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
class ContextType (Enum):
66
TEXT = 1 # 文本消息
77
VOICE = 2 # 音频消息
8-
IMAGE_CREATE = 3 # 创建图片命令
8+
IMAGE = 3 # 图片消息
9+
IMAGE_CREATE = 10 # 创建图片命令
910

1011
def __str__(self):
1112
return self.name

channel/chat_channel.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ def _generate_reply(self, context: Context, reply: Reply = Reply()) -> Reply:
170170
reply = self._generate_reply(new_context)
171171
else:
172172
return
173+
elif context.type == ContextType.IMAGE: # 图片消息,当前无默认逻辑
174+
pass
173175
else:
174176
logger.error('[WX] unknown context type: {}'.format(context.type))
175177
return

channel/wechat/wechat_channel.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@
2323
from common.expired_dict import ExpiredDict
2424
from plugins import *
2525

26-
@itchat.msg_register([TEXT,VOICE])
26+
@itchat.msg_register([TEXT,VOICE,PICTURE])
2727
def handler_single_msg(msg):
28+
# logger.debug("handler_single_msg: {}".format(msg))
29+
if msg['Type'] == PICTURE and msg['MsgType'] == 47:
30+
return None
2831
WechatChannel().handle_single(WeChatMessage(msg))
2932
return None
3033

31-
@itchat.msg_register([TEXT,VOICE], isGroupChat=True)
34+
@itchat.msg_register([TEXT,VOICE,PICTURE], isGroupChat=True)
3235
def handler_group_msg(msg):
36+
if msg['Type'] == PICTURE and msg['MsgType'] == 47:
37+
return None
3338
WechatChannel().handle_group(WeChatMessage(msg,True))
3439
return None
3540

@@ -127,6 +132,8 @@ def handle_single(self, cmsg : ChatMessage):
127132
if conf().get('speech_recognition') != True:
128133
return
129134
logger.debug("[WX]receive voice msg: {}".format(cmsg.content))
135+
elif cmsg.ctype == ContextType.IMAGE:
136+
logger.debug("[WX]receive image msg: {}".format(cmsg.content))
130137
else:
131138
logger.debug("[WX]receive text msg: {}, cmsg={}".format(json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg))
132139
context = self._compose_context(cmsg.ctype, cmsg.content, isgroup=False, msg=cmsg)

channel/wechat/wechat_message.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def __init__(self, itchat_msg, is_group=False):
2222
self.ctype = ContextType.VOICE
2323
self.content = TmpDir().path() + itchat_msg['FileName'] # content直接存临时目录路径
2424
self._prepare_fn = lambda: itchat_msg.download(self.content)
25+
elif itchat_msg['Type'] == PICTURE and itchat_msg['MsgType'] == 3:
26+
self.ctype = ContextType.IMAGE
27+
self.content = TmpDir().path() + itchat_msg['FileName'] # content直接存临时目录路径
28+
self._prepare_fn = lambda: itchat_msg.download(self.content)
2529
else:
2630
raise NotImplementedError("Unsupported message type: {}".format(itchat_msg['Type']))
2731

0 commit comments

Comments
 (0)