class Telegram::Bot::Client
Attributes
api[R]
logger[RW]
options[R]
Public Class Methods
new(token, h = {})
click to toggle source
# File lib/telegram/bot/client.rb, line 11 def initialize(token, h = {}) @options = default_options.merge(h) @api = Api.new(token) @logger = options.delete(:logger) end
run(*args, &block)
click to toggle source
# File lib/telegram/bot/client.rb, line 7 def self.run(*args, &block) new(*args).run(&block) end
Public Instance Methods
fetch_updates() { |message| ... }
click to toggle source
# File lib/telegram/bot/client.rb, line 48 def fetch_updates response = api.getUpdates(options) return unless response['ok'] response['result'].each do |data| update = Types::Update.new(data) @options[:offset] = update.update_id.next message = update.current_message log_incoming_message(message) yield message end rescue Faraday::Error::TimeoutError retry end
listen(&block)
click to toggle source
# File lib/telegram/bot/client.rb, line 21 def listen(&block) logger.info('Starting bot') catch(:stop) { loop { fetch_updates(&block) } } end
listen_for(minutes = 15, &block)
click to toggle source
Listen for a given period of time (in minutes)
# File lib/telegram/bot/client.rb, line 35 def listen_for(minutes = 15, &block) counter = 15 * 60 interval = 5 # Check every 5 seconds interval_timer = 1 # must start at 1 now = Time.now while Time.now - now < counter if interval_timer % interval == 0 #Every 5 attempts the activity will process fetch_updates(&block) end interval_timer = interval_timer + 1 end end
run() { |self| ... }
click to toggle source
# File lib/telegram/bot/client.rb, line 17 def run yield self end
stop()
click to toggle source
This can also be written in any calling code (the throw does not have to appear within the static scope of the catch), but is included here for completeness
# File lib/telegram/bot/client.rb, line 30 def stop throw :stop end
Private Instance Methods
default_options()
click to toggle source
# File lib/telegram/bot/client.rb, line 65 def default_options { offset: 0, timeout: 20, logger: NullLogger.new } end
log_incoming_message(message)
click to toggle source
# File lib/telegram/bot/client.rb, line 69 def log_incoming_message(message) uid = message.from ? message.from.id : nil logger.info( format('Incoming message: text="%s" uid=%s', message, uid) ) end