Class: Telegram::Bot::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/telegram/bot/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, h = {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
# File 'lib/telegram/bot/client.rb', line 10

def initialize(token, h = {})
  options = default_options.merge(h)
  @api = Api.new(token)
  @offset = options[:offset]
  @timeout = options[:timeout]
  @logger = options[:logger]
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



4
5
6
# File 'lib/telegram/bot/client.rb', line 4

def api
  @api
end

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/telegram/bot/client.rb', line 4

def logger
  @logger
end

#offsetObject (readonly)

Returns the value of attribute offset.



4
5
6
# File 'lib/telegram/bot/client.rb', line 4

def offset
  @offset
end

#timeoutObject (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



6
7
8
# File 'lib/telegram/bot/client.rb', line 6

def self.run(*args, &block)
  new(*args).run(&block)
end

Instance Method Details

#listenObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/telegram/bot/client.rb', line 22

def listen
  loop do
    response = api.getUpdates(offset: offset, timeout: timeout)
    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

#run {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



18
19
20
# File 'lib/telegram/bot/client.rb', line 18

def run
  yield self
end