Skip to content

feat(im): guide bot-not-in-chat recovery on message send#1740

Open
shifengjuan-dev wants to merge 1 commit into
larksuite:mainfrom
shifengjuan-dev:feat/im-send-bot-not-in-chat-hint
Open

feat(im): guide bot-not-in-chat recovery on message send#1740
shifengjuan-dev wants to merge 1 commit into
larksuite:mainfrom
shifengjuan-dev:feat/im-send-bot-not-in-chat-hint

Conversation

@shifengjuan-dev

@shifengjuan-dev shifengjuan-dev commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

What

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 driving the CLI — treated the task as failed instead of adding the bot and retrying.

Changes

  • shortcuts/im/im_errors.go: new enrichBotNotInChatErr helper that, on a 230002 error from a bot-identity group send, appends an actionable recovery hint via the existing appendIMRecoveryHint. The hint carries the exact add-bot command (chat.members create with member_id_type=app_id, --as user) pre-filled with the bot's own app_id (runtime.Config.AppID), plus a "retry the send" instruction. Non-230002 errors, P2P sends (no chat-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-send Execute 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 the chat-create.md convention), so the guidance is discoverable before the send.

The hint renders in the error envelope through the standard WriteTypedErrorEnvelope / Problem.Hint path (the same mechanism every other WithHint uses), 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/, gofmt clean, and go test ./shortcuts/im/ ./errs/... ./internal/output/... all pass.

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.
@github-actions github-actions Bot added domain/im PR touches the im domain size/M Single-domain feat or fix with limited business impact labels Jul 3, 2026
@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds handling for Lark API error code 230002 ("Bot/User can NOT be out of chat") by introducing enrichBotNotInChatErr in im_errors.go, wiring it into ImMessagesSend for bot-identity sends, adding unit tests, and updating skill documentation with a new precondition and troubleshooting entry.

Changes

Bot not-in-chat error enrichment

Layer / File(s) Summary
Error enrichment helper
shortcuts/im/im_errors.go
Adds botNotInChatCode constant and enrichBotNotInChatErr function that detects error code 230002 and attaches a CLI hint (using chatID/appID, with cli_xxx fallback) via appendIMRecoveryHint; other errors pass through unchanged.
Wiring into message send and tests
shortcuts/im/im_messages_send.go, shortcuts/im/im_errors_test.go
ImMessagesSend.Execute now enriches errors with enrichBotNotInChatErr only for bot identity sends; new tests cover hint enrichment, app ID placeholder, pass-through on other codes/empty chat ID, and nil input.
Skill documentation update
skills/lark-im/references/lark-im-messages-send.md
Adds a "Precondition: The Bot Must Be In The Chat" section and a troubleshooting table row describing error 230002 and the recover-and-retry workflow.

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
Loading

Suggested reviewers: haozhenghua-code, YangJunzhou-01, sammi-bytedance

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is concise and accurately highlights the main change: bot-not-in-chat recovery for message sends.
Description check ✅ Passed The description covers the motivation, changes, and tests, but it omits the template's Related Issues section.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
shortcuts/im/im_messages_send.go (1)

199-208: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Add an Execute-level test for the bot/230002 path. enrichBotNotInChatErr is covered directly, but the runtime.IsBot() branch here isn’t; the current integration test only checks the raw code/message, not the recovery hint or the wiring into Execute.

🤖 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

📥 Commits

Reviewing files that changed from the base of the PR and between a1506cd and 5cd23f8.

📒 Files selected for processing (4)
  • shortcuts/im/im_errors.go
  • shortcuts/im/im_errors_test.go
  • shortcuts/im/im_messages_send.go
  • skills/lark-im/references/lark-im-messages-send.md

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

🚀 PR Preview Install Guide

🧰 CLI update

npm i -g https://pkg.pr.new/larksuite/cli/@larksuite/cli@5cd23f8ba006e0033ef49e7e20ec56777b9bd270

🧩 Skill update

npx skills add shifengjuan-dev/lark-cli#feat/im-send-bot-not-in-chat-hint -y -g

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

domain/im PR touches the im domain size/M Single-domain feat or fix with limited business impact

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant