class Telegram::Bot::UpdatesPoller
Supposed to be used in development environments only.
Constants
- DEFAULT_TIMEOUT
Attributes
bot[R]
controller[R]
logger[R]
offset[R]
reload[R]
running[R]
timeout[R]
Public Class Methods
add(bot, controller)
click to toggle source
Create, start and add poller instnace to tracked instances list.
# File lib/telegram/bot/updates_poller.rb, line 13 def add(bot, controller) new(bot, controller).tap { |x| instances[bot] = x } end
instances()
click to toggle source
# File lib/telegram/bot/updates_poller.rb, line 8 def instances @@instances end
new(bot, controller, **options)
click to toggle source
# File lib/telegram/bot/updates_poller.rb, line 29 def initialize(bot, controller, **options) @logger = options.fetch(:logger) { defined?(Rails.logger) && Rails.logger } @bot = bot @controller = controller @timeout = options.fetch(:timeout) { DEFAULT_TIMEOUT } @offset = options[:offset] @reload = options.fetch(:reload) { defined?(Rails.env) && Rails.env.development? } end
start(bot_id, controller = nil)
click to toggle source
# 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
Public Instance Methods
fetch_updates(offset = self.offset)
click to toggle source
# File lib/telegram/bot/updates_poller.rb, line 70 def fetch_updates(offset = self.offset) response = bot.async(false) { bot.get_updates(offset: offset, timeout: timeout) } response.is_a?(Array) ? response : response['result'] rescue Timeout::Error log { 'Fetch timeout' } nil end
log(&block)
click to toggle source
# File lib/telegram/bot/updates_poller.rb, line 38 def log(&block) logger.info(&block) if logger end
process_update(update)
click to toggle source
Override this method to setup custom error collector.
# File lib/telegram/bot/updates_poller.rb, line 90 def process_update(update) controller.dispatch(bot, update) end
process_updates(updates)
click to toggle source
# File lib/telegram/bot/updates_poller.rb, line 78 def process_updates(updates) reload! do updates.each do |update| @offset = update['update_id'] + 1 process_update(update) end end rescue StandardError => e logger.error { ([e.message] + e.backtrace).join("\n") } if logger end
reload!() { || ... }
click to toggle source
# File lib/telegram/bot/updates_poller.rb, line 94 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() { || ... }
click to toggle source
# File lib/telegram/bot/updates_poller.rb, line 105 def reloading_code Rails.application.reloader.wrap do yield end end
run()
click to toggle source
# File lib/telegram/bot/updates_poller.rb, line 56 def run while running updates = fetch_updates process_updates(updates) if updates && updates.any? end end
start()
click to toggle source
# File lib/telegram/bot/updates_poller.rb, line 42 def start return if running begin @running = true log { 'Started bot poller.' } run rescue Interrupt nil # noop ensure @running = false end log { 'Stopped polling bot updates.' } end
stop()
click to toggle source
Method to stop poller from other thread.
# File lib/telegram/bot/updates_poller.rb, line 64 def stop return unless running log { 'Stopping polling bot updates.' } @running = false end