@@ -8,6 +8,7 @@ class ContextType(Enum): | |||||
VOICE = 2 # 音频消息 | VOICE = 2 # 音频消息 | ||||
IMAGE = 3 # 图片消息 | IMAGE = 3 # 图片消息 | ||||
IMAGE_CREATE = 10 # 创建图片命令 | IMAGE_CREATE = 10 # 创建图片命令 | ||||
JOIN_GROUP = 20 # 加入群聊 | |||||
def __str__(self): | def __str__(self): | ||||
return self.name | return self.name | ||||
@@ -28,18 +28,23 @@ from plugins import * | |||||
@itchat.msg_register([TEXT, VOICE, PICTURE]) | @itchat.msg_register([TEXT, VOICE, PICTURE]) | ||||
def handler_single_msg(msg): | def handler_single_msg(msg): | ||||
# logger.debug("handler_single_msg: {}".format(msg)) | |||||
if msg["Type"] == PICTURE and msg["MsgType"] == 47: | |||||
try: | |||||
cmsg = WeChatMessage(msg, False) | |||||
except NotImplementedError as e: | |||||
logger.debug("[WX]single message {} skipped: {}".format(msg["MsgId"], e)) | |||||
return None | return None | ||||
WechatChannel().handle_single(WeChatMessage(msg)) | |||||
WechatChannel().handle_single(cmsg) | |||||
return None | return None | ||||
@itchat.msg_register([TEXT, VOICE, PICTURE], isGroupChat=True) | |||||
@itchat.msg_register([TEXT, VOICE, PICTURE, NOTE], isGroupChat=True) | |||||
def handler_group_msg(msg): | def handler_group_msg(msg): | ||||
if msg["Type"] == PICTURE and msg["MsgType"] == 47: | |||||
try: | |||||
cmsg = WeChatMessage(msg, True) | |||||
except NotImplementedError as e: | |||||
logger.debug("[WX]group message {} skipped: {}".format(msg["MsgId"], e)) | |||||
return None | return None | ||||
WechatChannel().handle_group(WeChatMessage(msg, True)) | |||||
WechatChannel().handle_group(cmsg) | |||||
return None | return None | ||||
@@ -186,6 +191,8 @@ class WechatChannel(ChatChannel): | |||||
logger.debug("[WX]receive voice for group msg: {}".format(cmsg.content)) | logger.debug("[WX]receive voice for group msg: {}".format(cmsg.content)) | ||||
elif cmsg.ctype == ContextType.IMAGE: | elif cmsg.ctype == ContextType.IMAGE: | ||||
logger.debug("[WX]receive image for group msg: {}".format(cmsg.content)) | logger.debug("[WX]receive image for group msg: {}".format(cmsg.content)) | ||||
elif cmsg.ctype == ContextType.JOIN_GROUP: | |||||
logger.debug("[WX]receive join group msg: {}".format(cmsg.content)) | |||||
else: | else: | ||||
# logger.debug("[WX]receive group msg: {}, cmsg={}".format(json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg)) | # logger.debug("[WX]receive group msg: {}, cmsg={}".format(json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg)) | ||||
pass | pass | ||||
@@ -1,3 +1,5 @@ | |||||
import re | |||||
from bridge.context import ContextType | from bridge.context import ContextType | ||||
from channel.chat_message import ChatMessage | from channel.chat_message import ChatMessage | ||||
from common.log import logger | from common.log import logger | ||||
@@ -24,9 +26,31 @@ class WeChatMessage(ChatMessage): | |||||
self.ctype = ContextType.IMAGE | self.ctype = ContextType.IMAGE | ||||
self.content = TmpDir().path() + itchat_msg["FileName"] # content直接存临时目录路径 | self.content = TmpDir().path() + itchat_msg["FileName"] # content直接存临时目录路径 | ||||
self._prepare_fn = lambda: itchat_msg.download(self.content) | self._prepare_fn = lambda: itchat_msg.download(self.content) | ||||
elif itchat_msg["Type"] == NOTE and itchat_msg["MsgType"] == 10000: | |||||
if is_group and ( | |||||
"加入群聊" in itchat_msg["Content"] or "加入了群聊" in itchat_msg["Content"] | |||||
): | |||||
self.ctype = ContextType.JOIN_GROUP | |||||
logger.debug("[WX]join group message: " + itchat_msg["Content"]) | |||||
self.content = itchat_msg["Content"] | |||||
# 这里只能得到nickname, actual_user_id还是机器人的id | |||||
if "加入了群聊" in itchat_msg["Content"]: | |||||
self.actual_user_nickname = re.findall( | |||||
r"\"(.*?)\"", itchat_msg["Content"] | |||||
)[-1] | |||||
elif "加入群聊" in itchat_msg["Content"]: | |||||
self.actual_user_nickname = re.findall( | |||||
r"\"(.*?)\"", itchat_msg["Content"] | |||||
)[0] | |||||
else: | |||||
raise NotImplementedError( | |||||
"Unsupported note message: " + itchat_msg["Content"] | |||||
) | |||||
else: | else: | ||||
raise NotImplementedError( | raise NotImplementedError( | ||||
"Unsupported message type: {}".format(itchat_msg["Type"]) | |||||
"Unsupported message type: Type:{} MsgType:{}".format( | |||||
itchat_msg["Type"], itchat_msg["MsgType"] | |||||
) | |||||
) | ) | ||||
self.from_user_id = itchat_msg["FromUserName"] | self.from_user_id = itchat_msg["FromUserName"] | ||||
@@ -58,4 +82,5 @@ class WeChatMessage(ChatMessage): | |||||
if self.is_group: | if self.is_group: | ||||
self.is_at = itchat_msg["IsAt"] | self.is_at = itchat_msg["IsAt"] | ||||
self.actual_user_id = itchat_msg["ActualUserName"] | self.actual_user_id = itchat_msg["ActualUserName"] | ||||
self.actual_user_nickname = itchat_msg["ActualNickName"] | |||||
if self.ctype != ContextType.JOIN_GROUP: | |||||
self.actual_user_nickname = itchat_msg["ActualNickName"] |
@@ -23,7 +23,16 @@ class Hello(Plugin): | |||||
logger.info("[Hello] inited") | logger.info("[Hello] inited") | ||||
def on_handle_context(self, e_context: EventContext): | def on_handle_context(self, e_context: EventContext): | ||||
if e_context["context"].type != ContextType.TEXT: | |||||
if e_context["context"].type not in [ContextType.TEXT, ContextType.JOIN_GROUP]: | |||||
return | |||||
if e_context["context"].type == ContextType.JOIN_GROUP: | |||||
e_context["context"].type = ContextType.TEXT | |||||
msg: ChatMessage = e_context["context"]["msg"] | |||||
e_context[ | |||||
"context" | |||||
].content = f'请你随机使用一种风格说一句问候语来欢迎新用户"{msg.actual_user_nickname}"加入群聊。' | |||||
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑 | |||||
return | return | ||||
content = e_context["context"].content | content = e_context["context"].content | ||||