Files
abot/utils/message_formatter.py
2025-04-01 15:28:59 +08:00

39 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import xml.etree.ElementTree as ET
import html
def format_quote_message(xml_content):
"""
格式化引用消息
Args:
xml_content: XML格式的消息内容
Returns:
格式化后的消息文本
"""
try:
# 解析XML
root = ET.fromstring(xml_content)
# 获取主消息内容
title_elem = root.find('.//title')
main_content = title_elem.text if title_elem is not None else ""
# 获取引用消息信息
refer_msg = root.find('.//refermsg')
if refer_msg is not None:
display_name = refer_msg.find('displayname').text if refer_msg.find('displayname') is not None else "未知用户"
quoted_content = refer_msg.find('content').text if refer_msg.find('content') is not None else ""
# 解码HTML实体
quoted_content = html.unescape(quoted_content)
# 构建格式化的引用消息
formatted_message = f"┌─────────────────────────────────\n│ 引用 {display_name}{quoted_content}\n└─────────────────────────────────\n{main_content}"
return formatted_message
return main_content
except Exception as e:
return f"解析引用消息失败: {str(e)}"