Files
leadchat/app/jobs/webhooks/telegram_events_job.rb
ruslan b87b7972c1 feat(channel): add support for Telegram Business bots (#10181) (#11663)
Added support for Telegram Business bots. Telegram webhooks from such bots include the business_message field, which we transform into a standard message for Chatwoot. This PR also modifies how we handle replies, attachments, and image uploads when working with Telegram Business bots.

demo: https://drive.google.com/file/d/1Yz82wXBVRtb-mxjXogkUju4hlJbt3qyh/view?usp=sharing&t=4

Fixes #10181
2025-06-16 20:35:23 -07:00

45 lines
1.3 KiB
Ruby

class Webhooks::TelegramEventsJob < ApplicationJob
queue_as :default
def perform(params = {})
return unless params[:bot_token]
channel = Channel::Telegram.find_by(bot_token: params[:bot_token])
if channel_is_inactive?(channel)
log_inactive_channel(channel, params)
return
end
process_event_params(channel, params)
end
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)
return unless params[:telegram]
if params.dig(:telegram, :edited_message).present? || params.dig(:telegram, :edited_business_message).present?
Telegram::UpdateMessageService.new(inbox: channel.inbox, params: params['telegram'].with_indifferent_access).perform
else
Telegram::IncomingMessageService.new(inbox: channel.inbox, params: params['telegram'].with_indifferent_access).perform
end
end
end