Skip to content

fix: handle unavailable aiocqhttp api sends#8922

Open
EterUltimate wants to merge 1 commit into
AstrBotDevs:masterfrom
EterUltimate:codex/fix-aiocqhttp-api-unavailable
Open

fix: handle unavailable aiocqhttp api sends#8922
EterUltimate wants to merge 1 commit into
AstrBotDevs:masterfrom
EterUltimate:codex/fix-aiocqhttp-api-unavailable

Conversation

@EterUltimate

@EterUltimate EterUltimate commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Modifications / 改动点

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Handle aiocqhttp API unavailability more gracefully by falling back to event-based sending and skipping messages instead of raising errors when necessary.

Bug Fixes:

  • Ensure aiocqhttp message sending falls back to event-based send when primary group/private send APIs are unavailable.
  • Avoid raising errors when aiocqhttp APIs are unavailable and no event is provided, skipping the message instead while logging a warning.

Tests:

  • Add unit tests covering fallback to event-based send and non-raising behavior when aiocqhttp APIs are unavailable.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. labels Jun 20, 2026

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

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.

Code Review

This pull request introduces robust error handling for ApiNotAvailable exceptions in the aiocqhttp message dispatcher, falling back to bot.send when the primary sending methods fail, and logging a warning instead of raising an unhandled exception. It also adds unit tests to verify this fallback behavior. The review feedback suggests extending this error handling to forward messages that use bot.call_action and adding an additional test case to cover the scenario where both the primary and fallback send attempts fail.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

raise ValueError(
f"无法发送消息:缺少有效的数字 session_id({session_id}) 或 event({event})",
)
except ApiNotAvailable:

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

While handling ApiNotAvailable in _dispatch_send is a great improvement, the send_message method also calls bot.call_action directly for forward messages (when isinstance(seg, Node | Nodes)) without wrapping them in a try...except ApiNotAvailable block. If the API is unavailable when sending a forward message, it will still raise an unhandled ApiNotAvailable exception.

Consider wrapping those bot.call_action calls in send_message with a similar try...except ApiNotAvailable block to ensure consistent error handling across all message types.

user_id=987654,
message=[{"type": "text", "data": {"text": "hello"}}],
)
bot.send.assert_not_awaited()

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

To ensure full test coverage of the fallback failure path (where both the primary send and the fallback bot.send fail with ApiNotAvailable), we should add a test case verifying that the exception is caught and does not propagate.

    bot.send.assert_not_awaited()


@pytest.mark.asyncio
async def test_aiocqhttp_send_message_both_apis_unavailable():
    bot = AsyncMock()
    bot.send_private_msg.side_effect = ApiNotAvailable
    bot.send.side_effect = ApiNotAvailable
    event = Event.from_payload(
        {
            "post_type": "message",
            "message_type": "private",
            "sub_type": "friend",
            "self_id": 12345,
            "user_id": 987654,
            "message_id": 1,
            "message": [],
        }
    )
    chain = MessageChain([Comp.Plain("hello")])

    await AiocqhttpMessageEvent.send_message(
        bot=bot,
        message_chain=chain,
        event=event,
        is_group=False,
        session_id="987654",
    )

    bot.send_private_msg.assert_awaited_once()
    bot.send.assert_awaited_once()

@sourcery-ai sourcery-ai Bot left a comment

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.

Hey - I've reviewed your changes and they look great!


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.

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. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant