Skip to content

fix(instagram-facebook): send private replies via the page node again - #1122

Merged
sung17 merged 1 commit into
mainfrom
fix/instagram-private-reply-page-node
Sep 9, 2026
Merged

sung17 merged 1 commit into
mainfrom
fix/instagram-private-reply-page-node

Conversation

@sung17

@sung17 sung17 commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Instagram private replies on the via-Facebook-Login channel all failed with (#3) Application does not have the capability to make this API call. The app was not at fault — it holds instagram_manage_messages, pages_messaging, instagram_manage_comments and Human Agent at Advanced Access, and normal DMs kept working. Meta simply does not expose the messages edge on the IG node for this login type, so {igId}/messages can never succeed; the reply has to go to {pageId}/messages.
  • This is the second time this fix has been needed. fix(instagram-facebook): send private reply via page node, not igId #875 moved the endpoint to pageId; perf(builder): optimize build pipeline and gate types in CI #945 moved it back to green a stale test whose fixture carried no pageId at all, so the endpoint silently became /undefined/messages while the assertion on the IG node kept passing. The test is now the guard rather than the cause.
  • Blast radius was wider than comment automation: the automation's text and AIAgent replies, the first message of a flow reply, and the agent's manual private reply from the inbox (a separate entry point via handlers/comment/outgoing-private-reply).

Changes

  • integrations/instagram-facebook/src/apis/comment.ts — sendPrivateReplyMessage posts to {pageId}/messages. Guards a missing pageId explicitly instead of building /undefined/messages (the silent failure mode that hid the regression), and is async so that guard rejects rather than throwing synchronously. The doc comment now records why the Page node is correct and warns against reverting.
  • integrations/instagram-facebook/__tests__/send-private-reply.test.ts — fixture carries a pageId distinct from igId (the old one could not tell the two nodes apart), asserts the Page node, adds a regression case asserting the IG node is never called, and covers the missing-pageId guard.
  • integrations/instagram-facebook/src/lib/error-mapper.ts, integrations/instagram/src/lib/error-mapper.ts — code 3 was in neither Instagram mapper, so it fell through to the type === "OAuthException" fallback and was reported as AUTH_FAILED, pointing operators at a reconnect that could never help. It is a capability problem, so it now maps to PERMISSION_DENIED (permanent), as integrations/messenger already does.
  • integrations/instagram-facebook/src/lib/error-mapper.ts — isRevokedTokenError matched any OAuthException, including code 3, making the disconnect flow skip its remote teardown for unrelated errors. Narrowed to code 190 plus a revoked subcode, matching the Instagram Login variant (which excludes a bare 190 as ambiguous to avoid false-positive disconnects).
  • integrations/instagram-facebook/__tests__/error-mapper.test.ts (new) — pins both of the above.
  • apps/worker/.../received-message.ts — unrelated, found in the same log: the getProfile failure logged under error, so pino dropped the stack trace. Keyed to err per the structured-logging invariant.
  • Docs — docs/fb-comment-automation.md, the integration README, and the fb-comment-automation skill all record the Page-vs-IG node rule and the fix(instagram-facebook): send private reply via page node, not igId #875 → perf(builder): optimize build pipeline and gate types in CI #945 history, so the next reader does not "fix" it back. Also notes that both Instagram packages log module=integration-instagram, so production failures must be attributed by request host, not module name.

Not in this PR

apis/page.ts reads the API version off auth.version, which the connect flow never writes (it only writes auth.metadata.version), so sendInstagramMessage is pinned to DEFAULT_API_VERSION (v23.0) while comment.ts/attachment.ts use the configured v25.0. Documented in the README; fixing it changes the version of a working send path, so it deserves its own change and verification.

Test plan

  • pnpm lint (6297 files, includes i18n parity + schema drift)
  • pnpm --filter @chatbotx.io/integration-instagram-facebook check-types
  • pnpm --filter @chatbotx.io/integration-instagram check-types
  • pnpm --filter worker check-types
  • pnpm --filter builder check-types
  • pnpm --filter @chatbotx.io/integration-instagram-facebook test — 63 passed
  • pnpm --filter @chatbotx.io/integration-instagram test — 81 passed
  • pnpm --filter worker test — 2094 passed
  • pnpm --filter builder test — 3325 passed
  • Confirm against Meta with a real Page access token before merging. Meta's own docs disagree between the Instagram Messaging page (/<PAGE_ID>/messages) and the unified Instagram Platform page (/<APP_USERS_IG_ID>/messages), and this file has already flip-flopped twice, so one manual call settles it. Same comment_id (inside the 7-day window), same v25.0, both nodes: <PAGE_ID> should return recipient_id + message_id; <IG_ID> should reproduce #3.
  • Post-deploy, exercise all three entry points: automation private reply text, AIAgent, flow (first message), and the agent's manual private reply from the inbox (text and with an attachment)

sendPrivateReplyMessage posted to `{igId}/messages`, which Meta rejects with
`(#3) Application does not have the capability to make this API call.` even when
the app holds instagram_manage_messages, pages_messaging and Human Agent at
Advanced Access. For Instagram via Facebook Login the `messages` edge only
exists on the Page node, so every private reply on that channel failed: the
automation's `text` and `AIAgent` replies, the first message of a `flow` reply,
and the agent's manual private reply from the inbox.

This is the second time the fix has been needed. #875 moved the endpoint to
`pageId`, then #945 moved it back to green a stale test whose fixture carried no
`pageId` at all — so the endpoint silently became `/undefined/messages` and the
assertion on the IG node kept passing. The test is now the guard instead of the
cause: the fixture carries a `pageId` distinct from `igId`, and a new case
asserts the IG node is never called.

Also guards a missing `pageId` explicitly rather than building
`/undefined/messages`, which is the silent failure mode that hid the regression.

Two adjacent error-mapping bugs surfaced while tracing this:

- Code 3 was in neither Instagram mapper, so it fell through to the
  `type === "OAuthException"` fallback and was reported as AUTH_FAILED —
  pointing operators at a reconnect that could never help. It is a capability
  problem, so it now maps to PERMISSION_DENIED (permanent), as messenger
  already does.
- instagram-facebook's isRevokedTokenError matched *any* OAuthException,
  including code 3, making the disconnect flow skip its remote teardown for
  unrelated errors. Narrowed to code 190 plus a revoked subcode, matching the
  Instagram Login variant, which excludes a bare 190 as ambiguous to avoid
  false-positive disconnects.

Unrelated, found in the same log: the getProfile failure in received-message.ts
logged under `error`, so pino dropped the stack trace. Keyed to `err`.
@github-actions github-actions Bot added the bug Something isn't working as expected label Sep 9, 2026
@sung17
sung17 merged commit f8b7e0b into main Sep 9, 2026
5 checks passed
hello-jackytruong referenced this pull request in hello-jackytruong/ChatWorkAI Nov 22, 2026
… (#1122)

sendPrivateReplyMessage posted to `{igId}/messages`, which Meta rejects with
`(#3) Application does not have the capability to make this API call.` even when
the app holds instagram_manage_messages, pages_messaging and Human Agent at
Advanced Access. For Instagram via Facebook Login the `messages` edge only
exists on the Page node, so every private reply on that channel failed: the
automation's `text` and `AIAgent` replies, the first message of a `flow` reply,
and the agent's manual private reply from the inbox.

This is the second time the fix has been needed. #875 moved the endpoint to
`pageId`, then #945 moved it back to green a stale test whose fixture carried no
`pageId` at all — so the endpoint silently became `/undefined/messages` and the
assertion on the IG node kept passing. The test is now the guard instead of the
cause: the fixture carries a `pageId` distinct from `igId`, and a new case
asserts the IG node is never called.

Also guards a missing `pageId` explicitly rather than building
`/undefined/messages`, which is the silent failure mode that hid the regression.

Two adjacent error-mapping bugs surfaced while tracing this:

- Code 3 was in neither Instagram mapper, so it fell through to the
  `type === "OAuthException"` fallback and was reported as AUTH_FAILED —
  pointing operators at a reconnect that could never help. It is a capability
  problem, so it now maps to PERMISSION_DENIED (permanent), as messenger
  already does.
- instagram-facebook's isRevokedTokenError matched *any* OAuthException,
  including code 3, making the disconnect flow skip its remote teardown for
  unrelated errors. Narrowed to code 190 plus a revoked subcode, matching the
  Instagram Login variant, which excludes a bare 190 as ambiguous to avoid
  false-positive disconnects.

Unrelated, found in the same log: the getProfile failure in received-message.ts
logged under `error`, so pino dropped the stack trace. Keyed to `err`.

Co-authored-by: Deathgiver <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant