优化了处理语音消息和群聊图片消息,增加了发送语音消息(只能发送silk格式语音文件链接)和转发链接消息 (#1323)

* 优化了处理语音消息和处理群聊图片消息,增加了发送语音消息

* 增加了微信转发链接消息组件

* 增加了转发链接

* 修改字段内容手误问题

* 优化收到小程序,公众号转账等消息时将其通过unknown传递出来,并修复voice字段写错问题

* 移除有一处将数据当作base64处理并通过unknown中content(但是没有啊)传递。
This commit is contained in:
Junyan Qin (Chin)
2025-04-24 22:13:02 +08:00
committed by GitHub
2 changed files with 58 additions and 22 deletions

View File

@@ -58,6 +58,8 @@ class GewechatMessageConverter(adapter.MessageConverter):
elif isinstance(component, platform_message.WeChatLink):
content_list.append({'type': 'WeChatLink', 'link_title': component.link_title, 'link_desc': component.link_desc,
'link_thumb_url': component.link_thumb_url, 'link_url': component.link_url})
elif isinstance(component, platform_message.WeChatForwardLink):
content_list.append({'type': 'WeChatForwardLink', 'xml_data': component.xml_data})
elif isinstance(component, platform_message.Voice):
@@ -108,6 +110,9 @@ class GewechatMessageConverter(adapter.MessageConverter):
elif message["Data"]["MsgType"] == 3:
image_xml = message["Data"]["Content"]["string"]
if image_xml.startswith('wxid'): # 此处处理群聊发送图片会有微信id开头
xml_list = image_xml.split('\n')[2:]
image_xml = '\n'.join(xml_list)
if not image_xml:
return platform_message.MessageChain([
platform_message.Plain(text="[图片内容为空]")
@@ -135,14 +140,20 @@ class GewechatMessageConverter(adapter.MessageConverter):
platform_message.Plain(text=f"[图片处理失败]")
])
elif message["Data"]["MsgType"] == 34:
audio_base64 = message["Data"]["ImgBuf"]["buffer"]
return platform_message.MessageChain(
[platform_message.Voice(base64=f"data:audio/silk;base64,{audio_base64}")]
)
try:
audio_base64 = message["Data"]["ImgBuf"]["buffer"]
return platform_message.MessageChain(
[platform_message.Voice(base64=f"data:audio/silk;base64,{audio_base64}")]
)
except Exception as e:
return platform_message.MessageChain(
[platform_message.Plain(text="[无法解析群聊语音的消息]")] # 小测了一下免费版拿不到群聊语音消息的base64或者用什么办法解析xml里的url?
)
elif message["Data"]["MsgType"] == 49:
# 支持微信聊天记录的消息类型,将 XML 内容转换为 MessageChain 传递
content = message["Data"]["Content"]["string"]
try:
content = message["Data"]["Content"]["string"]
# content = message["Data"]["Content"]["string"]
# 有三种可能的消息结构weid开头私聊直接<?xml>和直接<msg>
if content.startswith('wxid'):
xml_list = content.split('\n')[2:]
@@ -182,43 +193,51 @@ class GewechatMessageConverter(adapter.MessageConverter):
return platform_message.MessageChain(message_list)
elif data_type == '51':
return platform_message.MessageChain(
[platform_message.Plain(text=f'[视频号消息]')]
[ # platform_message.Plain(text=f'[视频号消息]'),
platform_message.Unknown(text=content)]
)
# print(content_data)
elif data_type == '2000':
return platform_message.MessageChain(
[platform_message.Plain(text=f'[转账消息]')]
[ # platform_message.Plain(text=f'[转账消息]'),
platform_message.Unknown(text=content)]
)
elif data_type == '2001':
return platform_message.MessageChain(
[platform_message.Plain(text=f'[红包消息]')]
[ # platform_message.Plain(text=f'[红包消息]'),
platform_message.Unknown(text=content)]
)
elif data_type == '5':
return platform_message.MessageChain(
[platform_message.Plain(text=f'[公众号消息]')]
[ # platform_message.Plain(text=f'[公众号消息]'),
platform_message.Unknown(text=content)]
)
elif data_type == '33' or data_type == '36':
return platform_message.MessageChain(
[platform_message.Plain(text=f'[小程序消息]')]
[ # platform_message.Plain(text=f'[小程序消息]'),
platform_message.Unknown(text=content)]
)
# print(data_type.text)
else:
try:
content_bytes = content.encode('utf-8')
decoded_content = base64.b64decode(content_bytes)
return platform_message.MessageChain(
[platform_message.Unknown(content=decoded_content)]
)
except Exception as e:
return platform_message.MessageChain(
[platform_message.Plain(text=content)]
return platform_message.MessageChain(
[platform_message.Unknown(text=content)]
)
# try:
# content_bytes = content.encode('utf-8')
# decoded_content = base64.b64decode(content_bytes)
# return platform_message.MessageChain(
# [platform_message.Unknown(content=decoded_content)]
# ) # unknown中没有content
# except Exception as e:
# return platform_message.MessageChain(
# [platform_message.Plain(text=content)]
# )
except Exception as e:
print(f"Error processing type 49 message: {str(e)}")
return platform_message.MessageChain(
[platform_message.Plain(text="[无法解析的消息]")]
[ # platform_message.Plain(text="[无法解析的消息]"),
platform_message.Unknown(text=content)]
)
class GewechatEventConverter(adapter.EventConverter):
@@ -389,6 +408,11 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter):
,title=msg['link_title'], desc=msg['link_desc']
, link_url=msg['link_url'], thumb_url=msg['link_thumb_url'])
elif msg['type'] == 'WeChatForwardLink':
self.bot.forward_url(app_id=self.config['app_id'], to_wxid=target_id, xml=msg['xml_data'])
elif msg['type'] == 'voice':
self.bot.post_voice(app_id=self.config['app_id'], to_wxid=target_id, voice_url=msg['url'],voice_duration=msg['length'])
async def reply_message(
@@ -437,6 +461,11 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter):
self.bot.post_link(app_id=self.config['app_id'], to_wxid=target_id
, title=msg['link_title'], desc=msg['link_desc']
, link_url=msg['link_url'], thumb_url=msg['link_thumb_url'])
elif msg['type'] == 'WeChatForwardLink':
self.bot.forward_url(app_id=self.config['app_id'], to_wxid=target_id, xml=msg['xml_data'])
elif msg['type'] == 'voice':
self.bot.post_voice(app_id=self.config['app_id'], to_wxid=target_id, voice_url=msg['url'],
voice_duration=msg['length'])
async def is_muted(self, group_id: int) -> bool:
pass

View File

@@ -860,3 +860,10 @@ class WeChatLink(MessageComponent):
"""链接略缩图"""
link_thumb_url: str = ''
class WeChatForwardLink(MessageComponent):
"""转发链接。个人微信专用组件。"""
type: str = 'WeChatForwardLink'
"""xml数据"""
xml_data: str