Module: Telegram::Bot::ConfigMethods

Included in:
Telegram
Defined in:
lib/telegram/bot/config_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bot_poller_mode=(value) ⇒ Object (writeonly)

Keep this setting here, so we can avoid loading Bot::UpdatesPoller when polling is disabled.



12
13
14
# File 'lib/telegram/bot/config_methods.rb', line 12

def bot_poller_mode=(value)
  @bot_poller_mode = value
end

#bots_configObject

Returns config for .bots method. By default uses ‘telegram` section from secrets.yml merging `telegram` at :default key.

Can be overwritten with .bots_config=



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/telegram/bot/config_methods.rb', line 48

def bots_config
  @bots_config ||=
    if defined?(Rails.application)
      telegram_config = Rails.application.secrets[:telegram] || {}
      (telegram_config['bots'] || {}).symbolize_keys.tap do |config|
        default = telegram_config['bot']
        config[:default] = default if default
      end
    else
      {}
    end
end

Instance Method Details

#botObject

Default bot.



35
36
37
# File 'lib/telegram/bot/config_methods.rb', line 35

def bot
  @bot ||= bots[:default]
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.

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/telegram/bot/config_methods.rb', line 20

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

#botansObject

Hash of botan clients made from #bots.



40
41
42
# File 'lib/telegram/bot/config_methods.rb', line 40

def botans
  @botans ||= bots.transform_values(&:botan)
end

#botsObject

Hash of bots made with bots_config.



28
29
30
31
32
# File 'lib/telegram/bot/config_methods.rb', line 28

def bots
  @bots ||= bots_config.each_with_object({}) do |(id, config), h|
    h[id] = Client.wrap(config, id: id)
  end
end

#reset_botsObject

Resets all cached bots and their configs.



62
63
64
65
66
67
# File 'lib/telegram/bot/config_methods.rb', line 62

def reset_bots
  @bots = nil
  @bot = nil
  @bots_config = nil
  @botans = nil
end