2021-08-03 14:41:52 +00:00
|
|
|
# ref: https://github.com/jgorset/facebook-messenger#make-a-configuration-provider
|
|
|
|
class ChatwootFbProvider < Facebook::Messenger::Configuration::Providers::Base
|
|
|
|
def valid_verify_token?(_verify_token)
|
2021-11-24 19:25:26 +00:00
|
|
|
GlobalConfigService.load('FB_VERIFY_TOKEN', '')
|
2021-08-03 14:41:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def app_secret_for(_page_id)
|
2021-11-24 19:25:26 +00:00
|
|
|
GlobalConfigService.load('FB_APP_SECRET', '')
|
2021-08-03 14:41:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def access_token_for(page_id)
|
|
|
|
Channel::FacebookPage.where(page_id: page_id).last.page_access_token
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def bot
|
|
|
|
Chatwoot::Bot
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Rails.application.reloader.to_prepare do
|
|
|
|
Facebook::Messenger.configure do |config|
|
|
|
|
config.provider = ChatwootFbProvider.new
|
|
|
|
end
|
2020-05-18 10:02:26 +00:00
|
|
|
|
2021-06-07 08:28:01 +00:00
|
|
|
Facebook::Messenger::Bot.on :message do |message|
|
2021-09-13 12:35:14 +00:00
|
|
|
Webhooks::FacebookEventsJob.perform_later(message.to_json)
|
2020-05-18 10:02:26 +00:00
|
|
|
end
|
|
|
|
|
2021-06-07 08:28:01 +00:00
|
|
|
Facebook::Messenger::Bot.on :delivery do |delivery|
|
2020-05-18 10:02:26 +00:00
|
|
|
# delivery.ids # => 'mid.1457764197618:41d102a3e1ae206a38'
|
|
|
|
# delivery.sender # => { 'id' => '1008372609250235' }
|
|
|
|
# delivery.recipient # => { 'id' => '2015573629214912' }
|
|
|
|
# delivery.at # => 2016-04-22 21:30:36 +0200
|
|
|
|
# delivery.seq # => 37
|
|
|
|
updater = Integrations::Facebook::DeliveryStatus.new(delivery)
|
|
|
|
updater.perform
|
|
|
|
Rails.logger.info "Human was online at #{delivery.at}"
|
|
|
|
end
|
2020-09-04 13:43:47 +00:00
|
|
|
|
2021-06-07 08:28:01 +00:00
|
|
|
Facebook::Messenger::Bot.on :message_echo do |message|
|
2021-09-13 12:35:14 +00:00
|
|
|
Webhooks::FacebookEventsJob.perform_later(message.to_json)
|
2020-09-04 13:43:47 +00:00
|
|
|
end
|
2020-05-18 10:02:26 +00:00
|
|
|
end
|