chore: Add warning logs when Chatwoot receives events for inactive channels (#11066)
This commit is contained in:
@@ -5,13 +5,33 @@ class Webhooks::TelegramEventsJob < ApplicationJob
|
|||||||
return unless params[:bot_token]
|
return unless params[:bot_token]
|
||||||
|
|
||||||
channel = Channel::Telegram.find_by(bot_token: params[:bot_token])
|
channel = Channel::Telegram.find_by(bot_token: params[:bot_token])
|
||||||
return unless channel
|
|
||||||
|
if channel_is_inactive?(channel)
|
||||||
|
log_inactive_channel(channel, params)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
process_event_params(channel, params)
|
process_event_params(channel, params)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def channel_is_inactive?(channel)
|
||||||
|
return true if channel.blank?
|
||||||
|
return true unless channel.account.active?
|
||||||
|
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def log_inactive_channel(channel, params)
|
||||||
|
message = if channel&.id
|
||||||
|
"Account #{channel.account.id} is not active for channel #{channel.id}"
|
||||||
|
else
|
||||||
|
"Channel not found for bot_token: #{params[:bot_token]}"
|
||||||
|
end
|
||||||
|
Rails.logger.warn("Telegram event discarded: #{message}")
|
||||||
|
end
|
||||||
|
|
||||||
def process_event_params(channel, params)
|
def process_event_params(channel, params)
|
||||||
return unless params[:telegram]
|
return unless params[:telegram]
|
||||||
|
|
||||||
|
|||||||
@@ -17,8 +17,9 @@ RSpec.describe Webhooks::TelegramEventsJob do
|
|||||||
expect(described_class.perform_now({})).to be_nil
|
expect(described_class.perform_now({})).to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns nil when invalid bot_token' do
|
it 'logs a warning when channel is not found' do
|
||||||
expect(described_class.perform_now({ bot_token: 'invalid' })).to be_nil
|
expect(Rails.logger).to receive(:warn).with('Telegram event discarded: Channel not found for bot_token: invalid')
|
||||||
|
described_class.perform_now({ bot_token: 'invalid' })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -32,6 +33,19 @@ RSpec.describe Webhooks::TelegramEventsJob do
|
|||||||
expect(process_service).to receive(:perform)
|
expect(process_service).to receive(:perform)
|
||||||
described_class.perform_now(params.with_indifferent_access)
|
described_class.perform_now(params.with_indifferent_access)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'logs a warning and does not process events if account is suspended' do
|
||||||
|
account = telegram_channel.account
|
||||||
|
account.update!(status: :suspended)
|
||||||
|
|
||||||
|
process_service = double
|
||||||
|
allow(Telegram::IncomingMessageService).to receive(:new).and_return(process_service)
|
||||||
|
allow(process_service).to receive(:perform)
|
||||||
|
|
||||||
|
expect(Rails.logger).to receive(:warn).with("Telegram event discarded: Account #{account.id} is not active for channel #{telegram_channel.id}")
|
||||||
|
expect(Telegram::IncomingMessageService).not_to receive(:new)
|
||||||
|
described_class.perform_now(params.with_indifferent_access)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when update message params' do
|
context 'when update message params' do
|
||||||
|
|||||||
Reference in New Issue
Block a user