chore: merge itchat msg

This commit is contained in:
lanvent
2023-04-08 23:32:37 +08:00
parent 18740093d1
commit 3174b1158c

View File

@@ -23,26 +23,16 @@ from common.time_check import time_checker
from common.expired_dict import ExpiredDict from common.expired_dict import ExpiredDict
from plugins import * from plugins import *
@itchat.msg_register(TEXT) @itchat.msg_register([TEXT,VOICE])
def handler_single_msg(msg): def handler_single_msg(msg):
WechatChannel().handle_text(WeChatMessage(msg)) WechatChannel().handle_single(WeChatMessage(msg))
return None return None
@itchat.msg_register(TEXT, isGroupChat=True) @itchat.msg_register([TEXT,VOICE], isGroupChat=True)
def handler_group_msg(msg): def handler_group_msg(msg):
WechatChannel().handle_group(WeChatMessage(msg,True)) WechatChannel().handle_group(WeChatMessage(msg,True))
return None return None
@itchat.msg_register(VOICE)
def handler_single_voice(msg):
WechatChannel().handle_voice(WeChatMessage(msg))
return None
@itchat.msg_register(VOICE, isGroupChat=True)
def handler_group_voice(msg):
WechatChannel().handle_group_voice(WeChatMessage(msg,True))
return None
def _check(func): def _check(func):
def wrapper(self, cmsg: ChatMessage): def wrapper(self, cmsg: ChatMessage):
msgId = cmsg.msg_id msgId = cmsg.msg_id
@@ -118,7 +108,7 @@ class WechatChannel(ChatChannel):
# start message listener # start message listener
itchat.run() itchat.run()
# handle_* 系列函数处理收到的消息后构造Context然后传入_handle函数中处理Context和发送回复 # handle_* 系列函数处理收到的消息后构造Context然后传入produce函数中处理Context和发送回复
# Context包含了消息的所有信息包括以下属性 # Context包含了消息的所有信息包括以下属性
# type 消息类型, 包括TEXT、VOICE、IMAGE_CREATE # type 消息类型, 包括TEXT、VOICE、IMAGE_CREATE
# content 消息内容如果是TEXT类型content就是文本内容如果是VOICE类型content就是语音文件名如果是IMAGE_CREATE类型content就是图片生成命令 # content 消息内容如果是TEXT类型content就是文本内容如果是VOICE类型content就是语音文件名如果是IMAGE_CREATE类型content就是图片生成命令
@@ -132,37 +122,28 @@ class WechatChannel(ChatChannel):
@time_checker @time_checker
@_check @_check
def handle_voice(self, cmsg : ChatMessage): def handle_single(self, cmsg : ChatMessage):
if conf().get('speech_recognition') != True: if cmsg.ctype == ContextType.VOICE:
return if conf().get('speech_recognition') != True:
logger.debug("[WX]receive voice msg: {}".format(cmsg.content)) return
context = self._compose_context(ContextType.VOICE, cmsg.content, isgroup=False, msg=cmsg) logger.debug("[WX]receive voice msg: {}".format(cmsg.content))
if context: else:
self.produce(context) 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)
@time_checker
@_check
def handle_text(self, cmsg : ChatMessage):
logger.debug("[WX]receive text msg: {}, cmsg={}".format(json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg))
context = self._compose_context(ContextType.TEXT, cmsg.content, isgroup=False, msg=cmsg)
if context: if context:
self.produce(context) self.produce(context)
@time_checker @time_checker
@_check @_check
def handle_group(self, cmsg : ChatMessage): def handle_group(self, cmsg : ChatMessage):
logger.debug("[WX]receive group msg: {}, cmsg={}".format(json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg)) if cmsg.ctype == ContextType.VOICE:
context = self._compose_context(ContextType.TEXT, cmsg.content, isgroup=True, msg=cmsg) if conf().get('speech_recognition') != True:
if context: return
self.produce(context) logger.debug("[WX]receive voice for group msg: {}".format(cmsg.content))
else:
@time_checker # logger.debug("[WX]receive group msg: {}, cmsg={}".format(json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg))
@_check pass
def handle_group_voice(self, cmsg : ChatMessage): context = self._compose_context(cmsg.ctype, cmsg.content, isgroup=True, msg=cmsg)
if conf().get('group_speech_recognition', False) != True:
return
logger.debug("[WX]receive voice for group msg: {}".format(cmsg.content))
context = self._compose_context(ContextType.VOICE, cmsg.content, isgroup=True, msg=cmsg)
if context: if context:
self.produce(context) self.produce(context)