优化视频和音乐消息处理,返回结构化数据格式,增加小程序分享消息的处理
parent
250be48ea8
commit
af5b7f1a92
|
|
@ -468,23 +468,23 @@ class MessageHandler:
|
||||||
Parameters:
|
Parameters:
|
||||||
raw_message: dict: 原始消息
|
raw_message: dict: 原始消息
|
||||||
Returns:
|
Returns:
|
||||||
seg_data: Seg: 处理后的消息段
|
seg_data: Seg: 处理后的消息段(video_card类型)
|
||||||
"""
|
"""
|
||||||
message_data: dict = raw_message.get("data")
|
message_data: dict = raw_message.get("data")
|
||||||
file: str = message_data.get("file")
|
file: str = message_data.get("file", "")
|
||||||
url: str = message_data.get("url")
|
url: str = message_data.get("url", "")
|
||||||
file_size: str = message_data.get("file_size", "未知大小")
|
file_size: str = message_data.get("file_size", "")
|
||||||
|
|
||||||
if not file:
|
if not file:
|
||||||
logger.warning("视频消息缺少文件信息")
|
logger.warning("视频消息缺少文件信息")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# 视频消息返回文本描述,包含文件名和大小
|
# 返回结构化的视频卡片数据
|
||||||
video_text = f"[视频: {file}, 大小: {file_size}字节]"
|
return Seg(type="video_card", data={
|
||||||
if url:
|
"file": file,
|
||||||
video_text += f"\n视频链接: {url}"
|
"file_size": file_size,
|
||||||
|
"url": url
|
||||||
return Seg(type="text", data=video_text)
|
})
|
||||||
|
|
||||||
async def handle_json_message(self, raw_message: dict) -> Seg | None:
|
async def handle_json_message(self, raw_message: dict) -> Seg | None:
|
||||||
"""
|
"""
|
||||||
|
|
@ -544,13 +544,44 @@ class MessageHandler:
|
||||||
# 尝试从music字段提取信息
|
# 尝试从music字段提取信息
|
||||||
if music:
|
if music:
|
||||||
title = music.get("title", "")
|
title = music.get("title", "")
|
||||||
singer = music.get("singer", "")
|
singer = music.get("desc", "") or music.get("singer", "")
|
||||||
music_text = "[音乐卡片]"
|
jump_url = music.get("jumpUrl", "") or music.get("jump_url", "")
|
||||||
if title:
|
music_url = music.get("musicUrl", "") or music.get("music_url", "")
|
||||||
music_text += f"\n歌曲: {title}"
|
tag = music.get("tag", "") # 音乐来源标签,如"网易云音乐"
|
||||||
if singer:
|
preview = music.get("preview", "") # 封面图URL
|
||||||
music_text += f"\n歌手: {singer}"
|
|
||||||
return Seg(type="text", data=music_text)
|
# 返回结构化的音乐卡片数据
|
||||||
|
return Seg(type="music_card", data={
|
||||||
|
"title": title,
|
||||||
|
"singer": singer,
|
||||||
|
"jump_url": jump_url,
|
||||||
|
"music_url": music_url,
|
||||||
|
"tag": tag,
|
||||||
|
"preview": preview
|
||||||
|
})
|
||||||
|
|
||||||
|
# 检查是否为小程序分享(如B站视频分享)
|
||||||
|
if app == "com.tencent.miniapp_01":
|
||||||
|
meta = parsed_json.get("meta", {})
|
||||||
|
detail = meta.get("detail_1", {})
|
||||||
|
|
||||||
|
if detail:
|
||||||
|
title = detail.get("title", "") # 小程序名称,如"哔哩哔哩"
|
||||||
|
desc = detail.get("desc", "") # 分享内容描述
|
||||||
|
url = detail.get("url", "") # 小程序链接
|
||||||
|
qqdocurl = detail.get("qqdocurl", "") # 原始链接(如B站链接)
|
||||||
|
preview = detail.get("preview", "") # 预览图
|
||||||
|
icon = detail.get("icon", "") # 小程序图标
|
||||||
|
|
||||||
|
# 返回结构化的小程序卡片数据
|
||||||
|
return Seg(type="miniapp_card", data={
|
||||||
|
"title": title,
|
||||||
|
"desc": desc,
|
||||||
|
"url": url,
|
||||||
|
"source_url": qqdocurl,
|
||||||
|
"preview": preview,
|
||||||
|
"icon": icon
|
||||||
|
})
|
||||||
|
|
||||||
# 其他卡片消息使用prompt字段
|
# 其他卡片消息使用prompt字段
|
||||||
prompt = parsed_json.get("prompt", "[卡片消息]")
|
prompt = parsed_json.get("prompt", "[卡片消息]")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue