feat: Add preview for attachment messages (#1562)

Add preview for pending messages and attachments

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
Nithin David Thomas
2021-01-06 17:56:29 +05:30
committed by GitHub
parent db189e3c26
commit 3d2db95417
17 changed files with 434 additions and 250 deletions

View File

@@ -6,13 +6,18 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService
end
def perform_reply
result = FacebookBot::Bot.deliver(delivery_params, access_token: message.channel_token)
message.update!(source_id: JSON.parse(result)['message_id'])
send_message_to_facebook fb_text_message_params if message.content.present?
send_message_to_facebook fb_attachment_message_params if message.attachments.present?
rescue Facebook::Messenger::FacebookError => e
Rails.logger.info e
channel.authorization_error!
end
def send_message_to_facebook(delivery_params)
result = FacebookBot::Bot.deliver(delivery_params, access_token: message.channel_token)
message.update!(source_id: JSON.parse(result)['message_id'])
end
def fb_text_message_params
{
recipient: { id: contact.get_source_id(inbox.id) },
@@ -49,29 +54,6 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService
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