Facebook Messenger messages that share a post no longer break message ingestion. Following the recent sticker change, Meta now sends a shared post as a new `post` attachment type. Chatwoot didn't recognise `post` as a valid attachment file type, so the webhook job crashed and the message — and any others in the same batch — failed to sync. Shared posts now appear in the conversation as a fallback link (title + URL), the same way `share` attachments already do. This is the same class of bug as #14793 (sticker), just a different attachment type. #### Root cause `post` was neither skipped as unsupported nor mapped to a valid `file_type`, so `attachment_params` produced `file_type: :post`. The `Attachment#file_type` enum has no `post` value, so it raised `ArgumentError: 'post' is not a valid file_type`, crashing `Webhooks::FacebookEventsJob`. #### Fix The Facebook builder already maps `share -> :fallback` because shared posts point to facebook.com page URLs rather than downloadable media. A `post` is the same kind of attachment, so map both `share` and `post` to `fallback` (stores the title/link, no download attempt). Also read the fallback title from `payload.title`, since the post payload nests it there rather than at the top level. ## How to reproduce 1. Connect a Facebook Page inbox. 2. Share a Facebook post into Messenger to that page. 3. Before this change: `Webhooks::FacebookEventsJob` raises `ArgumentError: 'post' is not a valid file_type` and the message is dropped. 4. After this change: the shared post shows up in the conversation as a fallback link. Co-authored-by: Muhsin <12408980+muhsin-k@users.noreply.github.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
172 lines
5.7 KiB
Ruby
172 lines
5.7 KiB
Ruby
# This class creates both outgoing messages from chatwoot and echo outgoing messages based on the flag `outgoing_echo`
|
|
# Assumptions
|
|
# 1. Incase of an outgoing message which is echo, source_id will NOT be nil,
|
|
# based on this we are showing "not sent from chatwoot" message in frontend
|
|
# Hence there is no need to set user_id in message for outgoing echo messages.
|
|
|
|
class Messages::Facebook::MessageBuilder < Messages::Messenger::MessageBuilder
|
|
attr_reader :response
|
|
|
|
def initialize(response, inbox, outgoing_echo: false)
|
|
super()
|
|
@response = response
|
|
@inbox = inbox
|
|
@outgoing_echo = outgoing_echo
|
|
@sender_id = (@outgoing_echo ? @response.recipient_id : @response.sender_id)
|
|
@message_type = (@outgoing_echo ? :outgoing : :incoming)
|
|
@attachments = (@response.attachments || [])
|
|
end
|
|
|
|
def perform
|
|
# This channel might require reauthorization, may be owner might have changed the fb password
|
|
return if @inbox.channel.reauthorization_required?
|
|
|
|
ActiveRecord::Base.transaction do
|
|
build_contact_inbox
|
|
build_message
|
|
end
|
|
rescue Koala::Facebook::AuthenticationError => e
|
|
Rails.logger.warn("Facebook authentication error for inbox: #{@inbox.id} with error: #{e.message}")
|
|
Rails.logger.error e
|
|
@inbox.channel.authorization_error!
|
|
rescue StandardError => e
|
|
ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception
|
|
true
|
|
end
|
|
|
|
private
|
|
|
|
def build_contact_inbox
|
|
@contact_inbox = ::ContactInboxWithContactBuilder.new(
|
|
source_id: @sender_id,
|
|
inbox: @inbox,
|
|
contact_attributes: contact_params
|
|
).perform
|
|
end
|
|
|
|
def build_message
|
|
@message = conversation.messages.create!(message_params)
|
|
|
|
@attachments.each do |attachment|
|
|
process_attachment(attachment)
|
|
end
|
|
end
|
|
|
|
def conversation
|
|
@conversation ||= set_conversation_based_on_inbox_config
|
|
end
|
|
|
|
def set_conversation_based_on_inbox_config
|
|
if @inbox.lock_to_single_conversation
|
|
Conversation.where(conversation_params).order(created_at: :desc).first || build_conversation
|
|
else
|
|
find_or_build_for_multiple_conversations
|
|
end
|
|
end
|
|
|
|
def find_or_build_for_multiple_conversations
|
|
# If lock to single conversation is disabled, we will create a new conversation if previous conversation is resolved
|
|
last_conversation = Conversation.where(conversation_params).where.not(status: :resolved).order(created_at: :desc).first
|
|
return build_conversation if last_conversation.nil?
|
|
|
|
last_conversation
|
|
end
|
|
|
|
def build_conversation
|
|
Conversation.create!(conversation_params.merge(
|
|
contact_inbox_id: @contact_inbox.id
|
|
))
|
|
end
|
|
|
|
def location_params(attachment)
|
|
lat = attachment['payload']['coordinates']['lat']
|
|
long = attachment['payload']['coordinates']['long']
|
|
{
|
|
external_url: attachment['url'],
|
|
coordinates_lat: lat,
|
|
coordinates_long: long,
|
|
fallback_title: attachment['title']
|
|
}
|
|
end
|
|
|
|
def fallback_params(attachment)
|
|
{
|
|
fallback_title: attachment['title'] || attachment.dig('payload', 'title'),
|
|
external_url: attachment['url'] || attachment.dig('payload', 'url')
|
|
}
|
|
end
|
|
|
|
# Facebook shared posts point to page URLs, not downloadable media URLs.
|
|
# Both `share` and `post` attachment types carry a page URL rather than a media file,
|
|
# so map them to `fallback` (which keeps the title/link without attempting a download).
|
|
# Keep this Facebook-only so Messenger/Instagram share attachments still use the parent media handling.
|
|
def normalize_file_type(type)
|
|
return :fallback if [:share, :post].include?(type.to_sym)
|
|
|
|
super
|
|
end
|
|
|
|
def conversation_params
|
|
{
|
|
account_id: @inbox.account_id,
|
|
inbox_id: @inbox.id,
|
|
contact_id: @contact_inbox.contact_id
|
|
}
|
|
end
|
|
|
|
def message_params
|
|
content_attributes = {
|
|
in_reply_to_external_id: response.in_reply_to_external_id
|
|
}
|
|
content_attributes[:external_echo] = true if @outgoing_echo
|
|
|
|
{
|
|
account_id: conversation.account_id,
|
|
inbox_id: conversation.inbox_id,
|
|
message_type: @message_type,
|
|
status: @outgoing_echo ? :delivered : :sent,
|
|
content: response.content,
|
|
source_id: response.identifier,
|
|
content_attributes: content_attributes,
|
|
sender: @outgoing_echo ? nil : @contact_inbox.contact
|
|
}
|
|
end
|
|
|
|
def process_contact_params_result(result)
|
|
{
|
|
name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}",
|
|
account_id: @inbox.account_id,
|
|
avatar_url: result['profile_pic']
|
|
}
|
|
end
|
|
|
|
# rubocop:disable Metrics/AbcSize
|
|
# rubocop:disable Metrics/MethodLength
|
|
def contact_params
|
|
begin
|
|
k = Koala::Facebook::API.new(@inbox.channel.page_access_token) if @inbox.facebook?
|
|
result = k.get_object(@sender_id) || {}
|
|
rescue Koala::Facebook::AuthenticationError => e
|
|
Rails.logger.warn("Facebook authentication error for inbox: #{@inbox.id} with error: #{e.message}")
|
|
Rails.logger.error e
|
|
@inbox.channel.authorization_error!
|
|
raise
|
|
rescue Koala::Facebook::ClientError => e
|
|
result = {}
|
|
# OAuthException, code: 100, error_subcode: 2018218, message: (#100) No profile available for this user
|
|
# We don't need to capture this error as we don't care about contact params in case of echo messages
|
|
if e.message.include?('2018218')
|
|
Rails.logger.warn e
|
|
else
|
|
ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception unless @outgoing_echo
|
|
end
|
|
rescue StandardError => e
|
|
result = {}
|
|
ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception
|
|
end
|
|
process_contact_params_result(result)
|
|
end
|
|
# rubocop:enable Metrics/AbcSize
|
|
# rubocop:enable Metrics/MethodLength
|
|
end
|