зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-07 11:25:52 +03:00
Ability to use type-castion with telegram-bot-types
Этот коммит содержится в:
46
lib/telegram/bot/config_methods.rb
Обычный файл
46
lib/telegram/bot/config_methods.rb
Обычный файл
@@ -0,0 +1,46 @@
|
||||
require 'active_support/core_ext/hash/keys'
|
||||
require 'active_support/core_ext/hash/transform_values'
|
||||
|
||||
module Telegram
|
||||
module Bot
|
||||
module ConfigMethods
|
||||
# Overwrite config.
|
||||
attr_writer :bots_config
|
||||
|
||||
# Keep this setting here, so we can avoid loading Bot::UpdatesPoller
|
||||
# when polling is disabled.
|
||||
attr_writer :bot_poller_mode
|
||||
|
||||
# It just tells routes helpers whether to add routed bots to
|
||||
# Bot::UpdatesPoller, so their config will be available by bot key in
|
||||
# Bot::UpdatesPoller.start.
|
||||
def bot_poller_mode?
|
||||
return @bot_poller_mode if defined?(@bot_poller_mode)
|
||||
Rails.env.development? if defined?(Rails)
|
||||
end
|
||||
|
||||
# Hash of bots made with bots_config.
|
||||
def bots
|
||||
@bots ||= bots_config.transform_values(&Client.method(:wrap))
|
||||
end
|
||||
|
||||
# Default bot.
|
||||
def bot
|
||||
@bot ||= bots[:default]
|
||||
end
|
||||
|
||||
# Returns config for .bots method. By default uses `telegram['bots']` section
|
||||
# from `secrets.yml` merging `telegram['bot']` at `:default` key.
|
||||
#
|
||||
# Can be overwritten with .bots_config=
|
||||
def bots_config
|
||||
return @bots_config if @bots_config
|
||||
telegram_config = Rails.application.secrets[:telegram]
|
||||
(telegram_config['bots'] || {}).symbolize_keys.tap do |config|
|
||||
default = telegram_config['bot']
|
||||
config[:default] = default if default
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Ссылка в новой задаче
Block a user