Class: TelegramBotEngine::DeliveryJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/telegram_bot_engine/delivery_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(bot_id, chat_id, text, options = {}) ⇒ Object

Delivers through the chosen bot’s own client (resolved via the registry), not the process-global Telegram.bot (docs/0001 §3.3). ‘bot_id` is resolved to a Bot record; a missing/blank id falls back to the default bot so an in-flight job enqueued before a rollback still delivers.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/jobs/telegram_bot_engine/delivery_job.rb', line 12

def perform(bot_id, chat_id, text, options = {})
  bot = TelegramBotEngine::Bot.find_by(id: bot_id) || TelegramBotEngine::Bot.default

  TelegramBotEngine.client_for(bot).send_message(
    chat_id: chat_id,
    text: text,
    **options.symbolize_keys
  )

  TelegramBotEngine::Event.log(
    event_type: "delivery", action: "delivered",
    chat_id: chat_id, bot_id: bot.id,
    details: { bot: bot.slug, text_preview: text.to_s[0, 100] }
  )
rescue Telegram::Bot::Forbidden
  # User blocked this bot - deactivate only *this bot's* subscription for the chat,
  # not the chat's subscriptions to other bots (docs/0001 §3.4).
  TelegramBotEngine::Subscription.for_bot(bot).where(chat_id: chat_id).update_all(active: false)
  Rails.logger.info("[TelegramBotEngine] Deactivated subscription for blocked chat: #{chat_id}")

  TelegramBotEngine::Event.log(
    event_type: "delivery", action: "blocked",
    chat_id: chat_id, bot_id: bot&.id,
    details: { bot: bot&.slug, text_preview: text.to_s[0, 100] }
  )
end