818c769bb7
* Changes for the message to have multiple attachments * changed the message association to attachments from has_one to has_many * changed all the references of this association in building and fetching to reflect this change * Added number of attachments validation to the message model * Modified the backend responses and endpoints to reflect multiple attachment support (#737) * Changing the frontend components for multiple attachments * changed the request structure to reflect the multiple attachment structures * changed the message bubbles to support multiple attachments * bugfix: agent side attachment was not showing because of a missing await * broken message was shown because of the store filtering * Added documentation for ImageMagick * spec fixes * refactored code to reflect more apt namings * Added updated message listener for the dashboard (#727) * Added the publishing for message updated event * Implemented the listener for dashboard Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
95 lines
2.1 KiB
Ruby
95 lines
2.1 KiB
Ruby
class Facebook::SendReplyService
|
|
pattr_initialize [:message!]
|
|
|
|
def perform
|
|
return if message.private
|
|
return if inbox.channel.class.to_s != 'Channel::FacebookPage'
|
|
return unless outgoing_message_from_chatwoot?
|
|
|
|
Bot.deliver(delivery_params, access_token: message.channel_token)
|
|
end
|
|
|
|
private
|
|
|
|
delegate :contact, to: :conversation
|
|
|
|
def inbox
|
|
@inbox ||= message.inbox
|
|
end
|
|
|
|
def conversation
|
|
@conversation ||= message.conversation
|
|
end
|
|
|
|
def outgoing_message_from_chatwoot?
|
|
# messages sent directly from chatwoot won't have source_id.
|
|
message.outgoing? && !message.source_id
|
|
end
|
|
|
|
# def reopen_lock
|
|
# if message.incoming? && conversation.locked?
|
|
# conversation.unlock!
|
|
# end
|
|
# end
|
|
|
|
def fb_text_message_params
|
|
{
|
|
recipient: { id: contact.get_source_id(inbox.id) },
|
|
message: { text: message.content }
|
|
}
|
|
end
|
|
|
|
def fb_attachment_message_params
|
|
{
|
|
recipient: { id: contact.get_source_id(inbox.id) },
|
|
message: {
|
|
attachment: {
|
|
type: 'image',
|
|
payload: {
|
|
url: message.attachments.first.file_url
|
|
}
|
|
}
|
|
}
|
|
}
|
|
end
|
|
|
|
def fb_message_params
|
|
if message.attachments.blank?
|
|
fb_text_message_params
|
|
else
|
|
fb_attachment_message_params
|
|
end
|
|
end
|
|
|
|
def delivery_params
|
|
if twenty_four_hour_window_over?
|
|
fb_message_params.merge(tag: 'ISSUE_RESOLUTION')
|
|
else
|
|
fb_message_params
|
|
end
|
|
end
|
|
|
|
def twenty_four_hour_window_over?
|
|
return false unless after_24_hours?
|
|
return false if last_incoming_and_outgoing_message_after_one_day?
|
|
|
|
true
|
|
end
|
|
|
|
def last_incoming_and_outgoing_message_after_one_day?
|
|
last_incoming_message && sent_first_outgoing_message_after_24_hours?
|
|
end
|
|
|
|
def after_24_hours?
|
|
(Time.current - last_incoming_message.created_at) / 3600 >= 24
|
|
end
|
|
|
|
def sent_first_outgoing_message_after_24_hours?
|
|
# we can send max 1 message after 24 hour window
|
|
conversation.messages.outgoing.where('id > ?', last_incoming_message.id).count == 1
|
|
end
|
|
|
|
def last_incoming_message
|
|
conversation.messages.incoming.last
|
|
end
|
|
end
|