Class: Telegram::Bot::UpdatesPoller
- Inherits:
-
Object
- Object
- Telegram::Bot::UpdatesPoller
- Defined in:
- lib/telegram/bot/updates_poller.rb
Overview
Supposed to be used in development environments only.
Constant Summary collapse
- DEFAULT_TIMEOUT =
5
- @@instances =
rubocop:disable ClassVars
{}
Instance Attribute Summary collapse
-
#bot ⇒ Object
readonly
Returns the value of attribute bot.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#reload ⇒ Object
readonly
Returns the value of attribute reload.
-
#running ⇒ Object
readonly
Returns the value of attribute running.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Class Method Summary collapse
-
.add(bot, controller) ⇒ Object
Create, start and add poller instnace to tracked instances list.
- .instances ⇒ Object
- .start(bot_id, controller = nil) ⇒ Object
Instance Method Summary collapse
- #fetch_updates ⇒ Object
-
#initialize(bot, controller, **options) ⇒ UpdatesPoller
constructor
A new instance of UpdatesPoller.
- #log(&block) ⇒ Object
- #reload! ⇒ Object
- #reloading_code ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(bot, controller, **options) ⇒ UpdatesPoller
Returns a new instance of UpdatesPoller.
29 30 31 32 33 34 35 36 |
# File 'lib/telegram/bot/updates_poller.rb', line 29 def initialize(bot, controller, **) @logger = .fetch(:logger) { defined?(Rails.logger) && Rails.logger } @bot = bot @controller = controller @timeout = .fetch(:timeout) { DEFAULT_TIMEOUT } @offset = [:offset] @reload = .fetch(:reload) { defined?(Rails.env) && Rails.env.development? } end |
Instance Attribute Details
#bot ⇒ Object (readonly)
Returns the value of attribute bot.
27 28 29 |
# File 'lib/telegram/bot/updates_poller.rb', line 27 def bot @bot end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
27 28 29 |
# File 'lib/telegram/bot/updates_poller.rb', line 27 def controller @controller end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
27 28 29 |
# File 'lib/telegram/bot/updates_poller.rb', line 27 def logger @logger end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
27 28 29 |
# File 'lib/telegram/bot/updates_poller.rb', line 27 def offset @offset end |
#reload ⇒ Object (readonly)
Returns the value of attribute reload.
27 28 29 |
# File 'lib/telegram/bot/updates_poller.rb', line 27 def reload @reload end |
#running ⇒ Object (readonly)
Returns the value of attribute running.
27 28 29 |
# File 'lib/telegram/bot/updates_poller.rb', line 27 def running @running end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
27 28 29 |
# File 'lib/telegram/bot/updates_poller.rb', line 27 def timeout @timeout end |
Class Method Details
.add(bot, controller) ⇒ Object
Create, start and add poller instnace to tracked instances list.
13 14 15 |
# File 'lib/telegram/bot/updates_poller.rb', line 13 def add(bot, controller) new(bot, controller).tap { |x| instances[bot] = x } end |
.instances ⇒ Object
8 9 10 |
# File 'lib/telegram/bot/updates_poller.rb', line 8 def instances @@instances end |
.start(bot_id, controller = nil) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/telegram/bot/updates_poller.rb', line 17 def start(bot_id, controller = nil) bot = bot_id.is_a?(Symbol) ? Telegram.bots[bot_id] : Client.wrap(bot_id) instance = controller ? new(bot, controller) : instances[bot] raise "Poller not found for #{bot_id.inspect}" unless instance instance.start end |
Instance Method Details
#fetch_updates ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/telegram/bot/updates_poller.rb', line 66 def fetch_updates response = bot.async(false) { bot.get_updates(offset: offset, timeout: timeout) } updates = response.is_a?(Array) ? response : response['result'] return unless updates && updates.any? reload! do updates.each do |update| @offset = update['update_id'] + 1 yield update end end rescue Timeout::Error log { 'Fetch timeout' } end |
#log(&block) ⇒ Object
38 39 40 |
# File 'lib/telegram/bot/updates_poller.rb', line 38 def log(&block) logger.info(&block) if logger end |
#reload! ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/telegram/bot/updates_poller.rb', line 80 def reload! return yield unless reload reloading_code do if controller.is_a?(Class) && controller.name @controller = Object.const_get(controller.name) end yield end end |
#reloading_code ⇒ Object
91 92 93 94 95 |
# File 'lib/telegram/bot/updates_poller.rb', line 91 def reloading_code Rails.application.reloader.wrap do yield end end |
#start ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/telegram/bot/updates_poller.rb', line 42 def start return if running @running = true log { 'Started bot poller.' } while running begin fetch_updates do |update| controller.dispatch(bot, update) end rescue Interrupt @running = false rescue => e logger.error { ([e.] + e.backtrace).join("\n") } if logger end end log { 'Stop polling bot updates.' } end |
#stop ⇒ Object
60 61 62 63 64 |
# File 'lib/telegram/bot/updates_poller.rb', line 60 def stop return unless running log { 'Killing polling thread.' } @running = false end |