diff --git a/app/services/facebook/send_on_facebook_service.rb b/app/services/facebook/send_on_facebook_service.rb index baf72ef6e..0b2f45590 100644 --- a/app/services/facebook/send_on_facebook_service.rb +++ b/app/services/facebook/send_on_facebook_service.rb @@ -45,12 +45,12 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService end def fb_text_message_params - { + params = { recipient: { id: contact.get_source_id(inbox.id) }, - message: fb_text_message_payload, - messaging_type: 'MESSAGE_TAG', - tag: message_tag + message: fb_text_message_payload } + + merge_human_agent_tag(params) end def fb_text_message_payload @@ -79,7 +79,7 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService end def fb_attachment_message_params(attachment) - { + params = { recipient: { id: contact.get_source_id(inbox.id) }, message: { attachment: { @@ -88,14 +88,21 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService url: attachment.download_url } } - }, - messaging_type: 'MESSAGE_TAG', - tag: message_tag + } } + + merge_human_agent_tag(params) end - def message_tag - @message_tag ||= GlobalConfigService.load('ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT', nil) ? 'HUMAN_AGENT' : 'ACCOUNT_UPDATE' + def merge_human_agent_tag(params) + unless GlobalConfigService.load('ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT', nil) + params[:messaging_type] = 'RESPONSE' + return params + end + + params[:messaging_type] = 'MESSAGE_TAG' + params[:tag] = 'HUMAN_AGENT' + params end def attachment_type(attachment) @@ -104,11 +111,6 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService 'file' end - def sent_first_outgoing_message_after_24_hours? - # we can send max 1 message after 24 hour window - conversation.messages.outgoing.where('id > ?', conversation.last_incoming_message.id).count == 1 - end - def handle_facebook_error(exception) # Refer: https://github.com/jgorset/facebook-messenger/blob/64fe1f5cef4c1e3fca295b205037f64dfebdbcab/lib/facebook/messenger/error.rb return unless exception.to_s.include?('The session has been invalidated') || exception.to_s.include?('Error validating access token') diff --git a/spec/services/facebook/send_on_facebook_service_spec.rb b/spec/services/facebook/send_on_facebook_service_spec.rb index f99b1c469..0a2b6407c 100644 --- a/spec/services/facebook/send_on_facebook_service_spec.rb +++ b/spec/services/facebook/send_on_facebook_service_spec.rb @@ -73,8 +73,7 @@ describe Facebook::SendOnFacebookService do expect(bot).to have_received(:deliver).with({ recipient: { id: contact_inbox.source_id }, message: { text: message.content }, - messaging_type: 'MESSAGE_TAG', - tag: 'ACCOUNT_UPDATE' + messaging_type: 'RESPONSE' }, { page_id: facebook_channel.page_id }) expect(bot).to have_received(:deliver).with({ recipient: { id: contact_inbox.source_id }, @@ -86,17 +85,26 @@ describe Facebook::SendOnFacebookService do } } }, - messaging_type: 'MESSAGE_TAG', - tag: 'ACCOUNT_UPDATE' + messaging_type: 'RESPONSE' }, { page_id: facebook_channel.page_id }) end + it 'sends as a standard RESPONSE without a tag by default' do + message = create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, conversation: conversation) + described_class.new(message: message).perform + expect(bot).to have_received(:deliver).with( + hash_including(messaging_type: 'RESPONSE'), + { page_id: facebook_channel.page_id } + ) + expect(bot).not_to have_received(:deliver).with(hash_including(:tag), anything) + end + it 'sends with HUMAN_AGENT tag when ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT is enabled' do with_modified_env ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT: 'true' do message = create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, conversation: conversation) described_class.new(message: message).perform expect(bot).to have_received(:deliver).with( - hash_including(tag: 'HUMAN_AGENT'), + hash_including(messaging_type: 'MESSAGE_TAG', tag: 'HUMAN_AGENT'), { page_id: facebook_channel.page_id } ) end @@ -201,8 +209,7 @@ describe Facebook::SendOnFacebookService do { content_type: 'text', payload: 'text 2', title: 'text 2' } ] }, - messaging_type: 'MESSAGE_TAG', - tag: 'ACCOUNT_UPDATE' + messaging_type: 'RESPONSE' }, { page_id: facebook_channel.page_id }) end end