From 3f5ce2ddbf66beff9beb7be121ff7f0f975edb5a Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Mon, 18 May 2020 15:32:26 +0530 Subject: [PATCH] Bug: Fix autoload_paths for facebook bot (#877) * Fix autoload_paths for facebook bot --- app/bot/bot.rb | 19 ---------------- app/bot/facebook_bot.rb | 22 +++++++++++++++++++ app/services/facebook/send_reply_service.rb | 2 +- config/application.rb | 2 +- .../facebook/send_reply_service_spec.rb | 2 +- 5 files changed, 25 insertions(+), 22 deletions(-) delete mode 100644 app/bot/bot.rb create mode 100644 app/bot/facebook_bot.rb diff --git a/app/bot/bot.rb b/app/bot/bot.rb deleted file mode 100644 index c834e7774..000000000 --- a/app/bot/bot.rb +++ /dev/null @@ -1,19 +0,0 @@ -# app/bot/facebook_bot.rb -require 'facebook/messenger' -include Facebook::Messenger - -Bot.on :message do |message| - response = ::Integrations::Facebook::MessageParser.new(message) - ::Integrations::Facebook::MessageCreator.new(response).perform -end - -Bot.on :delivery do |delivery| - # 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 - puts "Human was online at #{delivery.at}" -end diff --git a/app/bot/facebook_bot.rb b/app/bot/facebook_bot.rb new file mode 100644 index 000000000..f0e04ebc2 --- /dev/null +++ b/app/bot/facebook_bot.rb @@ -0,0 +1,22 @@ +require 'facebook/messenger' + +class FacebookBot + include 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 + end + + Bot.on :delivery do |delivery| + # 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 +end diff --git a/app/services/facebook/send_reply_service.rb b/app/services/facebook/send_reply_service.rb index df897a1f2..9173ecac7 100644 --- a/app/services/facebook/send_reply_service.rb +++ b/app/services/facebook/send_reply_service.rb @@ -6,7 +6,7 @@ class Facebook::SendReplyService return if inbox.channel.class.to_s != 'Channel::FacebookPage' return unless outgoing_message_from_chatwoot? - Bot.deliver(delivery_params, access_token: message.channel_token) + FacebookBot::Bot.deliver(delivery_params, access_token: message.channel_token) end private diff --git a/config/application.rb b/config/application.rb index c7421cb4b..436a9392f 100644 --- a/config/application.rb +++ b/config/application.rb @@ -18,7 +18,7 @@ module Chatwoot # This is required in production for zeitwerk to autoload the file config.paths.add File.join('app', 'bot'), glob: File.join('**', '*.rb') - config.autoload_paths << Rails.root.join('app/bot') + config.autoload_paths << Rails.root.join('app/bot/*') # Settings in config/environments/* take precedence over those specified here. # Application configuration can go into files in config/initializers diff --git a/spec/services/facebook/send_reply_service_spec.rb b/spec/services/facebook/send_reply_service_spec.rb index 113b091e0..a74f1e826 100644 --- a/spec/services/facebook/send_reply_service_spec.rb +++ b/spec/services/facebook/send_reply_service_spec.rb @@ -9,7 +9,7 @@ describe Facebook::SendReplyService do end let!(:account) { create(:account) } - let(:bot) { class_double('Bot').as_stubbed_const } + let(:bot) { class_double('FacebookBot::Bot').as_stubbed_const } let!(:widget_inbox) { create(:inbox, account: account) } let!(:facebook_channel) { create(:channel_facebook_page, account: account) } let!(:facebook_inbox) { create(:inbox, channel: facebook_channel, account: account) }