Skip to content

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

@ar2em1s

Description

@ar2em1s

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions