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 1/2] =?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() From 5cdec18863dce9b85b7319ffd21ca807120089f7 Mon Sep 17 00:00:00 2001 From: anka <1350989414@qq.com> Date: Sat, 19 Apr 2025 16:52:30 +0000 Subject: [PATCH 2/2] =?UTF-8?q?improvement:=20=E5=AF=B9=E6=A0=87=E7=82=B9?= =?UTF-8?q?=E7=AC=A6=E5=8F=B7=E5=88=86=E5=89=B2=E8=80=8C=E4=B8=8D=E6=98=AF?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E5=88=87=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../platform/sources/wecom/wecom_event.py | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/astrbot/core/platform/sources/wecom/wecom_event.py b/astrbot/core/platform/sources/wecom/wecom_event.py index b42ddeac3..8758f785d 100644 --- a/astrbot/core/platform/sources/wecom/wecom_event.py +++ b/astrbot/core/platform/sources/wecom/wecom_event.py @@ -45,7 +45,30 @@ class WecomPlatformEvent(AstrMessageEvent): if len(plain) <= 2048: return [plain] else: - return [plain[i : i + 2048] for i in range(0, len(plain), 2048)] + result = [] + start = 0 + while start < len(plain): + # 剩下的字符串长度<2048时结束 + if start + 2048 >= len(plain): + result.append(plain[start:]) + break + + # 向前搜索分割标点符号 + end = min(start + 2048, len(plain)) + cut_position = end + for i in range(end, start, -1): + if i < len(plain) and plain[i-1] in ["。", "!", "?", ".", "!", "?", "\n", ";", ";"]: + cut_position = i + break + + # 没找到合适的位置分割, 直接切分 + if cut_position == end and end < len(plain): + cut_position = end + + result.append(plain[start:cut_position]) + start = cut_position + + return result async def send(self, message: MessageChain): message_obj = self.message_obj