chore: Logger for non-existent WhatsApp channels (#11064)

- Add a warning logger for cases where we are getting webhook events for
inactive numbers.
- Add config to discard events for inactive numbers so that the meta
will stop sending events

---------

Co-authored-by: Pranav <pranav@chatwoot.com>
This commit is contained in:
Sojan Jose
2025-03-12 15:50:38 -07:00
committed by GitHub
parent d0631e99a5
commit 29158e32fe
6 changed files with 73 additions and 2 deletions

View File

@@ -29,5 +29,34 @@ RSpec.describe 'Webhooks::WhatsappController', type: :request do
post '/webhooks/whatsapp/123221321', params: { content: 'hello' }
expect(response).to have_http_status(:success)
end
context 'when phone number is in inactive list' do
before do
allow(GlobalConfig).to receive(:get_value).with('INACTIVE_WHATSAPP_NUMBERS').and_return('+1234567890,+9876543210')
end
it 'returns service unavailable for inactive phone number in URL params' do
allow(Rails.logger).to receive(:warn)
expect(Rails.logger).to receive(:warn).with('Rejected webhook for inactive WhatsApp number: +1234567890')
post '/webhooks/whatsapp/+1234567890', params: { content: 'hello' }
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to eq('Inactive WhatsApp number')
end
end
context 'when INACTIVE_WHATSAPP_NUMBERS config is not set' do
before do
allow(GlobalConfig).to receive(:get_value).with('INACTIVE_WHATSAPP_NUMBERS').and_return(nil)
end
it 'processes the webhook normally' do
allow(Webhooks::WhatsappEventsJob).to receive(:perform_later)
expect(Webhooks::WhatsappEventsJob).to receive(:perform_later)
post '/webhooks/whatsapp/+1234567890', params: { content: 'hello' }
expect(response).to have_http_status(:success)
end
end
end
end

View File

@@ -81,6 +81,22 @@ RSpec.describe Webhooks::WhatsappEventsJob do
expect(Whatsapp::IncomingMessageService).not_to receive(:new)
job.perform_now(params)
end
it 'logs a warning when channel is inactive' do
channel.prompt_reauthorization!
allow(Rails.logger).to receive(:warn)
expect(Rails.logger).to receive(:warn).with("Inactive WhatsApp channel: #{channel.phone_number}")
job.perform_now(params)
end
it 'logs a warning with unknown phone number when channel does not exist' do
unknown_phone = '+1234567890'
allow(Rails.logger).to receive(:warn)
expect(Rails.logger).to receive(:warn).with("Inactive WhatsApp channel: unknown - #{unknown_phone}")
job.perform_now(phone_number: unknown_phone)
end
end
context 'when default provider' do