fix: Error when unsupported Whatsapp message status (#6295)

fixes error when unsupported WhatsApp message status
This commit is contained in:
Sojan Jose
2023-01-19 18:52:38 +05:30
committed by GitHub
parent 1193cf1847
commit e2ccac78d2
3 changed files with 30 additions and 14 deletions

View File

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

View File

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

View File

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