Skip to content

Commit

Permalink
feat: itchat support picture msg
Browse files Browse the repository at this point in the history
  • Loading branch information
lanvent committed Apr 8, 2023
1 parent 3174b11 commit 8779eab
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
3 changes: 2 additions & 1 deletion bridge/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
class ContextType (Enum):
TEXT = 1 # 文本消息
VOICE = 2 # 音频消息
IMAGE_CREATE = 3 # 创建图片命令
IMAGE = 3 # 图片消息
IMAGE_CREATE = 10 # 创建图片命令

def __str__(self):
return self.name
Expand Down
2 changes: 2 additions & 0 deletions channel/chat_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ def _generate_reply(self, context: Context, reply: Reply = Reply()) -> Reply:
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
Expand Down
11 changes: 9 additions & 2 deletions channel/wechat/wechat_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@
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

Expand Down Expand Up @@ -127,6 +132,8 @@ def handle_single(self, cmsg : ChatMessage):
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)
Expand Down
4 changes: 4 additions & 0 deletions channel/wechat/wechat_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def __init__(self, itchat_msg, is_group=False):
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']))

Expand Down

0 comments on commit 8779eab

Please sign in to comment.