telegram-bot icon indicating copy to clipboard operation
telegram-bot copied to clipboard

Code examples in "Multiple controllers for single bot" wiki page contain a mistake

Open ar2em1s opened this issue 7 months ago • 0 comments

Thank you for a great gem first of all!

In the docs custom router/update dispatching code examples contain such dispatch method definition:

def dispatch(bot, update)

But when webhook is used for updates processing actually 3 parameters (bot, update, webhook_request) are passed to the dispatch method which causes ArgumentError in production with custom dispatching. In the gem code Telegram::Bot::UpdatesController.dispatch -> Telegram::Bot::UpdatesController#initialize define 3 parameters and handle update correctly.

Please, update code examples with following code:

class TelegramWebhooksController < Telegram::Bot::UpdatesController
  require_dependency 'telegram_webhooks_controller/group_chat_controller'

  class << self
    # Use GroupChatController for group chats.
    def dispatch(bot, update, _webhook_request = nil)
      message = update['message'] || update['channel_post'] ||
        update.dig('callback_query', 'message')
      chat_type = message && message.dig('chat', 'type')
      if chat_type && chat_type != 'private'
        GroupChatController.dispatch(bot, update)
      else
        super
      end
    end
  end

  def start!(*)
    respond_with :message, text: 'Hi user!'
  end
end

# app/controllers/telegram_webhooks_controller/group_chat_controller.rb
class TelegramWebhooksController
  class GroupChatController < Telegram::Bot::UpdatesController
    def start!(*)
      respond_with :message, text: 'Hi group!'
    end
  end
end
module TelegramWebhooksRouter
  module_function

  def dispatch(bot, update, _webhook_request = nil)
    if something
      TelegramGroupChatController.dispatch(bot, update)
    else
      TelegramPrivateChatController.dispatch(bot, update)
    end
  end
end

# routes.rb
Rails.application.routes.draw do
  telegram_webhooks TelegramWebhooksRouter
end

ar2em1s avatar May 24 '25 21:05 ar2em1s