Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion astrbot/core/platform/sources/wecom/wecom_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os

from wechatpy.enterprise import WeChatClient
from wechatpy.exceptions import WeChatClientException

from astrbot.api import logger
from astrbot.api.event import AstrMessageEvent, MessageChain
Expand Down Expand Up @@ -95,7 +96,19 @@ async def send(self, message: MessageChain) -> None:
# Split long text messages if needed
plain_chunks = await self.split_plain(comp.text)
for chunk in plain_chunks:
kf_message_api.send_text(user_id, self.get_self_id(), chunk)
try:
kf_message_api.send_text(user_id, self.get_self_id(), chunk)
except WeChatClientException as e:
if getattr(e, "errcode", None) == 40096:
# 40096: invalid external userid, fallback to regular message API
logger.warning(
f"kf API error 40096 for user {user_id}, falling back to regular message API"
)
self.client.message.send_text(
self.get_self_id(), user_id, chunk
)
else:
raise
Comment on lines +99 to +111
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The fallback logic for errcode 40096 is currently implemented only for Plain (text) messages. However, other message types such as Image, Record, File, and Video also use kf_message_api.send_* methods, which could potentially return the same 40096 error. To ensure a comprehensive solution, consider applying similar try...except blocks with fallback logic to these other message types as well.

await asyncio.sleep(0.5) # Avoid sending too fast
elif isinstance(comp, Image):
img_path = await comp.convert_to_file_path()
Expand Down