2019-12-28 17:44:09 +00:00
|
|
|
class Facebook::SendReplyService
|
|
|
|
pattr_initialize [:message!]
|
2019-08-30 22:38:00 +00:00
|
|
|
|
2019-12-28 17:44:09 +00:00
|
|
|
def perform
|
|
|
|
return if message.private
|
|
|
|
return if inbox.channel.class.to_s != 'Channel::FacebookPage'
|
|
|
|
return unless outgoing_message_from_chatwoot?
|
2019-08-30 22:38:00 +00:00
|
|
|
|
2020-05-18 10:02:26 +00:00
|
|
|
FacebookBot::Bot.deliver(delivery_params, access_token: message.channel_token)
|
2019-12-28 17:44:09 +00:00
|
|
|
end
|
2019-08-30 22:38:00 +00:00
|
|
|
|
2019-12-28 17:44:09 +00:00
|
|
|
private
|
2019-08-30 22:38:00 +00:00
|
|
|
|
2019-12-28 17:44:09 +00:00
|
|
|
delegate :contact, to: :conversation
|
2019-10-20 19:10:18 +00:00
|
|
|
|
2019-12-28 17:44:09 +00:00
|
|
|
def inbox
|
|
|
|
@inbox ||= message.inbox
|
|
|
|
end
|
2019-08-30 22:38:00 +00:00
|
|
|
|
2019-12-28 17:44:09 +00:00
|
|
|
def conversation
|
|
|
|
@conversation ||= message.conversation
|
|
|
|
end
|
2019-08-30 22:38:00 +00:00
|
|
|
|
2019-12-28 17:44:09 +00:00
|
|
|
def outgoing_message_from_chatwoot?
|
2020-02-09 10:17:48 +00:00
|
|
|
# messages sent directly from chatwoot won't have source_id.
|
2020-06-09 18:24:35 +00:00
|
|
|
(message.outgoing? || message.template?) && !message.source_id
|
2019-12-28 17:44:09 +00:00
|
|
|
end
|
2019-08-30 22:38:00 +00:00
|
|
|
|
2019-12-28 17:44:09 +00:00
|
|
|
# def reopen_lock
|
|
|
|
# if message.incoming? && conversation.locked?
|
|
|
|
# conversation.unlock!
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
2020-03-28 06:13:02 +00:00
|
|
|
def fb_text_message_params
|
2019-12-28 17:44:09 +00:00
|
|
|
{
|
|
|
|
recipient: { id: contact.get_source_id(inbox.id) },
|
|
|
|
message: { text: message.content }
|
|
|
|
}
|
|
|
|
end
|
2019-08-30 22:38:00 +00:00
|
|
|
|
2020-03-28 06:13:02 +00:00
|
|
|
def fb_attachment_message_params
|
|
|
|
{
|
|
|
|
recipient: { id: contact.get_source_id(inbox.id) },
|
|
|
|
message: {
|
|
|
|
attachment: {
|
|
|
|
type: 'image',
|
|
|
|
payload: {
|
2020-04-17 15:45:20 +00:00
|
|
|
url: message.attachments.first.file_url
|
2020-03-28 06:13:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def fb_message_params
|
2020-04-17 15:45:20 +00:00
|
|
|
if message.attachments.blank?
|
2020-03-28 06:13:02 +00:00
|
|
|
fb_text_message_params
|
|
|
|
else
|
|
|
|
fb_attachment_message_params
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-28 17:44:09 +00:00
|
|
|
def delivery_params
|
|
|
|
if twenty_four_hour_window_over?
|
|
|
|
fb_message_params.merge(tag: 'ISSUE_RESOLUTION')
|
|
|
|
else
|
|
|
|
fb_message_params
|
2019-08-30 22:38:00 +00:00
|
|
|
end
|
2019-12-28 17:44:09 +00:00
|
|
|
end
|
2019-08-30 22:38:00 +00:00
|
|
|
|
2019-12-28 17:44:09 +00:00
|
|
|
def twenty_four_hour_window_over?
|
2020-03-17 15:39:20 +00:00
|
|
|
return false unless after_24_hours?
|
|
|
|
return false if last_incoming_and_outgoing_message_after_one_day?
|
2019-08-30 22:38:00 +00:00
|
|
|
|
2020-03-17 15:39:20 +00:00
|
|
|
true
|
|
|
|
end
|
2019-08-30 22:38:00 +00:00
|
|
|
|
2020-03-17 15:39:20 +00:00
|
|
|
def last_incoming_and_outgoing_message_after_one_day?
|
|
|
|
last_incoming_message && sent_first_outgoing_message_after_24_hours?
|
|
|
|
end
|
2019-08-30 22:38:00 +00:00
|
|
|
|
2020-03-17 15:39:20 +00:00
|
|
|
def after_24_hours?
|
|
|
|
(Time.current - last_incoming_message.created_at) / 3600 >= 24
|
2019-12-28 17:44:09 +00:00
|
|
|
end
|
2019-08-30 22:38:00 +00:00
|
|
|
|
2020-03-17 15:39:20 +00:00
|
|
|
def sent_first_outgoing_message_after_24_hours?
|
2019-12-28 17:44:09 +00:00
|
|
|
# we can send max 1 message after 24 hour window
|
2020-03-17 15:39:20 +00:00
|
|
|
conversation.messages.outgoing.where('id > ?', last_incoming_message.id).count == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_incoming_message
|
|
|
|
conversation.messages.incoming.last
|
2019-08-30 22:38:00 +00:00
|
|
|
end
|
|
|
|
end
|