From c82c48d491e5b93a105cae00188f9c236b62478d Mon Sep 17 00:00:00 2001 From: tt-P607 <68868379+tt-P607@users.noreply.github.com> Date: Thu, 18 Sep 2025 15:06:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(napcat):=20=E6=94=AF=E6=8C=81=E9=9F=B3?= =?UTF-8?q?=E4=B9=90=E5=88=86=E4=BA=AB=E5=8D=A1=E7=89=87=E5=B9=B6=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=E6=B6=88=E6=81=AF=E5=85=BC=E5=AE=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增对 NapCat 框架音乐分享类型消息的解析支持。现在可以正确识别并提取音乐卡片中的歌曲、歌手、来源和封面等信息,并将其格式化为图文消息进行展示。 此外,增强了消息接收的兼容性。对于缺少 `post_type` 字段但包含 `message_type` 的普通消息,会自动补充 `post_type` 字段,确保其能被正常路由和处理,避免消息丢失。 --- .../built_in/napcat_adapter_plugin/plugin.py | 8 +++- .../src/recv_handler/message_handler.py | 37 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/plugins/built_in/napcat_adapter_plugin/plugin.py b/src/plugins/built_in/napcat_adapter_plugin/plugin.py index 952fcaccc..e15e17b8f 100644 --- a/src/plugins/built_in/napcat_adapter_plugin/plugin.py +++ b/src/plugins/built_in/napcat_adapter_plugin/plugin.py @@ -64,9 +64,15 @@ async def message_recv(server_connection: Server.ServerConnection): # 处理完整消息(可能是重组后的,也可能是原本就完整的) post_type = decoded_raw_message.get("post_type") + + # 兼容没有 post_type 的普通消息 + if not post_type and "message_type" in decoded_raw_message: + decoded_raw_message["post_type"] = "message" + post_type = "message" + if post_type in ["meta_event", "message", "notice"]: await message_queue.put(decoded_raw_message) - elif post_type is None: + else: await put_response(decoded_raw_message) except json.JSONDecodeError as e: diff --git a/src/plugins/built_in/napcat_adapter_plugin/src/recv_handler/message_handler.py b/src/plugins/built_in/napcat_adapter_plugin/src/recv_handler/message_handler.py index 0a644345b..f289af687 100644 --- a/src/plugins/built_in/napcat_adapter_plugin/src/recv_handler/message_handler.py +++ b/src/plugins/built_in/napcat_adapter_plugin/src/recv_handler/message_handler.py @@ -859,6 +859,43 @@ class MessageHandler: data=f"这是一条小程序分享消息,可以根据来源,考虑使用对应解析工具\n{formatted_content}", ) + # 检查是否是音乐分享 + elif nested_data.get("view") == "music" and "music" in nested_data.get("meta", {}): + logger.debug("检测到音乐分享消息,开始提取信息") + music_info = nested_data["meta"]["music"] + title = music_info.get("title", "未知歌曲") + desc = music_info.get("desc", "未知艺术家") + jump_url = music_info.get("jumpUrl", "") + preview_url = music_info.get("preview", "") + source = music_info.get("tag", "未知来源") + + # 优化文本结构,使其更像卡片 + text_parts = [ + "--- 音乐分享 ---", + f"歌曲:{title}", + f"歌手:{desc}", + f"来源:{source}" + ] + if jump_url: + text_parts.append(f"链接:{jump_url}") + text_parts.append("----------------") + + text_content = "\n".join(text_parts) + + # 如果有预览图,创建一个seglist包含文本和图片 + if preview_url: + try: + image_base64 = await get_image_base64(preview_url) + if image_base64: + return Seg(type="seglist", data=[ + Seg(type="text", data=text_content + "\n"), + Seg(type="image", data=image_base64) + ]) + except Exception as e: + logger.error(f"下载音乐预览图失败: {e}") + + return Seg(type="text", data=text_content) + # 如果没有提取到关键信息,返回None return None