lib/telegram/bot/client.rb in telegram-bot-ruby-0.2.6 vs lib/telegram/bot/client.rb in telegram-bot-ruby-0.3.0
- old
+ new
@@ -1,18 +1,20 @@
module Telegram
module Bot
class Client
- attr_reader :api, :offset, :timeout
+ attr_reader :api, :offset, :timeout, :logger
def self.run(*args, &block)
new(*args).run(&block)
end
- def initialize(token, timeout = 20)
+ def initialize(token, h = {})
+ options = default_options.merge(h)
@api = Api.new(token)
- @offset = 0
- @timeout = timeout
+ @offset = options[:offset]
+ @timeout = options[:timeout]
+ @logger = options[:logger]
end
def run
yield self
end
@@ -23,14 +25,31 @@
next unless response['ok']
response['result'].each do |data|
update = Types::Update.new(data)
@offset = update.update_id.next
+ log_incoming_message(update.message)
yield update.message
end
end
rescue Net::ReadTimeout
retry
+ end
+
+ private
+
+ def default_options
+ { offset: 0, timeout: 20, logger: NullLogger.new }
+ end
+
+ def log_incoming_message(message)
+ logger.info(
+ format(
+ 'Incoming message: text="%s" uid=%i',
+ message.text,
+ message.from.id
+ )
+ )
end
end
end
end