chore: Update Facebook Messenger gem (#2342)
This commit is contained in:
parent
ae3cbf4f79
commit
d1b3c7b0c2
10 changed files with 29 additions and 240 deletions
2
Gemfile
2
Gemfile
|
@ -72,7 +72,7 @@ gem 'wisper', '2.0.0'
|
||||||
|
|
||||||
##--- gems for channels ---##
|
##--- gems for channels ---##
|
||||||
# TODO: bump up gem to 2.0
|
# TODO: bump up gem to 2.0
|
||||||
gem 'facebook-messenger', '1.5.0'
|
gem 'facebook-messenger'
|
||||||
gem 'telegram-bot-ruby'
|
gem 'telegram-bot-ruby'
|
||||||
gem 'twilio-ruby', '~> 5.32.0'
|
gem 'twilio-ruby', '~> 5.32.0'
|
||||||
# twitty will handle subscription of twitter account events
|
# twitty will handle subscription of twitter account events
|
||||||
|
|
|
@ -186,7 +186,7 @@ GEM
|
||||||
et-orbi (1.2.4)
|
et-orbi (1.2.4)
|
||||||
tzinfo
|
tzinfo
|
||||||
execjs (2.7.0)
|
execjs (2.7.0)
|
||||||
facebook-messenger (1.5.0)
|
facebook-messenger (2.0.1)
|
||||||
httparty (~> 0.13, >= 0.13.7)
|
httparty (~> 0.13, >= 0.13.7)
|
||||||
rack (>= 1.4.5)
|
rack (>= 1.4.5)
|
||||||
factory_bot (6.1.0)
|
factory_bot (6.1.0)
|
||||||
|
@ -333,7 +333,7 @@ GEM
|
||||||
method_source (1.0.0)
|
method_source (1.0.0)
|
||||||
mime-types (3.3.1)
|
mime-types (3.3.1)
|
||||||
mime-types-data (~> 3.2015)
|
mime-types-data (~> 3.2015)
|
||||||
mime-types-data (3.2020.0512)
|
mime-types-data (3.2021.0225)
|
||||||
mini_magick (4.10.1)
|
mini_magick (4.10.1)
|
||||||
mini_mime (1.1.0)
|
mini_mime (1.1.0)
|
||||||
mini_portile2 (2.5.1)
|
mini_portile2 (2.5.1)
|
||||||
|
@ -631,7 +631,7 @@ DEPENDENCIES
|
||||||
devise
|
devise
|
||||||
devise_token_auth
|
devise_token_auth
|
||||||
dotenv-rails
|
dotenv-rails
|
||||||
facebook-messenger (= 1.5.0)
|
facebook-messenger
|
||||||
factory_bot_rails
|
factory_bot_rails
|
||||||
faker
|
faker
|
||||||
fcm
|
fcm
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
require 'facebook/messenger'
|
require 'facebook/messenger'
|
||||||
|
|
||||||
class FacebookBot
|
class FacebookBot
|
||||||
include Facebook::Messenger
|
Facebook::Messenger::Bot.on :message do |message|
|
||||||
|
|
||||||
Bot.on :message do |message|
|
|
||||||
Rails.logger.info "MESSAGE_RECIEVED #{message}"
|
Rails.logger.info "MESSAGE_RECIEVED #{message}"
|
||||||
response = ::Integrations::Facebook::MessageParser.new(message)
|
response = ::Integrations::Facebook::MessageParser.new(message)
|
||||||
::Integrations::Facebook::MessageCreator.new(response).perform
|
::Integrations::Facebook::MessageCreator.new(response).perform
|
||||||
end
|
end
|
||||||
|
|
||||||
Bot.on :delivery do |delivery|
|
Facebook::Messenger::Bot.on :delivery do |delivery|
|
||||||
# delivery.ids # => 'mid.1457764197618:41d102a3e1ae206a38'
|
# delivery.ids # => 'mid.1457764197618:41d102a3e1ae206a38'
|
||||||
# delivery.sender # => { 'id' => '1008372609250235' }
|
# delivery.sender # => { 'id' => '1008372609250235' }
|
||||||
# delivery.recipient # => { 'id' => '2015573629214912' }
|
# delivery.recipient # => { 'id' => '2015573629214912' }
|
||||||
|
@ -20,7 +18,7 @@ class FacebookBot
|
||||||
Rails.logger.info "Human was online at #{delivery.at}"
|
Rails.logger.info "Human was online at #{delivery.at}"
|
||||||
end
|
end
|
||||||
|
|
||||||
Bot.on :message_echo do |message|
|
Facebook::Messenger::Bot.on :message_echo do |message|
|
||||||
Rails.logger.info "MESSAGE_ECHO #{message}"
|
Rails.logger.info "MESSAGE_ECHO #{message}"
|
||||||
response = ::Integrations::Facebook::MessageParser.new(message)
|
response = ::Integrations::Facebook::MessageParser.new(message)
|
||||||
::Integrations::Facebook::MessageCreator.new(response).perform
|
::Integrations::Facebook::MessageCreator.new(response).perform
|
||||||
|
|
|
@ -13,6 +13,7 @@ class Messages::Facebook::MessageBuilder
|
||||||
@outgoing_echo = outgoing_echo
|
@outgoing_echo = outgoing_echo
|
||||||
@sender_id = (@outgoing_echo ? @response.recipient_id : @response.sender_id)
|
@sender_id = (@outgoing_echo ? @response.recipient_id : @response.sender_id)
|
||||||
@message_type = (@outgoing_echo ? :outgoing : :incoming)
|
@message_type = (@outgoing_echo ? :outgoing : :incoming)
|
||||||
|
@attachments = (@response.attachments || [])
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform
|
def perform
|
||||||
|
@ -41,13 +42,19 @@ class Messages::Facebook::MessageBuilder
|
||||||
|
|
||||||
def build_message
|
def build_message
|
||||||
@message = conversation.messages.create!(message_params)
|
@message = conversation.messages.create!(message_params)
|
||||||
(response.attachments || []).each do |attachment|
|
@attachments.each do |attachment|
|
||||||
attachment_obj = @message.attachments.new(attachment_params(attachment).except(:remote_file_url))
|
process_attachment(attachment)
|
||||||
attachment_obj.save!
|
|
||||||
attach_file(attachment_obj, attachment_params(attachment)[:remote_file_url]) if attachment_params(attachment)[:remote_file_url]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def process_attachment(attachment)
|
||||||
|
return if attachment['type'].to_sym == :template
|
||||||
|
|
||||||
|
attachment_obj = @message.attachments.new(attachment_params(attachment).except(:remote_file_url))
|
||||||
|
attachment_obj.save!
|
||||||
|
attach_file(attachment_obj, attachment_params(attachment)[:remote_file_url]) if attachment_params(attachment)[:remote_file_url]
|
||||||
|
end
|
||||||
|
|
||||||
def attach_file(attachment, file_url)
|
def attach_file(attachment, file_url)
|
||||||
file_resource = LocalResource.new(file_url)
|
file_resource = LocalResource.new(file_url)
|
||||||
attachment.file.attach(io: file_resource.file, filename: file_resource.filename, content_type: file_resource.encoding)
|
attachment.file.attach(io: file_resource.file, filename: file_resource.filename, content_type: file_resource.encoding)
|
||||||
|
|
|
@ -1,55 +0,0 @@
|
||||||
class Api::V1::Accounts::FacebookIndicatorsController < Api::V1::Accounts::BaseController
|
|
||||||
before_action :set_access_token
|
|
||||||
around_action :handle_with_exception
|
|
||||||
|
|
||||||
def mark_seen
|
|
||||||
fb_bot.deliver(payload('mark_seen'), access_token: @access_token)
|
|
||||||
head :ok
|
|
||||||
end
|
|
||||||
|
|
||||||
def typing_on
|
|
||||||
fb_bot.deliver(payload('typing_on'), access_token: @access_token)
|
|
||||||
head :ok
|
|
||||||
end
|
|
||||||
|
|
||||||
def typing_off
|
|
||||||
fb_bot.deliver(payload('typing_off'), access_token: @access_token)
|
|
||||||
head :ok
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def fb_bot
|
|
||||||
::Facebook::Messenger::Bot
|
|
||||||
end
|
|
||||||
|
|
||||||
def handle_with_exception
|
|
||||||
yield
|
|
||||||
rescue Facebook::Messenger::Error => e
|
|
||||||
Rails.logger.debug "Rescued: #{e.inspect}"
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def payload(action)
|
|
||||||
{
|
|
||||||
recipient: { id: contact.source_id },
|
|
||||||
sender_action: action
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def inbox
|
|
||||||
@inbox ||= Current.account.inboxes.find(permitted_params[:inbox_id])
|
|
||||||
end
|
|
||||||
|
|
||||||
def set_access_token
|
|
||||||
@access_token = inbox.channel.page_access_token
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
end
|
|
|
@ -14,7 +14,7 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_message_to_facebook(delivery_params)
|
def send_message_to_facebook(delivery_params)
|
||||||
result = FacebookBot::Bot.deliver(delivery_params, access_token: message.channel_token)
|
result = Facebook::Messenger::Bot.deliver(delivery_params, page_id: channel.page_id)
|
||||||
message.update!(source_id: JSON.parse(result)['message_id'])
|
message.update!(source_id: JSON.parse(result)['message_id'])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
# Remember that Rails only eager loads everything in its production environment.
|
||||||
|
# In the development and test environments, it only requires files as you reference constants.
|
||||||
|
# You'll need to explicitly load app/bot
|
||||||
|
|
||||||
unless Rails.env.production?
|
unless Rails.env.production?
|
||||||
bot_files = Dir[Rails.root.join('app', 'bot', '**', '*.rb')]
|
bot_files = Dir[Rails.root.join('app', 'bot', '**', '*.rb')]
|
||||||
bot_reloader = ActiveSupport::FileUpdateChecker.new(bot_files) do
|
bot_reloader = ActiveSupport::FileUpdateChecker.new(bot_files) do
|
||||||
|
@ -11,21 +15,7 @@ unless Rails.env.production?
|
||||||
bot_files.each { |file| require_dependency file }
|
bot_files.each { |file| require_dependency file }
|
||||||
end
|
end
|
||||||
|
|
||||||
module Facebook
|
# ref: https://github.com/jgorset/facebook-messenger#make-a-configuration-provider
|
||||||
module Messenger
|
|
||||||
module Incoming
|
|
||||||
# The Message class represents an incoming Facebook Messenger message.
|
|
||||||
class Message
|
|
||||||
include Facebook::Messenger::Incoming::Common
|
|
||||||
|
|
||||||
def app_id
|
|
||||||
@messaging['message']['app_id']
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ChatwootFbProvider < Facebook::Messenger::Configuration::Providers::Base
|
class ChatwootFbProvider < Facebook::Messenger::Configuration::Providers::Base
|
||||||
def valid_verify_token?(_verify_token)
|
def valid_verify_token?(_verify_token)
|
||||||
ENV['FB_VERIFY_TOKEN']
|
ENV['FB_VERIFY_TOKEN']
|
||||||
|
@ -36,13 +26,13 @@ class ChatwootFbProvider < Facebook::Messenger::Configuration::Providers::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def access_token_for(page_id)
|
def access_token_for(page_id)
|
||||||
Channel::FacebookPage.where(page_id: page_id).last.access_token
|
Channel::FacebookPage.where(page_id: page_id).last.page_access_token
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def bot
|
def bot
|
||||||
MyApp::Bot
|
Chatwoot::Bot
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -88,14 +88,6 @@ Rails.application.routes.draw do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
resources :facebook_indicators, only: [] do
|
|
||||||
collection do
|
|
||||||
post :mark_seen
|
|
||||||
post :typing_on
|
|
||||||
post :typing_off
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
resources :inboxes, only: [:index, :create, :update, :destroy] do
|
resources :inboxes, only: [:index, :create, :update, :destroy] do
|
||||||
get :assignable_agents, on: :member
|
get :assignable_agents, on: :member
|
||||||
get :campaigns, on: :member
|
get :campaigns, on: :member
|
||||||
|
|
|
@ -1,143 +0,0 @@
|
||||||
require 'rails_helper'
|
|
||||||
|
|
||||||
RSpec.describe 'Facebook Indicators API', type: :request do
|
|
||||||
let(:account) { create(:account) }
|
|
||||||
let(:facebook_channel) { create(:channel_facebook_page, account: account) }
|
|
||||||
let(:inbox) { create(:inbox, account: account, channel: facebook_channel) }
|
|
||||||
let(:contact) { create(:contact, account: account) }
|
|
||||||
let(:valid_params) { { contact_id: contact.id, inbox_id: inbox.id } }
|
|
||||||
|
|
||||||
before do
|
|
||||||
allow(Facebook::Messenger::Bot).to receive(:deliver).and_return(true)
|
|
||||||
allow(Facebook::Messenger::Subscriptions).to receive(:subscribe).and_return(true)
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'POST /api/v1/accounts/{account.id}/facebook_indicators/mark_seen' do
|
|
||||||
context 'when it is an unauthenticated user' do
|
|
||||||
it 'returns unauthorized' do
|
|
||||||
post "/api/v1/accounts/#{account.id}/facebook_indicators/mark_seen"
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:unauthorized)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when it is an authenticated user' do
|
|
||||||
let(:agent) { create(:user, account: account, role: :agent) }
|
|
||||||
|
|
||||||
it 'marks a payload as seen' do
|
|
||||||
contact_inbox = create(:contact_inbox, contact: contact, inbox: inbox)
|
|
||||||
|
|
||||||
expect(Facebook::Messenger::Bot).to receive(:deliver).with(
|
|
||||||
{ recipient: { id: contact_inbox.source_id }, sender_action: 'mark_seen' },
|
|
||||||
access_token: inbox.channel.page_access_token
|
|
||||||
)
|
|
||||||
|
|
||||||
post "/api/v1/accounts/#{account.id}/facebook_indicators/mark_seen",
|
|
||||||
headers: agent.create_new_auth_token,
|
|
||||||
params: valid_params,
|
|
||||||
as: :json
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:success)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'rescues an error' do
|
|
||||||
create(:contact_inbox, contact: contact, inbox: inbox)
|
|
||||||
|
|
||||||
allow(Facebook::Messenger::Bot).to receive(:deliver).and_raise(Facebook::Messenger::Error)
|
|
||||||
|
|
||||||
post "/api/v1/accounts/#{account.id}/facebook_indicators/mark_seen",
|
|
||||||
headers: agent.create_new_auth_token,
|
|
||||||
params: valid_params,
|
|
||||||
as: :json
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:success)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'POST /api/v1/accounts/{account.id}/facebook_indicators/typing_on' do
|
|
||||||
context 'when it is an unauthenticated user' do
|
|
||||||
it 'returns unauthorized' do
|
|
||||||
post "/api/v1/accounts/#{account.id}/facebook_indicators/typing_on"
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:unauthorized)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when it is an authenticated user' do
|
|
||||||
let(:agent) { create(:user, account: account, role: :agent) }
|
|
||||||
|
|
||||||
it 'marks a payload as typing_on' do
|
|
||||||
contact_inbox = create(:contact_inbox, contact: contact, inbox: inbox)
|
|
||||||
|
|
||||||
expect(Facebook::Messenger::Bot).to receive(:deliver).with(
|
|
||||||
{ recipient: { id: contact_inbox.source_id }, sender_action: 'typing_on' },
|
|
||||||
access_token: inbox.channel.page_access_token
|
|
||||||
)
|
|
||||||
|
|
||||||
post "/api/v1/accounts/#{account.id}/facebook_indicators/typing_on",
|
|
||||||
headers: agent.create_new_auth_token,
|
|
||||||
params: valid_params,
|
|
||||||
as: :json
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:success)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'rescues an error' do
|
|
||||||
create(:contact_inbox, contact: contact, inbox: inbox)
|
|
||||||
|
|
||||||
allow(Facebook::Messenger::Bot).to receive(:deliver).and_raise(Facebook::Messenger::Error)
|
|
||||||
|
|
||||||
post "/api/v1/accounts/#{account.id}/facebook_indicators/typing_on",
|
|
||||||
headers: agent.create_new_auth_token,
|
|
||||||
params: valid_params,
|
|
||||||
as: :json
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:success)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'POST /api/v1/accounts/{account.id}/facebook_indicators/typing_off' do
|
|
||||||
context 'when it is an unauthenticated user' do
|
|
||||||
it 'returns unauthorized' do
|
|
||||||
post "/api/v1/accounts/#{account.id}/facebook_indicators/typing_off"
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:unauthorized)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when it is an authenticated user' do
|
|
||||||
let(:agent) { create(:user, account: account, role: :agent) }
|
|
||||||
|
|
||||||
it 'marks a payload as typing_off' do
|
|
||||||
contact_inbox = create(:contact_inbox, contact: contact, inbox: inbox)
|
|
||||||
|
|
||||||
expect(Facebook::Messenger::Bot).to receive(:deliver).with(
|
|
||||||
{ recipient: { id: contact_inbox.source_id }, sender_action: 'typing_off' },
|
|
||||||
access_token: inbox.channel.page_access_token
|
|
||||||
)
|
|
||||||
|
|
||||||
post "/api/v1/accounts/#{account.id}/facebook_indicators/typing_off",
|
|
||||||
headers: agent.create_new_auth_token,
|
|
||||||
params: valid_params,
|
|
||||||
as: :json
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:success)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'rescues an error' do
|
|
||||||
create(:contact_inbox, contact: contact, inbox: inbox)
|
|
||||||
|
|
||||||
allow(Facebook::Messenger::Bot).to receive(:deliver).and_raise(Facebook::Messenger::Error)
|
|
||||||
|
|
||||||
post "/api/v1/accounts/#{account.id}/facebook_indicators/typing_off",
|
|
||||||
headers: agent.create_new_auth_token,
|
|
||||||
params: valid_params,
|
|
||||||
as: :json
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:success)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -10,7 +10,7 @@ describe Facebook::SendOnFacebookService do
|
||||||
end
|
end
|
||||||
|
|
||||||
let!(:account) { create(:account) }
|
let!(:account) { create(:account) }
|
||||||
let(:bot) { class_double('FacebookBot::Bot').as_stubbed_const }
|
let(:bot) { class_double('Facebook::Messenger::Bot').as_stubbed_const }
|
||||||
let!(:widget_inbox) { create(:inbox, account: account) }
|
let!(:widget_inbox) { create(:inbox, account: account) }
|
||||||
let!(:facebook_channel) { create(:channel_facebook_page, account: account) }
|
let!(:facebook_channel) { create(:channel_facebook_page, account: account) }
|
||||||
let!(:facebook_inbox) { create(:inbox, channel: facebook_channel, account: account) }
|
let!(:facebook_inbox) { create(:inbox, channel: facebook_channel, account: account) }
|
||||||
|
@ -61,7 +61,7 @@ describe Facebook::SendOnFacebookService do
|
||||||
expect(bot).to have_received(:deliver).with({
|
expect(bot).to have_received(:deliver).with({
|
||||||
recipient: { id: contact_inbox.source_id },
|
recipient: { id: contact_inbox.source_id },
|
||||||
message: { text: message.content }
|
message: { text: message.content }
|
||||||
}, { access_token: facebook_channel.page_access_token })
|
}, { page_id: facebook_channel.page_id })
|
||||||
expect(bot).to have_received(:deliver).with({
|
expect(bot).to have_received(:deliver).with({
|
||||||
recipient: { id: contact_inbox.source_id },
|
recipient: { id: contact_inbox.source_id },
|
||||||
message: {
|
message: {
|
||||||
|
@ -72,7 +72,7 @@ describe Facebook::SendOnFacebookService do
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, { access_token: facebook_channel.page_access_token })
|
}, { page_id: facebook_channel.page_id })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue