feat(napcat): 支持音乐分享卡片并增强消息兼容性
新增对 NapCat 框架音乐分享类型消息的解析支持。现在可以正确识别并提取音乐卡片中的歌曲、歌手、来源和封面等信息,并将其格式化为图文消息进行展示。 此外,增强了消息接收的兼容性。对于缺少 `post_type` 字段但包含 `message_type` 的普通消息,会自动补充 `post_type` 字段,确保其能被正常路由和处理,避免消息丢失。
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user