From 8779eab36ba4708bc432e9c3aeb8b4e66e570d9b Mon Sep 17 00:00:00 2001 From: lanvent Date: Sun, 9 Apr 2023 00:45:42 +0800 Subject: [PATCH] feat: itchat support picture msg --- bridge/context.py | 3 ++- channel/chat_channel.py | 2 ++ channel/wechat/wechat_channel.py | 11 +++++++++-- channel/wechat/wechat_message.py | 4 ++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/bridge/context.py b/bridge/context.py index 50be100..7283333 100644 --- a/bridge/context.py +++ b/bridge/context.py @@ -5,7 +5,8 @@ from enum import Enum class ContextType (Enum): TEXT = 1 # 文本消息 VOICE = 2 # 音频消息 - IMAGE_CREATE = 3 # 创建图片命令 + IMAGE = 3 # 图片消息 + IMAGE_CREATE = 10 # 创建图片命令 def __str__(self): return self.name diff --git a/channel/chat_channel.py b/channel/chat_channel.py index 815b201..5000ae3 100644 --- a/channel/chat_channel.py +++ b/channel/chat_channel.py @@ -170,6 +170,8 @@ class ChatChannel(Channel): reply = self._generate_reply(new_context) else: return + elif context.type == ContextType.IMAGE: # 图片消息,当前无默认逻辑 + pass else: logger.error('[WX] unknown context type: {}'.format(context.type)) return diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 5a91d7a..36f6157 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -23,13 +23,18 @@ from common.time_check import time_checker from common.expired_dict import ExpiredDict from plugins import * -@itchat.msg_register([TEXT,VOICE]) +@itchat.msg_register([TEXT,VOICE,PICTURE]) def handler_single_msg(msg): + # logger.debug("handler_single_msg: {}".format(msg)) + if msg['Type'] == PICTURE and msg['MsgType'] == 47: + return None WechatChannel().handle_single(WeChatMessage(msg)) return None -@itchat.msg_register([TEXT,VOICE], isGroupChat=True) +@itchat.msg_register([TEXT,VOICE,PICTURE], isGroupChat=True) def handler_group_msg(msg): + if msg['Type'] == PICTURE and msg['MsgType'] == 47: + return None WechatChannel().handle_group(WeChatMessage(msg,True)) return None @@ -127,6 +132,8 @@ class WechatChannel(ChatChannel): if conf().get('speech_recognition') != True: return logger.debug("[WX]receive voice msg: {}".format(cmsg.content)) + elif cmsg.ctype == ContextType.IMAGE: + logger.debug("[WX]receive image msg: {}".format(cmsg.content)) else: logger.debug("[WX]receive text msg: {}, cmsg={}".format(json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg)) context = self._compose_context(cmsg.ctype, cmsg.content, isgroup=False, msg=cmsg) diff --git a/channel/wechat/wechat_message.py b/channel/wechat/wechat_message.py index 43050f8..bc12b43 100644 --- a/channel/wechat/wechat_message.py +++ b/channel/wechat/wechat_message.py @@ -22,6 +22,10 @@ class WeChatMessage(ChatMessage): self.ctype = ContextType.VOICE self.content = TmpDir().path() + itchat_msg['FileName'] # content直接存临时目录路径 self._prepare_fn = lambda: itchat_msg.download(self.content) + elif itchat_msg['Type'] == PICTURE and itchat_msg['MsgType'] == 3: + self.ctype = ContextType.IMAGE + self.content = TmpDir().path() + itchat_msg['FileName'] # content直接存临时目录路径 + self._prepare_fn = lambda: itchat_msg.download(self.content) else: raise NotImplementedError("Unsupported message type: {}".format(itchat_msg['Type']))