From c0f5a59cf88b65e9a30c650158f0f80cc49706d3 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Wed, 10 Nov 2021 18:32:46 +0530 Subject: [PATCH] Bug: Instagram render template message twice (#3313) --- .../messages/instagram/message_builder.rb | 19 +++++++------------ .../instagram/send_on_instagram_service.rb | 5 +++-- .../send_on_instagram_service_spec.rb | 8 +++++--- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app/builders/messages/instagram/message_builder.rb b/app/builders/messages/instagram/message_builder.rb index 18c82d813..69475fb16 100644 --- a/app/builders/messages/instagram/message_builder.rb +++ b/app/builders/messages/instagram/message_builder.rb @@ -38,6 +38,10 @@ class Messages::Instagram::MessageBuilder < Messages::Messenger::MessageBuilder @outgoing_echo ? :outgoing : :incoming end + def message_identifier + message[:mid] + end + def message_source_id @outgoing_echo ? recipient_id : sender_id end @@ -66,10 +70,6 @@ class Messages::Instagram::MessageBuilder < Messages::Messenger::MessageBuilder @messaging[:message][:text] end - def content_attributes - { message_id: @messaging[:message][:mid] } - end - def build_message return if @outgoing_echo && already_sent_from_chatwoot? @@ -103,22 +103,17 @@ class Messages::Instagram::MessageBuilder < Messages::Messenger::MessageBuilder account_id: conversation.account_id, inbox_id: conversation.inbox_id, message_type: message_type, - source_id: message_source_id, + source_id: message_identifier, content: message_content, - content_attributes: content_attributes, sender: @outgoing_echo ? nil : contact } end def already_sent_from_chatwoot? cw_message = conversation.messages.where( - source_id: nil, - message_type: 'outgoing', - content: message_content, - private: false, - status: :sent + source_id: @messaging[:message][:mid] ).first - cw_message.update(content_attributes: content_attributes) if cw_message.present? + cw_message.present? end diff --git a/app/services/instagram/send_on_instagram_service.rb b/app/services/instagram/send_on_instagram_service.rb index dde8cf3d0..4f331e58b 100644 --- a/app/services/instagram/send_on_instagram_service.rb +++ b/app/services/instagram/send_on_instagram_service.rb @@ -63,9 +63,10 @@ class Instagram::SendOnInstagramService < Base::SendOnChannelService query: query ) - Rails.logger.info("Instagram response: #{response} : #{message_content}") if response[:body] + Rails.logger.info("Instagram response: #{response['error']} : #{message_content}") if response['error'] + message.update!(source_id: response['message_id']) if response['message_id'].present? - response[:body] + response end def calculate_app_secret_proof(app_secret, access_token) diff --git a/spec/services/instagram/send_on_instagram_service_spec.rb b/spec/services/instagram/send_on_instagram_service_spec.rb index 0c5e754f1..41e081a34 100644 --- a/spec/services/instagram/send_on_instagram_service_spec.rb +++ b/spec/services/instagram/send_on_instagram_service_spec.rb @@ -21,7 +21,7 @@ describe Instagram::SendOnInstagramService do allow(Facebook::Messenger::Configuration::AppSecretProofCalculator).to receive(:call).and_return('app_secret_key', 'access_token') allow(HTTParty).to receive(:post).and_return( { - body: { recipient: { id: contact_inbox.source_id } } + 'message_id': 'anyrandommessageid1234567890' } ) end @@ -29,7 +29,8 @@ describe Instagram::SendOnInstagramService do it 'if message is sent from chatwoot and is outgoing' do message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation) response = ::Instagram::SendOnInstagramService.new(message: message).perform - expect(response).to eq({ recipient: { id: contact_inbox.source_id } }) + + expect(response).to eq({ message_id: 'anyrandommessageid1234567890' }) end it 'if message with attachment is sent from chatwoot and is outgoing' do @@ -38,7 +39,8 @@ describe Instagram::SendOnInstagramService do attachment.file.attach(io: File.open(Rails.root.join('spec/assets/avatar.png')), filename: 'avatar.png', content_type: 'image/png') message.save! response = ::Instagram::SendOnInstagramService.new(message: message).perform - expect(response).to eq({ recipient: { id: contact_inbox.source_id } }) + + expect(response).to eq({ message_id: 'anyrandommessageid1234567890' }) end end end