Class: Telegram::Bot::Client
- Inherits:
-
Object
- Object
- Telegram::Bot::Client
- Defined in:
- lib/telegram/bot/client.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #fetch_updates ⇒ Object
-
#initialize(token, h = {}) ⇒ Client
constructor
A new instance of Client.
- #listen(&block) ⇒ Object
- #run {|_self| ... } ⇒ Object
Constructor Details
#initialize(token, h = {}) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 |
# File 'lib/telegram/bot/client.rb', line 11 def initialize(token, h = {}) = .merge(h) @api = Api.new(token) @offset = [:offset] @timeout = [:timeout] @logger = [:logger] end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
4 5 6 |
# File 'lib/telegram/bot/client.rb', line 4 def api @api end |
#logger ⇒ Object
Returns the value of attribute logger.
5 6 7 |
# File 'lib/telegram/bot/client.rb', line 5 def logger @logger end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
4 5 6 |
# File 'lib/telegram/bot/client.rb', line 4 def offset @offset end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
4 5 6 |
# File 'lib/telegram/bot/client.rb', line 4 def timeout @timeout end |
Class Method Details
.run(*args, &block) ⇒ Object
7 8 9 |
# File 'lib/telegram/bot/client.rb', line 7 def self.run(*args, &block) new(*args).run(&block) end |
Instance Method Details
#fetch_updates ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/telegram/bot/client.rb', line 31 def fetch_updates response = api.getUpdates(offset: offset, timeout: timeout) return unless response['ok'] response['result'].each do |data| update = Types::Update.new(data) @offset = update.update_id.next = (update) () yield end rescue Faraday::Error::TimeoutError retry end |
#listen(&block) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/telegram/bot/client.rb', line 23 def listen(&block) logger.info('Starting bot') running = true Signal.trap('INT') { running = false } fetch_updates(&block) while running exit end |
#run {|_self| ... } ⇒ Object
19 20 21 |
# File 'lib/telegram/bot/client.rb', line 19 def run yield self end |