Module: Telegram::Bot::ConfigMethods
- Included in:
- Telegram
- Defined in:
- lib/telegram/bot/config_methods.rb
Instance Attribute Summary collapse
-
#bot_poller_mode ⇒ Object
writeonly
Keep this setting here, so we can avoid loading Bot::UpdatesPoller when polling is disabled.
-
#bots_config ⇒ Object
Returns config for .bots method.
Instance Method Summary collapse
-
#bot ⇒ Object
Default bot.
-
#bot_poller_mode? ⇒ Boolean
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.
-
#bots ⇒ Object
Hash of bots made with bots_config.
-
#reset_bots ⇒ Object
Resets all cached bots and their configs.
Instance Attribute Details
#bot_poller_mode=(value) ⇒ Object (writeonly)
Keep this setting here, so we can avoid loading Bot::UpdatesPoller when polling is disabled.
14 15 16 |
# File 'lib/telegram/bot/config_methods.rb', line 14 def bot_poller_mode=(value) @bot_poller_mode = value end |
#bots_config ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/telegram/bot/config_methods.rb', line 49 def bots_config # rubocop:disable Metrics/PerceivedComplexity @bots_config ||= if defined?(Rails.application) app = Rails.application store = app.credentials[:telegram] if app.respond_to?(:credentials) store ||= app.secrets[:telegram] if app.respond_to?(:secrets) store ||= {} store = store.with_indifferent_access store.fetch(:bots, {}).symbolize_keys.tap do |config| default = store[:bot] config[:default] = default if default end else {} end end |
Instance Method Details
#bot ⇒ Object
Default bot.
37 38 39 40 41 42 43 |
# File 'lib/telegram/bot/config_methods.rb', line 37 def bot @bot ||= bots.fetch(:default) do raise 'Default bot is not configured.' \ ' Add :default to bots_config' \ ' or use telegram.bot/telegram.bots.default section in secrets.yml.' end end |
#bot_poller_mode? ⇒ Boolean
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.
It’s enabled by default in Rails dev environment and ‘rake telegram:bot:poller` task. Use `BOT_POLLER_MODE=true` envvar to set it manually.
22 23 24 25 26 27 |
# File 'lib/telegram/bot/config_methods.rb', line 22 def bot_poller_mode? return @bot_poller_mode if defined?(@bot_poller_mode) @bot_poller_mode = ENV.fetch('BOT_POLLER_MODE') do Rails.env.development? if defined?(Rails.env) end end |
#bots ⇒ Object
Hash of bots made with bots_config.
30 31 32 33 34 |
# File 'lib/telegram/bot/config_methods.rb', line 30 def bots @bots ||= bots_config.each_with_object({}) do |(id, config), h| h[id] = Client.wrap(config, id: id) end end |
#reset_bots ⇒ Object
Resets all cached bots and their configs.
67 68 69 70 71 |
# File 'lib/telegram/bot/config_methods.rb', line 67 def reset_bots @bots = nil @bot = nil @bots_config = nil end |