2020-06-07 08:28:05 +00:00
|
|
|
class Api::V1::Accounts::FacebookIndicatorsController < Api::V1::Accounts::BaseController
|
2019-08-14 09:48:44 +00:00
|
|
|
before_action :set_access_token
|
2019-08-21 16:22:20 +00:00
|
|
|
around_action :handle_with_exception
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
def mark_seen
|
2019-10-27 13:31:59 +00:00
|
|
|
fb_bot.deliver(payload('mark_seen'), access_token: @access_token)
|
2019-08-14 09:48:44 +00:00
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def typing_on
|
2019-10-27 13:31:59 +00:00
|
|
|
fb_bot.deliver(payload('typing_on'), access_token: @access_token)
|
2019-08-14 09:48:44 +00:00
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def typing_off
|
2019-10-27 13:31:59 +00:00
|
|
|
fb_bot.deliver(payload('typing_off'), access_token: @access_token)
|
2019-08-14 09:48:44 +00:00
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
def fb_bot
|
|
|
|
::Facebook::Messenger::Bot
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def handle_with_exception
|
2019-10-20 08:47:26 +00:00
|
|
|
yield
|
|
|
|
rescue Facebook::Messenger::Error => e
|
2020-03-09 17:57:10 +00:00
|
|
|
Rails.logger.debug "Rescued: #{e.inspect}"
|
2019-10-20 08:47:26 +00:00
|
|
|
true
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def payload(action)
|
|
|
|
{
|
2019-10-27 13:31:59 +00:00
|
|
|
recipient: { id: contact.source_id },
|
2019-08-14 09:48:44 +00:00
|
|
|
sender_action: action
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
def inbox
|
2020-06-07 08:28:05 +00:00
|
|
|
@inbox ||= Current.account.inboxes.find(permitted_params[:inbox_id])
|
2019-10-27 13:31:59 +00:00
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def set_access_token
|
|
|
|
@access_token = inbox.channel.page_access_token
|
|
|
|
end
|
2019-10-27 13:31:59 +00:00
|
|
|
|
|
|
|
def contact
|
|
|
|
@contact ||= inbox.contact_inboxes.find_by!(contact_id: permitted_params[:contact_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def permitted_params
|
|
|
|
params.permit(:inbox_id, :contact_id)
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|