From d8656edc6182200cfe55ada0d872e1a14f00ca84 Mon Sep 17 00:00:00 2001 From: jaroenchai_t <84191989+jaroenchai-t@users.noreply.github.com> Date: Mon, 8 Jun 2026 18:28:36 +0700 Subject: [PATCH] fix(facebook): Stop force tagging Messenger replies with MESSAGE_TAG/ACCOUNT_UPDATE (#14329) Outbound Facebook Messenger replies were always sent with `messaging_type: MESSAGE_TAG` and a `tag` of either `HUMAN_AGENT` or `ACCOUNT_UPDATE` (fallback). `ACCOUNT_UPDATE` is reserved by Meta policy for non-recurring account notifications (password resets, suspicious activity, account-status changes), so general agent replies were getting rejected by Facebook with "Invalid parameter" and risked page-level penalties. After this fix, Facebook treats default replies as standard `RESPONSE` messages within the 24-hour window, and only attaches `HUMAN_AGENT` tagging when the operator opts in via `ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT`. ## Closes ## How to reproduce 1. Connect a Facebook page inbox. 2. Leave `ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT` disabled (default). 3. From the dashboard, reply to an active Messenger conversation with a regular/promotional message. 4. Before this PR: the request to Graph API includes `messaging_type=MESSAGE_TAG&tag=ACCOUNT_UPDATE`. Facebook returns "Invalid parameter" for content that does not match the `ACCOUNT_UPDATE` use case, and the message is marked failed in Chatwoot. 5. After this PR: the request omits `messaging_type` and `tag`, so Facebook treats it as a standard `RESPONSE` and accepts it within the 24-hour customer service window. ## What changed - `app/services/facebook/send_on_facebook_service.rb` now mirrors the Instagram service pattern: text and attachment params are built without `messaging_type`/`tag` by default, and a shared `merge_human_agent_tag` helper attaches `MESSAGE_TAG`/`HUMAN_AGENT` only when `ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT` is enabled. - Removed the unused `sent_first_outgoing_message_after_24_hours?` helper (dead code with no callers). - Updated `spec/services/facebook/send_on_facebook_service_spec.rb`: dropped `ACCOUNT_UPDATE` expectations, added a spec asserting no `messaging_type`/`tag` is sent by default, and tightened the HUMAN_AGENT spec to verify both `messaging_type` and `tag`. ## Operator note Operators who were relying on the prior `ACCOUNT_UPDATE` fallback to bypass the 24-hour window were already in violation of Meta policy and likely seeing send failures. They should enable `ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT` (and request the Human Agent permission from Meta) for the 7-day extended window --------- Co-authored-by: Muhsin Keloth Co-authored-by: Muhsin <12408980+muhsin-k@users.noreply.github.com> --- .../facebook/send_on_facebook_service.rb | 32 ++++++++++--------- .../facebook/send_on_facebook_service_spec.rb | 21 ++++++++---- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/app/services/facebook/send_on_facebook_service.rb b/app/services/facebook/send_on_facebook_service.rb index baf72ef6e..0b2f45590 100644 --- a/app/services/facebook/send_on_facebook_service.rb +++ b/app/services/facebook/send_on_facebook_service.rb @@ -45,12 +45,12 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService end def fb_text_message_params - { + params = { recipient: { id: contact.get_source_id(inbox.id) }, - message: fb_text_message_payload, - messaging_type: 'MESSAGE_TAG', - tag: message_tag + message: fb_text_message_payload } + + merge_human_agent_tag(params) end def fb_text_message_payload @@ -79,7 +79,7 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService end def fb_attachment_message_params(attachment) - { + params = { recipient: { id: contact.get_source_id(inbox.id) }, message: { attachment: { @@ -88,14 +88,21 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService url: attachment.download_url } } - }, - messaging_type: 'MESSAGE_TAG', - tag: message_tag + } } + + merge_human_agent_tag(params) end - def message_tag - @message_tag ||= GlobalConfigService.load('ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT', nil) ? 'HUMAN_AGENT' : 'ACCOUNT_UPDATE' + def merge_human_agent_tag(params) + unless GlobalConfigService.load('ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT', nil) + params[:messaging_type] = 'RESPONSE' + return params + end + + params[:messaging_type] = 'MESSAGE_TAG' + params[:tag] = 'HUMAN_AGENT' + params end def attachment_type(attachment) @@ -104,11 +111,6 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService 'file' end - def sent_first_outgoing_message_after_24_hours? - # we can send max 1 message after 24 hour window - conversation.messages.outgoing.where('id > ?', conversation.last_incoming_message.id).count == 1 - end - def handle_facebook_error(exception) # Refer: https://github.com/jgorset/facebook-messenger/blob/64fe1f5cef4c1e3fca295b205037f64dfebdbcab/lib/facebook/messenger/error.rb return unless exception.to_s.include?('The session has been invalidated') || exception.to_s.include?('Error validating access token') diff --git a/spec/services/facebook/send_on_facebook_service_spec.rb b/spec/services/facebook/send_on_facebook_service_spec.rb index f99b1c469..0a2b6407c 100644 --- a/spec/services/facebook/send_on_facebook_service_spec.rb +++ b/spec/services/facebook/send_on_facebook_service_spec.rb @@ -73,8 +73,7 @@ describe Facebook::SendOnFacebookService do expect(bot).to have_received(:deliver).with({ recipient: { id: contact_inbox.source_id }, message: { text: message.content }, - messaging_type: 'MESSAGE_TAG', - tag: 'ACCOUNT_UPDATE' + messaging_type: 'RESPONSE' }, { page_id: facebook_channel.page_id }) expect(bot).to have_received(:deliver).with({ recipient: { id: contact_inbox.source_id }, @@ -86,17 +85,26 @@ describe Facebook::SendOnFacebookService do } } }, - messaging_type: 'MESSAGE_TAG', - tag: 'ACCOUNT_UPDATE' + messaging_type: 'RESPONSE' }, { page_id: facebook_channel.page_id }) end + it 'sends as a standard RESPONSE without a tag by default' do + message = create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, conversation: conversation) + described_class.new(message: message).perform + expect(bot).to have_received(:deliver).with( + hash_including(messaging_type: 'RESPONSE'), + { page_id: facebook_channel.page_id } + ) + expect(bot).not_to have_received(:deliver).with(hash_including(:tag), anything) + end + it 'sends with HUMAN_AGENT tag when ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT is enabled' do with_modified_env ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT: 'true' do message = create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, conversation: conversation) described_class.new(message: message).perform expect(bot).to have_received(:deliver).with( - hash_including(tag: 'HUMAN_AGENT'), + hash_including(messaging_type: 'MESSAGE_TAG', tag: 'HUMAN_AGENT'), { page_id: facebook_channel.page_id } ) end @@ -201,8 +209,7 @@ describe Facebook::SendOnFacebookService do { content_type: 'text', payload: 'text 2', title: 'text 2' } ] }, - messaging_type: 'MESSAGE_TAG', - tag: 'ACCOUNT_UPDATE' + messaging_type: 'RESPONSE' }, { page_id: facebook_channel.page_id }) end end