chore: Move Facebook event processing to worker (#2988)

This commit is contained in:
Sojan Jose 2021-09-13 18:05:14 +05:30 committed by GitHub
parent 26a3150fd8
commit 328edd24de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 21 deletions

View file

@ -0,0 +1,8 @@
class Webhooks::FacebookEventsJob < ApplicationJob
queue_as :default
def perform(message)
response = ::Integrations::Facebook::MessageParser.new(message)
::Integrations::Facebook::MessageCreator.new(response).perform
end
end

View file

@ -25,9 +25,7 @@ Rails.application.reloader.to_prepare do
end
Facebook::Messenger::Bot.on :message do |message|
Rails.logger.info "MESSAGE_RECIEVED #{message}"
response = ::Integrations::Facebook::MessageParser.new(message)
::Integrations::Facebook::MessageCreator.new(response).perform
Webhooks::FacebookEventsJob.perform_later(message.to_json)
end
Facebook::Messenger::Bot.on :delivery do |delivery|
@ -42,8 +40,6 @@ Rails.application.reloader.to_prepare do
end
Facebook::Messenger::Bot.on :message_echo do |message|
Rails.logger.info "MESSAGE_ECHO #{message}"
response = ::Integrations::Facebook::MessageParser.new(message)
::Integrations::Facebook::MessageCreator.new(response).perform
Webhooks::FacebookEventsJob.perform_later(message.to_json)
end
end

View file

@ -22,6 +22,7 @@ class Integrations::Facebook::MessageCreator
private
def agent_message_via_echo?
# TODO : check and remove send_from_chatwoot_app if not working
response.echo? && !response.sent_from_chatwoot_app?
# this means that it is an agent message from page, but not sent from chatwoot.
# User can send from fb page directly on mobile / web messenger, so this case should be handled as agent message

View file

@ -2,45 +2,47 @@
class Integrations::Facebook::MessageParser
def initialize(response_json)
@response = response_json
@response = JSON.parse(response_json)
end
def sender_id
@response.sender['id']
@response.dig 'messaging', 'sender', 'id'
end
def recipient_id
@response.recipient['id']
@response.dig 'messaging', 'recipient', 'id'
end
def time_stamp
@response.sent_at
@response.dig 'messaging', 'timestamp'
end
def content
@response.text
@response.dig 'messaging', 'message', 'text'
end
def sequence
@response.seq
@response.dig 'messaging', 'message', 'seq'
end
def attachments
@response.attachments
@response.dig 'messaging', 'message', 'attachments'
end
def identifier
@response.id
@response.dig 'messaging', 'message', 'mid'
end
def echo?
@response.echo?
@response.dig 'messaging', 'message', 'is_echo'
end
# TODO : i don't think the payload contains app_id. if not remove
def app_id
@response.app_id
@response.dig 'messaging', 'message', 'app_id'
end
# TODO : does this work ?
def sent_from_chatwoot_app?
app_id && app_id == ENV['FB_APP_ID'].to_i
end

View file

@ -4,7 +4,7 @@ describe ::Messages::Facebook::MessageBuilder do
subject(:message_builder) { described_class.new(incoming_fb_text_message, facebook_channel.inbox).perform }
let!(:facebook_channel) { create(:channel_facebook_page) }
let!(:message_object) { JSON.parse(build(:incoming_fb_text_message).to_json, object_class: OpenStruct) }
let!(:message_object) { build(:incoming_fb_text_message).to_json }
let!(:incoming_fb_text_message) { Integrations::Facebook::MessageParser.new(message_object) }
let(:fb_object) { double }

View file

@ -2,10 +2,11 @@
FactoryBot.define do
factory :incoming_fb_text_message, class: Hash do
sender { { id: '3383290475046708' } }
recipient { { id: '117172741761305' } }
message { { mid: 'm_KXGKDUpO6xbVdAmZFBVpzU1AhKVJdAIUnUH4cwkvb_K3iZsWhowDRyJ_DcowEpJjncaBwdCIoRrixvCbbO1PcA', text: 'facebook message' } }
text { 'facebook message' }
messaging do
{ sender: { id: '3383290475046708' },
recipient: { id: '117172741761305' },
message: { mid: 'm_KXGKDUpO6xbVdAmZFBVpzU1AhKVJdAIUnUH4cwkvb_K3iZsWhowDRyJ_DcowEpJjncaBwdCIoRrixvCbbO1PcA', text: 'facebook message' } }
end
initialize_with { attributes }
end

View file

@ -0,0 +1,27 @@
require 'rails_helper'
RSpec.describe Webhooks::FacebookEventsJob, type: :job do
subject(:job) { described_class.perform_later(params) }
let!(:params) { { test: 'test' } }
it 'enqueues the job' do
expect { job }.to have_enqueued_job(described_class)
.with(params)
.on_queue('default')
end
context 'when called with params' do
it 'calls MessagePArsed and do message create' do
parser = double
creator = double
allow(::Integrations::Facebook::MessageParser).to receive(:new).and_return(parser)
allow(::Integrations::Facebook::MessageCreator).to receive(:new).and_return(creator)
allow(creator).to receive(:perform).and_return(true)
expect(::Integrations::Facebook::MessageParser).to receive(:new).with(params)
expect(::Integrations::Facebook::MessageCreator).to receive(:new).with(parser)
expect(creator).to receive(:perform)
described_class.perform_now(params)
end
end
end