对消息进行格式化
This commit is contained in:
38
utils/message_formatter.py
Normal file
38
utils/message_formatter.py
Normal file
@@ -0,0 +1,38 @@
|
||||
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)}"
|
||||
Reference in New Issue
Block a user