lib/telegram/bot/routes_helper.rb in telegram-bot-0.14.4 vs lib/telegram/bot/routes_helper.rb in telegram-bot-0.15.0
- old
+ new
@@ -1,5 +1,8 @@
+require 'base64'
+require 'openssl'
+
require 'telegram/bot'
require 'active_support/core_ext/array/wrap'
module Telegram
module Bot
@@ -15,14 +18,12 @@
name = bots.invert[bot]
name && "#{name}_telegram_webhook"
end || 'telegram_webhook'
end
- # Replaces colon with underscore so rails don't treat it as
- # route parameter.
- def escape_token(token)
- token && token.tr(':', '_')
+ def token_hash(token)
+ Base64.urlsafe_encode64(OpenSSL::Digest::SHA1.digest(token), padding: false)
end
end
# Define route which processes requests using given controller and bot.
#
@@ -32,17 +33,20 @@
# # same as:
# telegram_webhook TelegramController, :default
#
# # pass additional options
# telegram_webhook TelegramController, :default, as: :custom_route_name
- def telegram_webhook(controller, bot = :default, **options)
+ #
+ # # Default path is generated using hashed bot token. Override it using:
+ # telegram_webhook TelegramController, :default, path: 'top/secret'
+ def telegram_webhook(controller, bot = :default, path: nil, **options)
bot = Client.wrap(bot)
params = {
to: Middleware.new(bot, controller),
as: RoutesHelper.route_name_for_bot(bot),
format: false,
}.merge!(options)
- post("telegram/#{RoutesHelper.escape_token bot.token}", params)
+ post(path || "telegram/#{bot.token && RoutesHelper.token_hash(bot.token)}", params)
UpdatesPoller.add(bot, controller) if Telegram.bot_poller_mode?
end
end
end
end