From d18c8fc08a98eb0f8eec81a7ecfe2e3e3f64ccb5 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Fri, 4 Sep 2020 19:13:47 +0530 Subject: [PATCH] chore: Add message_echo listener for Facebook (#1192) Co-authored-by: Sojan Jose --- app/bot/facebook_bot.rb | 6 ++++++ app/builders/messages/facebook/message_builder.rb | 7 ++++--- app/models/channel/api.rb | 4 ++++ app/models/channel/email.rb | 4 ++++ app/models/channel/facebook_page.rb | 4 ++++ app/models/channel/twilio_sms.rb | 8 ++++---- app/models/channel/twitter_profile.rb | 4 ++++ app/models/channel/web_widget.rb | 4 ++++ app/models/inbox.rb | 4 ++++ lib/integrations/slack/send_on_slack_service.rb | 5 +++-- spec/models/channel/facebook_page_spec.rb | 6 +++++- spec/models/inbox_spec.rb | 6 +++++- 12 files changed, 51 insertions(+), 11 deletions(-) diff --git a/app/bot/facebook_bot.rb b/app/bot/facebook_bot.rb index f0e04ebc2..f3f87220d 100644 --- a/app/bot/facebook_bot.rb +++ b/app/bot/facebook_bot.rb @@ -19,4 +19,10 @@ class FacebookBot updater.perform Rails.logger.info "Human was online at #{delivery.at}" end + + Bot.on :message_echo do |message| + Rails.logger.info "MESSAGE_ECHO #{message}" + response = ::Integrations::Facebook::MessageParser.new(message) + ::Integrations::Facebook::MessageCreator.new(response).perform + end end diff --git a/app/builders/messages/facebook/message_builder.rb b/app/builders/messages/facebook/message_builder.rb index aa232d2e1..92fc28c0e 100644 --- a/app/builders/messages/facebook/message_builder.rb +++ b/app/builders/messages/facebook/message_builder.rb @@ -10,8 +10,9 @@ class Messages::Facebook::MessageBuilder def initialize(response, inbox, outgoing_echo = false) @response = response @inbox = inbox - @sender_id = (outgoing_echo ? @response.recipient_id : @response.sender_id) - @message_type = (outgoing_echo ? :outgoing : :incoming) + @outgoing_echo = outgoing_echo + @sender_id = (@outgoing_echo ? @response.recipient_id : @response.sender_id) + @message_type = (@outgoing_echo ? :outgoing : :incoming) end def perform @@ -120,7 +121,7 @@ class Messages::Facebook::MessageBuilder message_type: @message_type, content: response.content, source_id: response.identifier, - sender: contact + sender: @outgoing_echo ? nil : contact } end diff --git a/app/models/channel/api.rb b/app/models/channel/api.rb index 7f3f18111..baa6015d3 100644 --- a/app/models/channel/api.rb +++ b/app/models/channel/api.rb @@ -17,6 +17,10 @@ class Channel::Api < ApplicationRecord has_one :inbox, as: :channel, dependent: :destroy + def name + 'API' + end + def has_24_hour_messaging_window? false end diff --git a/app/models/channel/email.rb b/app/models/channel/email.rb index a87419678..582a1f26e 100644 --- a/app/models/channel/email.rb +++ b/app/models/channel/email.rb @@ -26,6 +26,10 @@ class Channel::Email < ApplicationRecord has_one :inbox, as: :channel, dependent: :destroy before_validation :ensure_forward_to_address, on: :create + def name + 'Email' + end + def has_24_hour_messaging_window? false end diff --git a/app/models/channel/facebook_page.rb b/app/models/channel/facebook_page.rb index a3bfaf1f0..80dbf0e6d 100644 --- a/app/models/channel/facebook_page.rb +++ b/app/models/channel/facebook_page.rb @@ -28,6 +28,10 @@ class Channel::FacebookPage < ApplicationRecord after_create_commit :subscribe before_destroy :unsubscribe + def name + 'Facebook' + end + def has_24_hour_messaging_window? true end diff --git a/app/models/channel/twilio_sms.rb b/app/models/channel/twilio_sms.rb index 81c38793a..3174409f9 100644 --- a/app/models/channel/twilio_sms.rb +++ b/app/models/channel/twilio_sms.rb @@ -30,11 +30,11 @@ class Channel::TwilioSms < ApplicationRecord has_one :inbox, as: :channel, dependent: :destroy + def name + medium == :sms ? 'Twilio SMS' : 'Whatsapp' + end + def has_24_hour_messaging_window? true end - - def name - 'Twilio SMS' - end end diff --git a/app/models/channel/twitter_profile.rb b/app/models/channel/twitter_profile.rb index 1430e471d..be32087cd 100644 --- a/app/models/channel/twitter_profile.rb +++ b/app/models/channel/twitter_profile.rb @@ -26,6 +26,10 @@ class Channel::TwitterProfile < ApplicationRecord before_destroy :unsubscribe + def name + 'Twitter' + end + def has_24_hour_messaging_window? false end diff --git a/app/models/channel/web_widget.rb b/app/models/channel/web_widget.rb index 1e3b18cec..e83b4f9c6 100644 --- a/app/models/channel/web_widget.rb +++ b/app/models/channel/web_widget.rb @@ -33,6 +33,10 @@ class Channel::WebWidget < ApplicationRecord 2 => :emoji_picker, :column => 'feature_flags' + def name + 'Website' + end + def has_24_hour_messaging_window? false end diff --git a/app/models/inbox.rb b/app/models/inbox.rb index efdeba6b8..1f01e4bdd 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -66,6 +66,10 @@ class Inbox < ApplicationRecord channel.class.name.to_s == 'Channel::WebWidget' end + def inbox_type + channel.name + end + def webhook_data { id: id, diff --git a/lib/integrations/slack/send_on_slack_service.rb b/lib/integrations/slack/send_on_slack_service.rb index c7c1fd7a4..c024aae1a 100644 --- a/lib/integrations/slack/send_on_slack_service.rb +++ b/lib/integrations/slack/send_on_slack_service.rb @@ -32,7 +32,7 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService if conversation.identifier.present? "#{private_indicator}#{message.content}" else - "*Inbox: #{message.inbox.name}* \n\n #{message.content}" + "*Inbox: #{message.inbox.name} [#{message.inbox.inbox_type}]* \n\n #{message.content}" end end @@ -43,11 +43,12 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService def send_message sender = message.sender sender_type = sender.class == Contact ? 'Contact' : 'Agent' + sender_name = sender.try(:name) ? "#{sender_type}: #{sender.try(:name)}" : sender_type @slack_message = slack_client.chat_postMessage( channel: hook.reference_id, text: message_content, - username: "#{sender_type}: #{sender.try(:name)}", + username: sender_name, thread_ts: conversation.identifier, icon_url: avatar_url(sender) ) diff --git a/spec/models/channel/facebook_page_spec.rb b/spec/models/channel/facebook_page_spec.rb index 59aba39d5..c31f466d5 100644 --- a/spec/models/channel/facebook_page_spec.rb +++ b/spec/models/channel/facebook_page_spec.rb @@ -3,10 +3,14 @@ require 'rails_helper' RSpec.describe Channel::FacebookPage do - before { create(:channel_facebook_page) } + let(:channel) { create(:channel_facebook_page) } it { is_expected.to validate_presence_of(:account_id) } # it { is_expected.to validate_uniqueness_of(:page_id).scoped_to(:account_id) } it { is_expected.to belong_to(:account) } it { is_expected.to have_one(:inbox).dependent(:destroy) } + + it 'has a valid name' do + expect(channel.name).to eq('Facebook') + end end diff --git a/spec/models/inbox_spec.rb b/spec/models/inbox_spec.rb index e92e66c31..e494173e8 100644 --- a/spec/models/inbox_spec.rb +++ b/spec/models/inbox_spec.rb @@ -69,13 +69,17 @@ RSpec.describe Inbox do it do expect(inbox.facebook?).to eq(true) + expect(inbox.inbox_type).to eq('Facebook') end end context 'when the channel type is not Channel::FacebookPage' do let(:channel_val) { Channel::WebWidget.new } - it { expect(inbox.facebook?).to eq(false) } + it do + expect(inbox.facebook?).to eq(false) + expect(inbox.inbox_type).to eq('Website') + end end end end