Skip to content

Commit

Permalink
Merge pull request #1805 from tactilenews/1804_fix_duplicate_message_…
Browse files Browse the repository at this point in the history
…whats_app

Fix duplicate message sent bug when quote replying
  • Loading branch information
mattwr18 authored Mar 11, 2024
2 parents 271a227 + 0b31b7d commit a1a5d28
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/adapters/whats_app_adapter/twilio_inbound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ def request_to_receive_message?(contributor, whats_app_message)
text = whats_app_message[:body]
return false if request_for_more_info?(text) || unsubscribe_text?(text) || resubscribe_text?(text)

contributor.whats_app_message_template_sent_at.present? || whats_app_message[:original_replied_message_sid]
contributor.whats_app_message_template_sent_at.present? ||
(whats_app_message[:original_replied_message_sid] &&
whats_app_message[:button_text].eql?(I18n.t('adapter.whats_app.quick_reply_button_text.answer')))
end

def quick_reply_response?(text)
Expand Down
81 changes: 81 additions & 0 deletions spec/adapters/whats_app_adapter/twilio_inbound_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

# frozen_string_literal: true, frozen

require 'rails_helper'

RSpec.describe WhatsAppAdapter::TwilioInbound do
let(:adapter) { described_class.new }
let(:whats_app_phone_number) { '+491511234567' }
let(:twilio_message_sid) { 'someValidMessageSid' }
let(:original_message_sid) { 'someUniqueSid' }
let!(:contributor) { create(:contributor, whats_app_phone_number: whats_app_phone_number) }
let(:whats_app_message) do
{
'AccountSid' => 'someAccount',
'ApiVersion' => '2010-04-01',
'Body' => 'Antworten',
'ButtonPayload' => 'Antworten-Payload',
'ButtonText' => 'Antworten',
'From' => "whatsapp:#{whats_app_phone_number}",
'MessageSid' => twilio_message_sid,
'NumMedia' => '0',
'NumSegments' => '1',
'OriginalRepliedMessageSender' => "whatsapp:#{Setting.whats_app_server_phone_number}",
'OriginalRepliedMessageSid' => original_message_sid,
'ProfileName' => 'Matthew Rider',
'ReferralNumMedia' => '0',
'SmsMessageSid' => twilio_message_sid,
'SmsSid' => twilio_message_sid,
'SmsStatus' => 'received',
'To' => "whatsapp:#{Setting.whats_app_server_phone_number}",
'WaId' => '491511234567'
}.transform_keys(&:underscore)
end
let(:quote_response) do
{
'AccountSid' => 'someAccount',
'ApiVersion' => '2010-04-01',
'Body' => 'This is simply a quote reply',
'From' => "whatsapp:#{whats_app_phone_number}",
'MessageSid' => twilio_message_sid,
'NumMedia' => '0',
'NumSegments' => '1',
'OriginalRepliedMessageSender' => "whatsapp:#{Setting.whats_app_server_phone_number}",
'OriginalRepliedMessageSid' => original_message_sid,
'ProfileName' => 'Matthew Rider',
'ReferralNumMedia' => '0',
'SmsMessageSid' => twilio_message_sid,
'SmsSid' => twilio_message_sid,
'SmsStatus' => 'received',
'To' => "whatsapp:#{Setting.whats_app_server_phone_number}",
'WaId' => '491511234567'
}.transform_keys(&:underscore)
end

describe '#on' do
describe 'REQUEST_TO_RECEIVE_MESSAGE' do
let(:request_to_receive_message_callback) { spy('request_to_receive_message_callback') }

before do
adapter.on(WhatsAppAdapter::TwilioInbound::REQUEST_TO_RECEIVE_MESSAGE) do |sender, original_replied_message_sid|
request_to_receive_message_callback.call(sender, original_replied_message_sid)
end
end

subject do
adapter.consume(whats_app_message)
request_to_receive_message_callback
end

context 'given a quick reply message is received' do
it { is_expected.to have_received(:call).with(contributor, original_message_sid) }
end

context 'given a reply to a message that is not a quick reply is received' do
let(:whats_app_message) { quote_response }
it { is_expected.not_to have_received(:call).with(contributor, original_message_sid) }
end
end
end
end
1 change: 1 addition & 0 deletions spec/requests/whats_app/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
before do
subject.call
params['OriginalRepliedMessageSid'] = 'someUniqueId'
params['ButtonText'] = 'Antworten'
allow(Twilio::REST::Client).to receive(:new).and_return(mock_twilio_rest_client)
allow(mock_twilio_rest_client).to receive(:messages).with('someUniqueId').and_return(messages_double)
allow(messages_double).to receive(:fetch).and_return(messages_double)
Expand Down

0 comments on commit a1a5d28

Please sign in to comment.