Class: TelegramBotEngine::Dispatch

Inherits:
Object
  • Object
show all
Defined in:
lib/telegram_bot_engine/dispatch.rb

Overview

The single inbound webhook endpoint for ALL bots (docs/0001 §3.7). The host app mounts this once: ‘mount TelegramBotEngine::Dispatch, at: config.webhook_mount_path`. For `POST <mount>/:webhook_secret` it resolves the Bot by its secret path segment, validates the X-Telegram-Bot-Api-Secret-Token header (telegram-bot 0.16 does NOT — docs/0001 §9), then hands off to the host’s UpdatesController via ‘dispatch(bot.client, update, request)`.

Constant Summary collapse

SECRET_TOKEN_HEADER =
"HTTP_X_TELEGRAM_BOT_API_SECRET_TOKEN"
ENV_BOT_KEY =

The resolved Bot record is exposed to the controller here so SubscriberCommands can scope inbound /start·/stop to the bot the update arrived for.

"telegram_bot_engine.bot"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(env) ⇒ Object



18
19
20
# File 'lib/telegram_bot_engine/dispatch.rb', line 18

def self.call(env)
  new.call(env)
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/telegram_bot_engine/dispatch.rb', line 22

def call(env)
  request = ActionDispatch::Request.new(env)
  return respond(405, "method not allowed") unless request.post?

  bot = resolve_bot(request)
  return respond(404, "unknown bot") unless bot
  return respond(403, "invalid secret token") unless valid_secret_token?(request, bot)

  controller = dispatch_controller
  return respond(503, "dispatch_controller not configured") unless controller

  begin
    update = request.request_parameters
  rescue ActionDispatch::Http::Parameters::ParseError
    return respond(400, "bad request")
  end

  request.set_header(ENV_BOT_KEY, bot)
  controller.dispatch(bot.client, update, request)
  respond(200, "")
end