浏览代码

feat: itchat support joingroup message

master
lanvent 1年前
父节点
当前提交
6404332adc
共有 4 个文件被更改,包括 51 次插入9 次删除
  1. +1
    -0
      bridge/context.py
  2. +13
    -6
      channel/wechat/wechat_channel.py
  3. +27
    -2
      channel/wechat/wechat_message.py
  4. +10
    -1
      plugins/hello/hello.py

+ 1
- 0
bridge/context.py 查看文件

@@ -8,6 +8,7 @@ class ContextType(Enum):
VOICE = 2 # 音频消息
IMAGE = 3 # 图片消息
IMAGE_CREATE = 10 # 创建图片命令
JOIN_GROUP = 20 # 加入群聊

def __str__(self):
return self.name


+ 13
- 6
channel/wechat/wechat_channel.py 查看文件

@@ -28,18 +28,23 @@ from plugins import *

@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:
try:
cmsg = WeChatMessage(msg, False)
except NotImplementedError as e:
logger.debug("[WX]single message {} skipped: {}".format(msg["MsgId"], e))
return None
WechatChannel().handle_single(WeChatMessage(msg))
WechatChannel().handle_single(cmsg)
return None


@itchat.msg_register([TEXT, VOICE, PICTURE], isGroupChat=True)
@itchat.msg_register([TEXT, VOICE, PICTURE, NOTE], isGroupChat=True)
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
WechatChannel().handle_group(WeChatMessage(msg, True))
WechatChannel().handle_group(cmsg)
return None


@@ -186,6 +191,8 @@ class WechatChannel(ChatChannel):
logger.debug("[WX]receive voice for group msg: {}".format(cmsg.content))
elif cmsg.ctype == ContextType.IMAGE:
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:
# logger.debug("[WX]receive group msg: {}, cmsg={}".format(json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg))
pass


+ 27
- 2
channel/wechat/wechat_message.py 查看文件

@@ -1,3 +1,5 @@
import re

from bridge.context import ContextType
from channel.chat_message import ChatMessage
from common.log import logger
@@ -24,9 +26,31 @@ class WeChatMessage(ChatMessage):
self.ctype = ContextType.IMAGE
self.content = TmpDir().path() + itchat_msg["FileName"] # 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:
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"]
@@ -58,4 +82,5 @@ class WeChatMessage(ChatMessage):
if self.is_group:
self.is_at = itchat_msg["IsAt"]
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"]

+ 10
- 1
plugins/hello/hello.py 查看文件

@@ -23,7 +23,16 @@ class Hello(Plugin):
logger.info("[Hello] inited")

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

content = e_context["context"].content


正在加载...
取消
保存