From 15f856f951c9ed496e4088dc2ef981007e3c160e Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sun, 20 Apr 2025 00:27:04 +0800 Subject: [PATCH] =?UTF-8?q?perf(wecom):=20=E4=BC=81=E4=B8=9A=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E6=B7=BB=E5=8A=A0=E9=95=BF=E6=96=87=E6=9C=AC=E5=88=86?= =?UTF-8?q?=E5=89=B2=E5=8A=9F=E8=83=BD=E4=BB=A5=E6=94=AF=E6=8C=81=E5=8F=91?= =?UTF-8?q?=E9=80=81=E8=B6=85=E8=BF=87=202048=20=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E7=9A=84=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes: #564 --- .../platform/sources/wecom/wecom_event.py | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/astrbot/core/platform/sources/wecom/wecom_event.py b/astrbot/core/platform/sources/wecom/wecom_event.py index d8ee8b9a3..b42ddeac3 100644 --- a/astrbot/core/platform/sources/wecom/wecom_event.py +++ b/astrbot/core/platform/sources/wecom/wecom_event.py @@ -1,4 +1,5 @@ import uuid +import asyncio from astrbot.api.event import AstrMessageEvent, MessageChain from astrbot.api.platform import AstrBotMessage, PlatformMetadata from astrbot.api.message_components import Plain, Image, Record @@ -33,14 +34,31 @@ class WecomPlatformEvent(AstrMessageEvent): ): pass + async def split_plain(self, plain: str) -> list[str]: + """将长文本分割成多个小文本, 每个小文本长度不超过 2048 字符 + + Args: + plain (str): 要分割的长文本 + Returns: + list[str]: 分割后的文本列表 + """ + if len(plain) <= 2048: + return [plain] + else: + return [plain[i : i + 2048] for i in range(0, len(plain), 2048)] + async def send(self, message: MessageChain): message_obj = self.message_obj for comp in message.chain: if isinstance(comp, Plain): - self.client.message.send_text( - message_obj.self_id, message_obj.session_id, comp.text - ) + # Split long text messages if needed + plain_chunks = await self.split_plain(comp.text) + for chunk in plain_chunks: + self.client.message.send_text( + message_obj.self_id, message_obj.session_id, chunk + ) + await asyncio.sleep(0.5) # Avoid sending too fast elif isinstance(comp, Image): img_path = await comp.convert_to_file_path()