feat(im): guide bot-not-in-chat recovery on message send#1740
feat(im): guide bot-not-in-chat recovery on message send#1740shifengjuan-dev wants to merge 1 commit into
Conversation
Sending a message with `--as bot --chat-id` to a group the bot is not a
member of fails with API error 230002 ("Bot/User can NOT be out of the
chat"). Previously the raw error was returned with no recovery path, so
callers (especially AI agents) treated the task as failed instead of
adding the bot and retrying.
Enrich the 230002 error on bot-identity group sends with an actionable
hint that includes the exact add-bot command (chat.members create with
member_id_type=app_id, --as user) filled with the bot's own app_id, then
retry. Non-230002 errors, P2P sends, and user-identity sends are left
unchanged.
Also promote the buried "bot must be in the chat" note in the
messages-send skill reference into an explicit precondition section and a
Common Errors table so the guidance is discoverable before the send.
📝 WalkthroughWalkthroughThis PR adds handling for Lark API error code 230002 ("Bot/User can NOT be out of chat") by introducing ChangesBot not-in-chat error enrichment
Estimated code review effort: 2 (Simple) | ~12 minutes Sequence Diagram(s)sequenceDiagram
participant ImMessagesSend
participant LarkAPI
participant enrichBotNotInChatErr
participant appendIMRecoveryHint
ImMessagesSend->>LarkAPI: POST /open-apis/im/v1/messages
LarkAPI-->>ImMessagesSend: error code 230002
alt runtime.IsBot()
ImMessagesSend->>enrichBotNotInChatErr: err, chatFlag, appID
enrichBotNotInChatErr->>enrichBotNotInChatErr: check code == 230002
enrichBotNotInChatErr->>appendIMRecoveryHint: attach chat.members create hint
appendIMRecoveryHint-->>ImMessagesSend: enriched error
else non-bot
ImMessagesSend-->>ImMessagesSend: return raw error
end
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
shortcuts/im/im_messages_send.go (1)
199-208: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAdd an
Execute-level test for the bot/230002 path.enrichBotNotInChatErris covered directly, but theruntime.IsBot()branch here isn’t; the current integration test only checks the raw code/message, not the recovery hint or the wiring intoExecute.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@shortcuts/im/im_messages_send.go` around lines 199 - 208, Add an Execute-level test that exercises the runtime.IsBot() branch in im_messages_send.go when the send API returns 230002, so the test verifies the bot recovery path is wired through Execute and not just enrichBotNotInChatErr directly. Use the existing SendMessage/Execute flow and mock runtime.DoAPIJSONTyped to return the 230002 failure, then assert the returned error includes the add-bot-then-retry recovery hint for the bot/removed-from-chat case.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@shortcuts/im/im_messages_send.go`:
- Around line 199-208: Add an Execute-level test that exercises the
runtime.IsBot() branch in im_messages_send.go when the send API returns 230002,
so the test verifies the bot recovery path is wired through Execute and not just
enrichBotNotInChatErr directly. Use the existing SendMessage/Execute flow and
mock runtime.DoAPIJSONTyped to return the 230002 failure, then assert the
returned error includes the add-bot-then-retry recovery hint for the
bot/removed-from-chat case.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 2566a477-0000-48fc-9e54-6c98f6971312
📒 Files selected for processing (4)
shortcuts/im/im_errors.goshortcuts/im/im_errors_test.goshortcuts/im/im_messages_send.goskills/lark-im/references/lark-im-messages-send.md
🚀 PR Preview Install Guide🧰 CLI updatenpm i -g https://pkg.pr.new/larksuite/cli/@larksuite/cli@5cd23f8ba006e0033ef49e7e20ec56777b9bd270🧩 Skill updatenpx skills add shifengjuan-dev/lark-cli#feat/im-send-bot-not-in-chat-hint -y -g |
What
Sending a message with
--as bot --chat-idto a group the bot is not a member of fails with API error 230002 (Bot/User can NOT be out of the chat). Previously the raw error was returned with no recovery path, so callers — especially AI agents driving the CLI — treated the task as failed instead of adding the bot and retrying.Changes
shortcuts/im/im_errors.go: newenrichBotNotInChatErrhelper that, on a 230002 error from a bot-identity group send, appends an actionable recovery hint via the existingappendIMRecoveryHint. The hint carries the exact add-bot command (chat.members createwithmember_id_type=app_id,--as user) pre-filled with the bot's ownapp_id(runtime.Config.AppID), plus a "retry the send" instruction. Non-230002 errors, P2P sends (nochat-id), user-identity sends, and nil are passed through unchanged, preserving classification / code / log_id.shortcuts/im/im_messages_send.go: wire the helper into the+messages-sendExecute error path, gated on bot identity.skills/lark-im/references/lark-im-messages-send.md: promote the buried "bot must be in the chat" note into an explicit Precondition section and a Common Errors and Troubleshooting table (following thechat-create.mdconvention), so the guidance is discoverable before the send.The hint renders in the error envelope through the standard
WriteTypedErrorEnvelope/Problem.Hintpath (the same mechanism every otherWithHintuses), so the recovery command appears directly in the output the caller reads.Tests
shortcuts/im/im_errors_test.go: added coverage for the helper — hint content + preserved code, empty-app_id placeholder, pass-through on other codes / empty chat-id, and nil. Written test-first (RED → GREEN).go build ./...,go vet ./shortcuts/im/,gofmtclean, andgo test ./shortcuts/im/ ./errs/... ./internal/output/...all pass.