feat: 支持 gewechat 图片等更多类型的主动消息 #710

This commit is contained in:
Soulter
2025-03-06 22:26:58 +08:00
parent 24c20a19f1
commit 768398b991
2 changed files with 26 additions and 24 deletions
@@ -37,12 +37,9 @@ class GewechatPlatformEvent(AstrMessageEvent):
self.client = client
@staticmethod
async def send_with_client(message: MessageChain, user_name: str):
pass
async def send(self, message: MessageChain):
to_wxid = self.message_obj.raw_message.get("to_wxid", None)
async def send_with_client(
message: MessageChain, to_wxid: str, client: SimpleGewechatClient
):
if not to_wxid:
logger.error("无法获取到 to_wxid。")
return
@@ -70,7 +67,7 @@ class GewechatPlatformEvent(AstrMessageEvent):
payload["content"] = text
payload["ats"] = ats
has_at = True
await self.client.post_text(**payload)
await client.post_text(**payload)
elif isinstance(comp, Image):
img_url = comp.file
@@ -90,9 +87,9 @@ class GewechatPlatformEvent(AstrMessageEvent):
img_path = save_temp_img(f.read())
file_id = os.path.basename(img_path)
img_url = f"{self.client.file_server_url}/{file_id}"
img_url = f"{client.file_server_url}/{file_id}"
logger.debug(f"gewe callback img url: {img_url}")
await self.client.post_image(to_wxid, img_url)
await client.post_image(to_wxid, img_url)
elif isinstance(comp, Record):
# 默认已经存在 data/temp 中
record_url = comp.file
@@ -110,16 +107,14 @@ class GewechatPlatformEvent(AstrMessageEvent):
duration = await wav_to_tencent_silk(record_path, silk_path)
except Exception as e:
logger.error(traceback.format_exc())
await self.send(
MessageChain().message(f"语音文件转换失败。{str(e)}")
)
await client.post_text(to_wxid, f"语音文件转换失败。{str(e)}")
logger.info("Silk 语音文件格式转换至: " + record_path)
if duration == 0:
duration = get_wav_duration(record_path)
file_id = os.path.basename(silk_path)
record_url = f"{self.client.file_server_url}/{file_id}"
record_url = f"{client.file_server_url}/{file_id}"
logger.debug(f"gewe callback record url: {record_url}")
await self.client.post_voice(to_wxid, record_url, duration * 1000)
await client.post_voice(to_wxid, record_url, duration * 1000)
elif isinstance(comp, File):
file_path = comp.file
file_name = comp.name
@@ -131,12 +126,17 @@ class GewechatPlatformEvent(AstrMessageEvent):
file_path = file_path
file_id = os.path.basename(file_path)
file_url = f"{self.client.file_server_url}/{file_id}"
file_url = f"{client.file_server_url}/{file_id}"
logger.debug(f"gewe callback file url: {file_url}")
await self.client.post_file(to_wxid, file_url, file_id)
await client.post_file(to_wxid, file_url, file_id)
elif isinstance(comp, At):
pass
else:
logger.debug(f"gewechat 忽略: {comp.type}")
async def send(self, message: MessageChain):
to_wxid = self.message_obj.raw_message.get("to_wxid", None)
await GewechatPlatformEvent.send_with_client(
message, to_wxid, self.client
)
await super().send(message)
@@ -45,14 +45,16 @@ class GewechatPlatformAdapter(Platform):
async def send_by_session(
self, session: MessageSesion, message_chain: MessageChain
):
to_wxid = session.session_id
if not to_wxid:
logger.error("无法获取到 to_wxid。")
return
session_id = session.session_id
if "#" in session_id:
# unique session
to_wxid = session_id.split("#")[1]
else:
to_wxid = session_id
for comp in message_chain.chain:
if isinstance(comp, Plain):
await self.client.post_text(to_wxid, comp.text)
await GewechatPlatformEvent.send_with_client(
message_chain, to_wxid, self.client
)
await super().send_by_session(session, message_chain)
@@ -81,7 +83,7 @@ class GewechatPlatformAdapter(Platform):
async def handle_msg(self, message: AstrBotMessage):
if message.type == MessageType.GROUP_MESSAGE:
if self.settingss["unique_session"]:
message.session_id = message.sender.user_id + "_" + message.group_id
message.session_id = message.sender.user_id + "#" + message.group_id
message_event = GewechatPlatformEvent(
message_str=message.message_str,