From e2ccac78d27691e8c3ef2a69aea54e764ee6156a Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Thu, 19 Jan 2023 18:52:38 +0530 Subject: [PATCH] fix: Error when unsupported Whatsapp message status (#6295) fixes error when unsupported WhatsApp message status --- .../whatsapp/incoming_message_base_service.rb | 19 +++++-------------- .../incoming_message_service_helpers.rb | 14 ++++++++++++++ .../whatsapp/incoming_message_service_spec.rb | 11 +++++++++++ 3 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 app/services/whatsapp/incoming_message_service_helpers.rb diff --git a/app/services/whatsapp/incoming_message_base_service.rb b/app/services/whatsapp/incoming_message_base_service.rb index 83fceb452..5dc495412 100644 --- a/app/services/whatsapp/incoming_message_base_service.rb +++ b/app/services/whatsapp/incoming_message_base_service.rb @@ -2,6 +2,8 @@ # https://docs.360dialog.com/whatsapp-api/whatsapp-api/media # https://developers.facebook.com/docs/whatsapp/api/media/ class Whatsapp::IncomingMessageBaseService + include ::Whatsapp::IncomingMessageServiceHelpers + pattr_initialize [:inbox!, :params!] def perform @@ -37,6 +39,8 @@ class Whatsapp::IncomingMessageBaseService return unless find_message_by_source_id(@processed_params[:statuses].first[:id]) update_message_with_status(@message, @processed_params[:statuses].first) + rescue ArgumentError => e + Rails.logger.error "Error while processing whatsapp status update #{e.message}" end def update_message_with_status(message, status) @@ -49,7 +53,7 @@ class Whatsapp::IncomingMessageBaseService end def create_messages - return if unprocessable_message_type? + return if unprocessable_message_type?(message_type) @message = @conversation.messages.build( content: message_content(@processed_params[:messages].first), @@ -108,23 +112,10 @@ class Whatsapp::IncomingMessageBaseService @conversation = ::Conversation.create!(conversation_params) end - def file_content_type(file_type) - return :image if %w[image sticker].include?(file_type) - return :audio if %w[audio voice].include?(file_type) - return :video if ['video'].include?(file_type) - return :location if ['location'].include?(file_type) - - :file - end - def message_type @processed_params[:messages].first[:type] end - def unprocessable_message_type? - %w[reaction contacts ephemeral unsupported].include?(message_type) - end - def attach_files return if %w[text button interactive location].include?(message_type) diff --git a/app/services/whatsapp/incoming_message_service_helpers.rb b/app/services/whatsapp/incoming_message_service_helpers.rb new file mode 100644 index 000000000..263e21c11 --- /dev/null +++ b/app/services/whatsapp/incoming_message_service_helpers.rb @@ -0,0 +1,14 @@ +module Whatsapp::IncomingMessageServiceHelpers + def file_content_type(file_type) + return :image if %w[image sticker].include?(file_type) + return :audio if %w[audio voice].include?(file_type) + return :video if ['video'].include?(file_type) + return :location if ['location'].include?(file_type) + + :file + end + + def unprocessable_message_type?(message_type) + %w[reaction contacts ephemeral unsupported].include?(message_type) + end +end diff --git a/spec/services/whatsapp/incoming_message_service_spec.rb b/spec/services/whatsapp/incoming_message_service_spec.rb index 3721d48cd..a6734f32d 100644 --- a/spec/services/whatsapp/incoming_message_service_spec.rb +++ b/spec/services/whatsapp/incoming_message_service_spec.rb @@ -113,6 +113,17 @@ describe Whatsapp::IncomingMessageService do expect(message.reload.status).to eq('failed') expect(message.external_error).to eq('123: abc') end + + it 'will not throw error if unsupported status' do + status_params = { + 'statuses' => [{ 'recipient_id' => from, 'id' => from, 'status' => 'deleted', + 'errors' => [{ 'code': 123, 'title': 'abc' }] }] + }.with_indifferent_access + + message = Message.find_by!(source_id: from) + expect(message.status).to eq('sent') + expect { described_class.new(inbox: whatsapp_channel.inbox, params: status_params).perform }.not_to raise_error + end end context 'when valid interactive message params' do