Bug: Instagram render template message twice (#3313)

This commit is contained in:
Tejaswini Chile 2021-11-10 18:32:46 +05:30 committed by GitHub
parent 358171062e
commit c0f5a59cf8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 17 deletions

View file

@ -38,6 +38,10 @@ class Messages::Instagram::MessageBuilder < Messages::Messenger::MessageBuilder
@outgoing_echo ? :outgoing : :incoming @outgoing_echo ? :outgoing : :incoming
end end
def message_identifier
message[:mid]
end
def message_source_id def message_source_id
@outgoing_echo ? recipient_id : sender_id @outgoing_echo ? recipient_id : sender_id
end end
@ -66,10 +70,6 @@ class Messages::Instagram::MessageBuilder < Messages::Messenger::MessageBuilder
@messaging[:message][:text] @messaging[:message][:text]
end end
def content_attributes
{ message_id: @messaging[:message][:mid] }
end
def build_message def build_message
return if @outgoing_echo && already_sent_from_chatwoot? return if @outgoing_echo && already_sent_from_chatwoot?
@ -103,22 +103,17 @@ class Messages::Instagram::MessageBuilder < Messages::Messenger::MessageBuilder
account_id: conversation.account_id, account_id: conversation.account_id,
inbox_id: conversation.inbox_id, inbox_id: conversation.inbox_id,
message_type: message_type, message_type: message_type,
source_id: message_source_id, source_id: message_identifier,
content: message_content, content: message_content,
content_attributes: content_attributes,
sender: @outgoing_echo ? nil : contact sender: @outgoing_echo ? nil : contact
} }
end end
def already_sent_from_chatwoot? def already_sent_from_chatwoot?
cw_message = conversation.messages.where( cw_message = conversation.messages.where(
source_id: nil, source_id: @messaging[:message][:mid]
message_type: 'outgoing',
content: message_content,
private: false,
status: :sent
).first ).first
cw_message.update(content_attributes: content_attributes) if cw_message.present?
cw_message.present? cw_message.present?
end end

View file

@ -63,9 +63,10 @@ class Instagram::SendOnInstagramService < Base::SendOnChannelService
query: query 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 end
def calculate_app_secret_proof(app_secret, access_token) def calculate_app_secret_proof(app_secret, access_token)

View file

@ -21,7 +21,7 @@ describe Instagram::SendOnInstagramService do
allow(Facebook::Messenger::Configuration::AppSecretProofCalculator).to receive(:call).and_return('app_secret_key', 'access_token') allow(Facebook::Messenger::Configuration::AppSecretProofCalculator).to receive(:call).and_return('app_secret_key', 'access_token')
allow(HTTParty).to receive(:post).and_return( allow(HTTParty).to receive(:post).and_return(
{ {
body: { recipient: { id: contact_inbox.source_id } } 'message_id': 'anyrandommessageid1234567890'
} }
) )
end end
@ -29,7 +29,8 @@ describe Instagram::SendOnInstagramService do
it 'if message is sent from chatwoot and is outgoing' 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) message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
response = ::Instagram::SendOnInstagramService.new(message: message).perform 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
it 'if message with attachment is sent from chatwoot and is outgoing' do 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') attachment.file.attach(io: File.open(Rails.root.join('spec/assets/avatar.png')), filename: 'avatar.png', content_type: 'image/png')
message.save! message.save!
response = ::Instagram::SendOnInstagramService.new(message: message).perform 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 end
end end