Module: Telegram::Bot::RoutesHelper
- Defined in:
- lib/telegram/bot/routes_helper.rb
Class Method Summary collapse
-
.escape_token(token) ⇒ Object
Replaces colon with underscore so rails don’t treat it as route parameter.
-
.route_name_for_bot(bot) ⇒ Object
Returns route name for given bot.
Instance Method Summary collapse
-
#telegram_webhook(controller, bot = :default, **options) ⇒ Object
Define route which processes requests using given controller and bot.
-
#telegram_webhooks(controllers, bots = nil, **options) ⇒ Object
# Create routes for all Telegram.bots to use same controller: telegram_webhooks TelegramController.
Class Method Details
.escape_token(token) ⇒ Object
Replaces colon with underscore so rails don’t treat it as route parameter.
22 23 24 |
# File 'lib/telegram/bot/routes_helper.rb', line 22 def escape_token(token) token && token.tr(':', '_') end |
.route_name_for_bot(bot) ⇒ Object
Returns route name for given bot. Result depends on ‘Telegram.bots`. When there is single bot it returns ’telegram_webhook’. When there are it will use bot’s key in the ‘Telegram.bots` as prefix (eg. `chat_telegram_webhook`).
12 13 14 15 16 17 18 |
# File 'lib/telegram/bot/routes_helper.rb', line 12 def route_name_for_bot(bot) bots = Telegram.bots if bots.size != 1 name = bots.invert[bot] name && "#{name}_telegram_webhook" end || 'telegram_webhook' end |
Instance Method Details
#telegram_webhook(controller, bot = :default, **options) ⇒ Object
Define route which processes requests using given controller and bot.
telegram_webhook TelegramController, bot
telegram_webhook TelegramController
# same as:
telegram_webhook TelegramController, :default
# pass additional options
telegram_webhook TelegramController, :default, as: :custom_route_name
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/telegram/bot/routes_helper.rb', line 64 def telegram_webhook(controller, bot = :default, **) bot = Client.wrap(bot) params = { to: Middleware.new(bot, controller), as: RoutesHelper.route_name_for_bot(bot), format: false, }.merge!() post("telegram/#{RoutesHelper.escape_token bot.token}", params) UpdatesPoller.add(bot, controller) if Telegram.bot_poller_mode? end |
#telegram_webhooks(controllers, bots = nil, **options) ⇒ Object
# Create routes for all Telegram.bots to use same controller:
telegram_webhooks TelegramController
# Or pass custom bots usin any of supported config options:
telegram_webhooks TelegramController, [
bot,
{token: token, username: username},
other_bot_token,
]
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/telegram/bot/routes_helper.rb', line 36 def telegram_webhooks(controllers, bots = nil, **) Bot.deprecation_0_14.deprecation_warning(:telegram_webhooks, " It brings unnecessary complexity and encourages writeng less readable code.\n Please use telegram_webhook method instead.\n It's signature `telegram_webhook(controller, bot = :default, **options)`.\n Multiple-bot environments now requires calling this method in a loop\n or using statement for each bot.\n TXT\n unless controllers.is_a?(Hash)\n bots = bots ? Array.wrap(bots) : Telegram.bots.values\n controllers = Hash[bots.map { |x| [x, controllers] }]\n end\n controllers.each do |bot, controller|\n controller, bot_options = controller if controller.is_a?(Array)\n telegram_webhook(controller, bot, options.merge(bot_options || {}))\n end\nend\n".strip_heredoc) |