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

61 lines
3.3 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
import re
def format_quote_message(xml_content):
"""
格式化引用消息
Args:
xml_content: XML格式的消息内容
Returns:
格式化后的消息文本
"""
try:
xml_content = xml_content.replace('&lt;', '<').replace('&gt;', '>')
print(xml_content)
# 使用正则表达式直接提取关键信息避免XML解析问题
title_match = re.search(r'<title>(.*?)</title>', xml_content)
main_content = title_match.group(1) if title_match else "[无标题]"
# 提取引用消息的发送者和内容
display_name_match = re.search(r'<displayname>(.*?)</displayname>', xml_content)
display_name = display_name_match.group(1) if display_name_match else "未知用户"
quoted_content_match = re.search(r'<refermsg>.*?<content>(.*?)</content>', xml_content, re.DOTALL)
quoted_content = quoted_content_match.group(1) if quoted_content_match else ""
# 解码HTML实体
try:
quoted_content = html.unescape(quoted_content)
except:
pass # 如果解码失败,使用原始内容
# 构建格式化的引用消息
if display_name and quoted_content:
formatted_message = f"{main_content}\n引用 {display_name}{quoted_content}"
return formatted_message
return main_content
except Exception as e:
# 如果解析失败尝试提取title标签内容
try:
match = re.search(r'<title>(.*?)</title>', xml_content)
if match:
return match.group(1)
except:
pass
return f"[引用消息]" # 返回一个简单的标识,而不是错误信息
if __name__ == '__main__':
strs ="""
<?xml version="1.0"?> <msg> <appmsg appid="" sdkver="0"> <title>那也没事,都是富二代,都是送出国镀个金回家继承家业的</title> <des /> <action /> <type>57</type> <showtype>0</showtype> <soundtype>0</soundtype> <mediatagname /> <messageext /> <messageaction /> <content /> <contentattr>0</contentattr> <url /> <lowurl /> <dataurl /> <lowdataurl /> <songalbumurl /> <songlyric /> <appattach> <totallen>0</totallen> <attachid /> <emoticonmd5></emoticonmd5> <fileext /> <aeskey></aeskey> </appattach> <extinfo /> <sourceusername /> <sourcedisplayname /> <thumburl /> <md5 /> <statextstr /> <refermsg> <type>1</type> <svrid>2568355911763278189</svrid> <fromusr>45317011307@chatroom</fromusr> <chatusr>wxid_twrbhdxddlud12</chatusr> <displayname>水牛🐃</displayname> <content>都是富二代。</content> <msgsource>&lt;msgsource&gt; &lt;sec_msg_node&gt; &lt;alnode&gt; &lt;fr&gt;1&lt;/fr&gt; &lt;/alnode&gt; &lt;/sec_msg_node&gt; &lt;pua&gt;1&lt;/pua&gt; &lt;silence&gt;1&lt;/silence&gt; &lt;membercount&gt;125&lt;/membercount&gt; &lt;signature&gt;N0_V1_DaX/Y3s2|v1_PvH3m56P&lt;/signature&gt; &lt;tmp_node&gt; &lt;publisher-id&gt;&lt;/publisher-id&gt; &lt;/tmp_node&gt; &lt;/msgsource&gt; </msgsource> <createtime>1743491847</createtime> </refermsg> </appmsg> <fromusername>maoyijie</fromusername> <scene>0</scene> <appinfo> <version>1</version> <appname /> </appinfo> <commenturl /> </msg>
"""
sss = format_quote_message(strs)
print(sss)