Skip to content

fix(comment-automation): deliver flow-type private replies via comment-anchored send - #831

Merged
sung17 merged 1 commit into
mainfrom
fix/comment-automation-private-reply-flow
Jul 24, 2026
Merged

sung17 merged 1 commit into
mainfrom
fix/comment-automation-private-reply-flow

Conversation

@sung17

@sung17 sung17 commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Facebook comment automation's private reply configured as "flow" silently failed to deliver: it used the normal PSID-based Send API, which Facebook rejects for contacts who only commented and never messaged the Page. The "text" reply type already worked because it uses the comment_id-anchored Send API (sendPrivateReply). The rejection was also swallowed after the outgoing message row was optimistically marked as sent, so the job reported success with nothing delivered.
  • Threads an optional commentAnchor { commentId } through the flow engine (flow.ts, flow-utils.ts, step.ts, condition.ts) and the chat queue plumbing, forwarded across every re-enqueued sendFlow job until claimed by the first message-producing step. The Messenger channel handler then sends that first message via the new sendPrivateReplyMessage API (comment_id-anchored, any payload type) instead of the normal Send API; later messages in the same flow fall back to the normal path once the private reply has opened a standard messaging window.
  • sendPrivateReplyMessage also stamps message.metadata and persona_id so the message_echo webhook recognizes our own sends and persona configuration is respected, matching every other Messenger send path; sendPrivateReply now delegates to it to remove duplicated request construction.

Changes

  • apps/worker/src/integration/handlers/comment-automation/index.ts — creates commentAnchor for messenger flow-type private replies
  • apps/worker/src/integration/handlers/{flow.ts,flow-utils.ts,step.ts,condition.ts} — propagate + consume-once commentAnchor through the flow engine
  • apps/worker/src/chat/handlers/{send-flow-step.ts,send-message.ts} — forward commentAnchor through the chat-queue send path (Messenger-only, defense-in-depth channel check)
  • integrations/messenger/src/apis/comment.ts — new sendPrivateReplyMessage (arbitrary payload, comment_id-anchored, stamps metadata/persona_id); sendPrivateReply now delegates to it
  • integrations/messenger/src/handlers/message/outgoing-message/index.ts — uses the comment-anchored send for the first Facebook message of a run when commentAnchor is present
  • packages/sdk/src/lib/shared/index.ts — new shared CommentAnchor type (removes duplicated inline type across sdk/worker-config/worker)
  • Tests: extended comment-automation.test.ts, flow.test.ts, send-flow-step.test.ts; new send-flow-step-comment-anchor.test.ts and send-private-reply.test.ts in integrations/messenger

Known gap (documented inline, not fixed here): a private-reply flow with a wait/followUp/questionnaire step before its first message step loses the anchor across the scheduled delay, since ContactOnSmartDelay has no column to carry it — would need a schema change.

Test plan

  • pnpm --filter worker check-types, pnpm --filter @chatbotx.io/integration-messenger check-types, pnpm --filter @chatbotx.io/sdk check-types, pnpm --filter @chatbotx.io/worker-config check-types — all pass
  • pnpm --filter worker vitest run — 1098/1098 pass
  • pnpm --filter @chatbotx.io/integration-messenger vitest run — 177/177 pass
  • pnpm lint — clean
  • Manual: configure a comment automation with private reply = flow on a real connected Page, comment with an account that has never messaged the Page, confirm the private message actually arrives (not just shown as "sent" in the UI)

…t-anchored send

Private reply configured as "text" already worked because it calls
sendPrivateReply, which uses Facebook's comment_id-anchored Send API
(bypasses the messaging-window rule). Private reply configured as
"flow" only enqueued a generic sendFlow job, which delivers via the
normal PSID-based Send API — rejected by Facebook for contacts who
only commented and never messaged the Page. The rejection was also
silently swallowed after the outgoing message row was optimistically
marked as sent, so the job reported success with nothing delivered.

Threads an optional commentAnchor { commentId } through the flow
engine (flow.ts, flow-utils.ts, step.ts, condition.ts) and the chat
queue plumbing, forwarded across every re-enqueued sendFlow job until
claimed by the first message-producing step. The Messenger channel
handler then sends that first message through the new
sendPrivateReplyMessage API (comment_id-anchored, any payload type,
not just text) instead of the normal Send API; later messages in the
same flow fall back to the normal path once the private reply has
opened a standard messaging window.

sendPrivateReplyMessage also stamps message.metadata and persona_id
so the message_echo webhook still recognizes our own sends and
persona configuration is respected on the anchored path, matching
every other Messenger send path. sendPrivateReply now delegates to it
to remove the duplicated request construction.

Known gap (documented inline, not fixed here): a private-reply flow
with a wait/followUp/questionnaire step before its first message step
loses the anchor across the scheduled delay, since
ContactOnSmartDelay has no column to carry it — needs a schema change.
@github-actions github-actions Bot added the bug Something isn't working as expected label Jul 24, 2026
@sung17
sung17 merged commit e54607d into main Jul 24, 2026
8 checks passed
sung17 added a commit that referenced this pull request Jul 25, 2026
…ot DMs (#834)

publicReply.type === "flow" only enqueued a generic sendFlow job, which
always routed its outgoing messages through the private "message"
channel (a normal DM Send API call). It never posted a public comment
reply, unlike "text" and "AIAgent", both of which correctly call
postPublicCommentReply. The flow's messages either landed silently as
a DM the user never asked for, or failed outright for commenters who
had never messaged the Page (same messaging-window rejection class as
the earlier private-reply-flow bug, PR #831).

Extends the commentAnchor mechanism from that earlier fix with a
replyChannel: "public" | "private" discriminant instead of adding a
parallel mechanism, since the propagation plumbing (flow.ts,
flow-utils.ts, step.ts, condition.ts) already treats the anchor
opaquely and needs no changes.

- executePublicReply's flow branch now sets commentAnchor with
  replyChannel: "public" — unlike private reply, this is NOT gated to
  Messenger, since both Messenger and Instagram implement
  comment.sendComment.
- send-flow-step.ts branches on replyChannel: "public" builds the
  message row as type:"comment" with contentAttributes.replyToCommentId
  and routes through sendMessageToChannel (reusing its existing
  isComment routing, sourceId persistence, and retry-suppression logic
  instead of inventing a new path); "private" (or absent) keeps the
  existing sendFlowStepToChannel/comment-anchored-DM behavior.
- sendMessageToChannel now returns { messageIds } instead of void so
  the flow-step handler's message:sent analytics/sourceId tracking
  works identically for both branches.
- Messenger's outgoing-message handler only honors the anchor for
  replyChannel: "private" (defense-in-depth — a public anchor should
  never reach the DM channel handler).

Known gap (documented inline, unchanged from PR #831): a reply flow
with a wait/followUp/questionnaire step before its first message step
still loses the anchor across the scheduled delay, for both public and
private now — ContactOnSmartDelay has no column to carry it.

Co-authored-by: Deathgiver <[email protected]>
realcodesiman pushed a commit that referenced this pull request Aug 23, 2026
Both `docs/fb-comment-automation.md` and the `fb-comment-automation` skill still
claimed Instagram comment automation was unimplemented ("the builder always stores
`type = "messenger"`", "IG private-DM text is out of scope"), which has been wrong
since #874 shipped the `ig-comments` builder feature and the `instagram` /
`instagramFacebook` channel types. #831 and #1018 then moved flow-based private
replies onto Meta's comment_id-anchored Send API. None of those PRs touched the
docs, so the guidance actively told readers (and AI agents) not to work on IG.

Rewrite the scope banner, the `type` option row, the reply-type table, the known-gap
bullet and skill trap 6 to describe the three `CommentAutomationChannelType` values,
and record the real per-channel limits: comment liking is messenger +
instagramFacebook only, and the hide image/video attachment lookup is messenger only.


Claude-Session: https://claude.ai/code/session_01Jr3BRqH3woCzbcePEBPira

Co-authored-by: Claude Opus 5 <[email protected]>
viethung0o0 pushed a commit that referenced this pull request Sep 7, 2026
…t-anchored send (#831)

Private reply configured as "text" already worked because it calls
sendPrivateReply, which uses Facebook's comment_id-anchored Send API
(bypasses the messaging-window rule). Private reply configured as
"flow" only enqueued a generic sendFlow job, which delivers via the
normal PSID-based Send API — rejected by Facebook for contacts who
only commented and never messaged the Page. The rejection was also
silently swallowed after the outgoing message row was optimistically
marked as sent, so the job reported success with nothing delivered.

Threads an optional commentAnchor { commentId } through the flow
engine (flow.ts, flow-utils.ts, step.ts, condition.ts) and the chat
queue plumbing, forwarded across every re-enqueued sendFlow job until
claimed by the first message-producing step. The Messenger channel
handler then sends that first message through the new
sendPrivateReplyMessage API (comment_id-anchored, any payload type,
not just text) instead of the normal Send API; later messages in the
same flow fall back to the normal path once the private reply has
opened a standard messaging window.

sendPrivateReplyMessage also stamps message.metadata and persona_id
so the message_echo webhook still recognizes our own sends and
persona configuration is respected on the anchored path, matching
every other Messenger send path. sendPrivateReply now delegates to it
to remove the duplicated request construction.

Known gap (documented inline, not fixed here): a private-reply flow
with a wait/followUp/questionnaire step before its first message step
loses the anchor across the scheduled delay, since
ContactOnSmartDelay has no column to carry it — needs a schema change.

Co-authored-by: Deathgiver <[email protected]>
viethung0o0 pushed a commit that referenced this pull request Sep 7, 2026
…ot DMs (#834)

publicReply.type === "flow" only enqueued a generic sendFlow job, which
always routed its outgoing messages through the private "message"
channel (a normal DM Send API call). It never posted a public comment
reply, unlike "text" and "AIAgent", both of which correctly call
postPublicCommentReply. The flow's messages either landed silently as
a DM the user never asked for, or failed outright for commenters who
had never messaged the Page (same messaging-window rejection class as
the earlier private-reply-flow bug, PR #831).

Extends the commentAnchor mechanism from that earlier fix with a
replyChannel: "public" | "private" discriminant instead of adding a
parallel mechanism, since the propagation plumbing (flow.ts,
flow-utils.ts, step.ts, condition.ts) already treats the anchor
opaquely and needs no changes.

- executePublicReply's flow branch now sets commentAnchor with
  replyChannel: "public" — unlike private reply, this is NOT gated to
  Messenger, since both Messenger and Instagram implement
  comment.sendComment.
- send-flow-step.ts branches on replyChannel: "public" builds the
  message row as type:"comment" with contentAttributes.replyToCommentId
  and routes through sendMessageToChannel (reusing its existing
  isComment routing, sourceId persistence, and retry-suppression logic
  instead of inventing a new path); "private" (or absent) keeps the
  existing sendFlowStepToChannel/comment-anchored-DM behavior.
- sendMessageToChannel now returns { messageIds } instead of void so
  the flow-step handler's message:sent analytics/sourceId tracking
  works identically for both branches.
- Messenger's outgoing-message handler only honors the anchor for
  replyChannel: "private" (defense-in-depth — a public anchor should
  never reach the DM channel handler).

Known gap (documented inline, unchanged from PR #831): a reply flow
with a wait/followUp/questionnaire step before its first message step
still loses the anchor across the scheduled delay, for both public and
private now — ContactOnSmartDelay has no column to carry it.

Co-authored-by: Deathgiver <[email protected]>
viethung0o0 pushed a commit that referenced this pull request Sep 7, 2026
Both `docs/fb-comment-automation.md` and the `fb-comment-automation` skill still
claimed Instagram comment automation was unimplemented ("the builder always stores
`type = "messenger"`", "IG private-DM text is out of scope"), which has been wrong
since #874 shipped the `ig-comments` builder feature and the `instagram` /
`instagramFacebook` channel types. #831 and #1018 then moved flow-based private
replies onto Meta's comment_id-anchored Send API. None of those PRs touched the
docs, so the guidance actively told readers (and AI agents) not to work on IG.

Rewrite the scope banner, the `type` option row, the reply-type table, the known-gap
bullet and skill trap 6 to describe the three `CommentAutomationChannelType` values,
and record the real per-channel limits: comment liking is messenger +
instagramFacebook only, and the hide image/video attachment lookup is messenger only.


Claude-Session: https://claude.ai/code/session_01Jr3BRqH3woCzbcePEBPira

Co-authored-by: Claude Opus 5 <[email protected]>

This branch was previously deployed

1 inactive deployment
dev — 218ffbcd Deployed Jul 24, 2026 by sung17 via Build realtime #2102
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