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 <!-- Add the relevant issue link, e.g. Closes #1234 --> ## 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 <muhsinkeramam@gmail.com> Co-authored-by: Muhsin <12408980+muhsin-k@users.noreply.github.com>
121 lines
3.8 KiB
Ruby
121 lines
3.8 KiB
Ruby
class Facebook::SendOnFacebookService < Base::SendOnChannelService
|
|
private
|
|
|
|
def channel_class
|
|
Channel::FacebookPage
|
|
end
|
|
|
|
def perform_reply
|
|
send_message_to_facebook fb_text_message_params if message.content.present?
|
|
|
|
if message.attachments.present?
|
|
message.attachments.each do |attachment|
|
|
send_message_to_facebook fb_attachment_message_params(attachment)
|
|
end
|
|
end
|
|
rescue Facebook::Messenger::FacebookError => e
|
|
# TODO : handle specific errors or else page will get disconnected
|
|
handle_facebook_error(e)
|
|
Messages::StatusUpdateService.new(message, 'failed', e.message).perform
|
|
end
|
|
|
|
def send_message_to_facebook(delivery_params)
|
|
parsed_result = deliver_message(delivery_params)
|
|
return if parsed_result.nil?
|
|
|
|
if parsed_result['error'].present?
|
|
Messages::StatusUpdateService.new(message, 'failed', external_error(parsed_result)).perform
|
|
Rails.logger.info "Facebook::SendOnFacebookService: Error sending message to Facebook : Page - #{channel.page_id} : #{parsed_result}"
|
|
end
|
|
|
|
message.update!(source_id: parsed_result['message_id']) if parsed_result['message_id'].present?
|
|
end
|
|
|
|
def deliver_message(delivery_params)
|
|
result = Facebook::Messenger::Bot.deliver(delivery_params, page_id: channel.page_id)
|
|
JSON.parse(result)
|
|
rescue JSON::ParserError
|
|
Messages::StatusUpdateService.new(message, 'failed', 'Facebook was unable to process this request').perform
|
|
Rails.logger.error "Facebook::SendOnFacebookService: Error parsing JSON response from Facebook : Page - #{channel.page_id} : #{result}"
|
|
nil
|
|
rescue Net::OpenTimeout
|
|
Messages::StatusUpdateService.new(message, 'failed', 'Request timed out, please try again later').perform
|
|
Rails.logger.error "Facebook::SendOnFacebookService: Timeout error sending message to Facebook : Page - #{channel.page_id}"
|
|
nil
|
|
end
|
|
|
|
def fb_text_message_params
|
|
params = {
|
|
recipient: { id: contact.get_source_id(inbox.id) },
|
|
message: fb_text_message_payload
|
|
}
|
|
|
|
merge_human_agent_tag(params)
|
|
end
|
|
|
|
def fb_text_message_payload
|
|
if message.content_type == 'input_select' && message.content_attributes['items'].any?
|
|
{
|
|
text: message.content,
|
|
quick_replies: message.content_attributes['items'].map do |item|
|
|
{
|
|
content_type: 'text',
|
|
payload: item['title'],
|
|
title: item['title']
|
|
}
|
|
end
|
|
}
|
|
else
|
|
{ text: message.outgoing_content }
|
|
end
|
|
end
|
|
|
|
def external_error(response)
|
|
# https://developers.facebook.com/docs/graph-api/guides/error-handling/
|
|
error_message = response['error']['message']
|
|
error_code = response['error']['code']
|
|
|
|
"#{error_code} - #{error_message}"
|
|
end
|
|
|
|
def fb_attachment_message_params(attachment)
|
|
params = {
|
|
recipient: { id: contact.get_source_id(inbox.id) },
|
|
message: {
|
|
attachment: {
|
|
type: attachment_type(attachment),
|
|
payload: {
|
|
url: attachment.download_url
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
merge_human_agent_tag(params)
|
|
end
|
|
|
|
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)
|
|
return attachment.file_type if %w[image audio video file].include? attachment.file_type
|
|
|
|
'file'
|
|
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')
|
|
|
|
channel.authorization_error!
|
|
end
|
|
end
|