Skip to content

fix(wecom): fallback to message API when kf returns 40096#7012

Merged
Soulter merged 2 commits intoAstrBotDevs:masterfrom
silwings1986:master
Mar 27, 2026
Merged

fix(wecom): fallback to message API when kf returns 40096#7012
Soulter merged 2 commits intoAstrBotDevs:masterfrom
silwings1986:master

Conversation

@silwings1986
Copy link
Copy Markdown
Contributor

@silwings1986 silwings1986 commented Mar 26, 2026

Summary

When kf/send_msg returns error 40096 (invalid external userid), fall back to regular message/send API. This handles internal WeCom employees who don't have external userids.

Changes

In astrbot/core/platform/sources/wecom/wecom_event.py:

  • Added WeChatClientException import from wechatpy.exceptions
  • Wrapped kf_message_api.send_text() in try/except block
  • On errcode == 40096: log warning and call self.client.message.send_text() instead

Testing

Production tested on zuju.hortorcreative.com - log shows:
kf API error 40096 for user WangCong, falling back to regular message API

Summary by Sourcery

Bug Fixes:

  • Ensure messages are still delivered to internal WeCom users by retrying via the regular message API when the kf API returns error 40096 (invalid external userid).

When sending WeCom messages via kf/send_msg, if the API returns error
40096 (invalid external userid), fall back to the regular message/send
API. This handles internal employees who don't have external userids.

Fixes the issue where internal WeCom users (e.g. WangCong) would cause
kf API to fail with 'invalid external userid' error.
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 26, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the robustness of WeCom message delivery by introducing a critical error handling mechanism. It ensures that messages are successfully sent to all recipients, including internal employees who might lack external user IDs, by automatically switching from the customer service API to the standard message API upon encountering a specific error code. This improvement prevents message delivery failures and maintains consistent communication within the WeCom platform.

Highlights

  • WeCom Message API Fallback: Implemented a fallback mechanism for the WeCom customer service (kf) message sending API. When the kf/send_msg API returns error code 40096 (invalid external userid), the system now gracefully falls back to using the regular message/send API to ensure message delivery.
  • Internal Employee Messaging: This change specifically addresses the issue of sending messages to internal WeCom employees who may not have external user IDs, ensuring they receive communications by utilizing the standard message API when the kf API fails due to an invalid external user ID.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider extracting the magic error code 40096 into a named constant (e.g. ERR_INVALID_EXTERNAL_USERID) near the WeCom integration code so the meaning is documented and easier to reuse or change.
  • In the warning log on fallback, it may be helpful to include e.errmsg or the exception string as well as the hardcoded code, so unexpected variations of the error are easier to diagnose.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider extracting the magic error code `40096` into a named constant (e.g. `ERR_INVALID_EXTERNAL_USERID`) near the WeCom integration code so the meaning is documented and easier to reuse or change.
- In the warning log on fallback, it may be helpful to include `e.errmsg` or the exception string as well as the hardcoded code, so unexpected variations of the error are easier to diagnose.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dosubot dosubot Bot added the area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. label Mar 26, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces error handling for sending text messages via the WeCom customer service API. It specifically catches WeChatClientException with errcode 40096 (invalid external user ID) and falls back to the regular WeCom message API, logging a warning. The review feedback suggests extending this fallback logic to other message types (Image, Record, File, Video) that also use kf_message_api.send_* methods, as they could potentially encounter the same 40096 error.

Comment on lines +99 to +107
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
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.

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Mar 27, 2026
@Soulter Soulter merged commit 2662788 into AstrBotDevs:master Mar 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants