diff --git a/app/models/channel/facebook_page.rb b/app/models/channel/facebook_page.rb index bed88e575..6b36a3a54 100644 --- a/app/models/channel/facebook_page.rb +++ b/app/models/channel/facebook_page.rb @@ -33,7 +33,7 @@ class Channel::FacebookPage < ApplicationRecord end def has_24_hour_messaging_window? - true + false end def create_contact_inbox(instagram_id, name) diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 409efab6d..cf3a4c256 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -90,10 +90,20 @@ class Conversation < ApplicationRecord delegate :auto_resolve_duration, to: :account def can_reply? + return last_message_less_than_24_hrs? if additional_attributes['type'] == 'instagram_direct_message' + return true unless inbox&.channel&.has_24_hour_messaging_window? - last_incoming_message = messages.incoming.last + return false if last_incoming_message.nil? + last_message_less_than_24_hrs? + end + + def last_incoming_message + messages&.incoming&.last + end + + def last_message_less_than_24_hrs? return false if last_incoming_message.nil? Time.current < last_incoming_message.created_at + 24.hours diff --git a/app/services/facebook/send_on_facebook_service.rb b/app/services/facebook/send_on_facebook_service.rb index 12e416ef7..52c259edb 100644 --- a/app/services/facebook/send_on_facebook_service.rb +++ b/app/services/facebook/send_on_facebook_service.rb @@ -22,7 +22,9 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService def fb_text_message_params { recipient: { id: contact.get_source_id(inbox.id) }, - message: { text: message.content } + message: { text: message.content }, + messaging_type: 'MESSAGE_TAG', + tag: 'ACCOUNT_UPDATE' } end @@ -37,7 +39,9 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService url: attachment.file_url } } - } + }, + messaging_type: 'MESSAGE_TAG', + tag: 'ACCOUNT_UPDATE' } end diff --git a/spec/jobs/webhooks/instagram_events_job_spec.rb b/spec/jobs/webhooks/instagram_events_job_spec.rb index bf3b43e14..69952920a 100644 --- a/spec/jobs/webhooks/instagram_events_job_spec.rb +++ b/spec/jobs/webhooks/instagram_events_job_spec.rb @@ -57,7 +57,6 @@ describe Webhooks::InstagramEventsJob do instagram_webhook.perform_now(test_params[:entry]) instagram_inbox.reload - expect(instagram_inbox.messages.count).to be 1 expect(instagram_inbox.messages.last.content).to eq('This is a test message from facebook.') end diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index d5295e340..8c278a81b 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -489,7 +489,7 @@ RSpec.describe Conversation, type: :model do let!(:conversation) { create(:conversation, inbox: facebook_inbox, account: facebook_channel.account) } it 'returns false if there are no incoming messages' do - expect(conversation.can_reply?).to eq false + expect(conversation.can_reply?).to eq true end it 'return false if last incoming message is outside of 24 hour window' do @@ -500,7 +500,7 @@ RSpec.describe Conversation, type: :model do conversation: conversation, created_at: Time.now - 25.hours ) - expect(conversation.can_reply?).to eq false + expect(conversation.can_reply?).to eq true end it 'return true if last incoming message is inside 24 hour window' do diff --git a/spec/services/facebook/send_on_facebook_service_spec.rb b/spec/services/facebook/send_on_facebook_service_spec.rb index 8ff842591..e0a39ba75 100644 --- a/spec/services/facebook/send_on_facebook_service_spec.rb +++ b/spec/services/facebook/send_on_facebook_service_spec.rb @@ -60,7 +60,9 @@ describe Facebook::SendOnFacebookService do ::Facebook::SendOnFacebookService.new(message: message).perform expect(bot).to have_received(:deliver).with({ recipient: { id: contact_inbox.source_id }, - message: { text: message.content } + message: { text: message.content }, + messaging_type: 'MESSAGE_TAG', + tag: 'ACCOUNT_UPDATE' }, { page_id: facebook_channel.page_id }) expect(bot).to have_received(:deliver).with({ recipient: { id: contact_inbox.source_id }, @@ -71,7 +73,9 @@ describe Facebook::SendOnFacebookService do url: attachment.file_url } } - } + }, + messaging_type: 'MESSAGE_TAG', + tag: 'ACCOUNT_UPDATE' }, { page_id: facebook_channel.page_id }) end end