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
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

View file

@ -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)

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(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