@@ -9,6 +9,7 @@ class ContextType(Enum): | |||||
IMAGE = 3 # 图片消息 | IMAGE = 3 # 图片消息 | ||||
IMAGE_CREATE = 10 # 创建图片命令 | IMAGE_CREATE = 10 # 创建图片命令 | ||||
JOIN_GROUP = 20 # 加入群聊 | JOIN_GROUP = 20 # 加入群聊 | ||||
PATPAT = 21 # 拍了拍 | |||||
def __str__(self): | def __str__(self): | ||||
return self.name | return self.name | ||||
@@ -26,7 +26,7 @@ from lib.itchat.content import * | |||||
from plugins import * | from plugins import * | ||||
@itchat.msg_register([TEXT, VOICE, PICTURE]) | |||||
@itchat.msg_register([TEXT, VOICE, PICTURE, NOTE]) | |||||
def handler_single_msg(msg): | def handler_single_msg(msg): | ||||
try: | try: | ||||
cmsg = WeChatMessage(msg, False) | cmsg = WeChatMessage(msg, False) | ||||
@@ -170,12 +170,16 @@ class WechatChannel(ChatChannel): | |||||
logger.debug("[WX]receive voice msg: {}".format(cmsg.content)) | logger.debug("[WX]receive voice msg: {}".format(cmsg.content)) | ||||
elif cmsg.ctype == ContextType.IMAGE: | elif cmsg.ctype == ContextType.IMAGE: | ||||
logger.debug("[WX]receive image msg: {}".format(cmsg.content)) | logger.debug("[WX]receive image msg: {}".format(cmsg.content)) | ||||
else: | |||||
elif cmsg.ctype == ContextType.PATPAT: | |||||
logger.debug("[WX]receive patpat msg: {}".format(cmsg.content)) | |||||
elif cmsg.ctype == ContextType.TEXT: | |||||
logger.debug( | logger.debug( | ||||
"[WX]receive text msg: {}, cmsg={}".format( | "[WX]receive text msg: {}, cmsg={}".format( | ||||
json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg | json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg | ||||
) | ) | ||||
) | ) | ||||
else: | |||||
logger.debug("[WX]receive msg: {}, cmsg={}".format(cmsg.content, cmsg)) | |||||
context = self._compose_context( | context = self._compose_context( | ||||
cmsg.ctype, cmsg.content, isgroup=False, msg=cmsg | cmsg.ctype, cmsg.content, isgroup=False, msg=cmsg | ||||
) | ) | ||||
@@ -191,11 +195,13 @@ 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: | |||||
elif cmsg.ctype in [ContextType.JOIN_GROUP, ContextType.PATPAT]: | |||||
logger.debug("[WX]receive note msg: {}".format(cmsg.content)) | |||||
elif cmsg.ctype == ContextType.TEXT: | |||||
# 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 | ||||
else: | |||||
logger.debug("[WX]receive group msg: {}".format(cmsg.content)) | |||||
context = self._compose_context( | context = self._compose_context( | ||||
cmsg.ctype, cmsg.content, isgroup=True, msg=cmsg | cmsg.ctype, cmsg.content, isgroup=True, msg=cmsg | ||||
) | ) | ||||
@@ -31,7 +31,6 @@ class WeChatMessage(ChatMessage): | |||||
"加入群聊" in itchat_msg["Content"] or "加入了群聊" in itchat_msg["Content"] | "加入群聊" in itchat_msg["Content"] or "加入了群聊" in itchat_msg["Content"] | ||||
): | ): | ||||
self.ctype = ContextType.JOIN_GROUP | self.ctype = ContextType.JOIN_GROUP | ||||
logger.debug("[WX]join group message: " + itchat_msg["Content"]) | |||||
self.content = itchat_msg["Content"] | self.content = itchat_msg["Content"] | ||||
# 这里只能得到nickname, actual_user_id还是机器人的id | # 这里只能得到nickname, actual_user_id还是机器人的id | ||||
if "加入了群聊" in itchat_msg["Content"]: | if "加入了群聊" in itchat_msg["Content"]: | ||||
@@ -42,6 +41,13 @@ class WeChatMessage(ChatMessage): | |||||
self.actual_user_nickname = re.findall( | self.actual_user_nickname = re.findall( | ||||
r"\"(.*?)\"", itchat_msg["Content"] | r"\"(.*?)\"", itchat_msg["Content"] | ||||
)[0] | )[0] | ||||
elif "拍了拍我" in itchat_msg["Content"]: | |||||
self.ctype = ContextType.PATPAT | |||||
self.content = itchat_msg["Content"] | |||||
if is_group: | |||||
self.actual_user_nickname = re.findall( | |||||
r"\"(.*?)\"", itchat_msg["Content"] | |||||
)[0] | |||||
else: | else: | ||||
raise NotImplementedError( | raise NotImplementedError( | ||||
"Unsupported note message: " + itchat_msg["Content"] | "Unsupported note message: " + itchat_msg["Content"] | ||||
@@ -82,5 +88,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"] | ||||
if self.ctype != ContextType.JOIN_GROUP: | |||||
if self.ctype not in [ContextType.JOIN_GROUP, ContextType.PATPAT]: | |||||
self.actual_user_nickname = itchat_msg["ActualNickName"] | self.actual_user_nickname = itchat_msg["ActualNickName"] |
@@ -23,7 +23,11 @@ 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 not in [ContextType.TEXT, ContextType.JOIN_GROUP]: | |||||
if e_context["context"].type not in [ | |||||
ContextType.TEXT, | |||||
ContextType.JOIN_GROUP, | |||||
ContextType.PATPAT, | |||||
]: | |||||
return | return | ||||
if e_context["context"].type == ContextType.JOIN_GROUP: | if e_context["context"].type == ContextType.JOIN_GROUP: | ||||
@@ -35,6 +39,13 @@ class Hello(Plugin): | |||||
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑 | e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑 | ||||
return | return | ||||
if e_context["context"].type == ContextType.PATPAT: | |||||
e_context["context"].type = ContextType.TEXT | |||||
msg: ChatMessage = e_context["context"]["msg"] | |||||
e_context["context"].content = f"请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。" | |||||
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑 | |||||
return | |||||
content = e_context["context"].content | content = e_context["context"].content | ||||
logger.debug("[Hello] on_handle_context. content: %s" % content) | logger.debug("[Hello] on_handle_context. content: %s" % content) | ||||
if content == "Hello": | if content == "Hello": | ||||