Module: TelegramBotEngine::WebhookRegistrar

Defined in:
lib/telegram_bot_engine/webhook_registrar.rb

Overview

Registers/removes a bot’s Telegram webhook (docs/0001 §3.6). The webhook URL embeds the bot’s per-bot ‘webhook_secret` path segment, and `secret_token` is set to the same secret so the inbound dispatcher can authenticate Telegram via the X-Telegram-Bot-Api-Secret-Token header. setWebhook is idempotent (overwrites), so re-registering is always safe.

Class Method Summary collapse

Class Method Details

.register(bot, base_url: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/telegram_bot_engine/webhook_registrar.rb', line 10

def register(bot, base_url: nil)
  base_url ||= TelegramBotEngine.config.webhook_base_url
  return false if base_url.blank?

  TelegramBotEngine.client_for(bot).set_webhook(
    url: webhook_url(bot, base_url),
    secret_token: bot.webhook_secret
  )
  true
end

.remove(bot) ⇒ Object



21
22
23
24
# File 'lib/telegram_bot_engine/webhook_registrar.rb', line 21

def remove(bot)
  TelegramBotEngine.client_for(bot).delete_webhook
  true
end

.webhook_url(bot, base_url) ⇒ Object



26
27
28
29
# File 'lib/telegram_bot_engine/webhook_registrar.rb', line 26

def webhook_url(bot, base_url)
  # The URL path carries the NON-secret webhook_id; the secret stays in secret_token only.
  "#{base_url.to_s.chomp('/')}#{TelegramBotEngine.config.webhook_mount_path}/#{bot.webhook_id}"
end