Class: Telegram::Bot::Runner

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Runner



10
11
12
13
# File 'lib/telegram/bot/runner.rb', line 10

def initialize(token)
  @api = Api.new(token)
  @offset = 0
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



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

def api
  @api
end

#offsetObject (readonly)

Returns the value of attribute offset.



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

def offset
  @offset
end

Class Method Details

.run(*args, &block) ⇒ Object



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

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

Instance Method Details

#listenObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/telegram/bot/runner.rb', line 19

def listen
  loop do
    response = api.getUpdates(offset: offset)
    next unless response['ok']

    response['result'].each do |data|
      update = Types::Update.new(data)
      @offset = update.update_id.next
      yield update.message
    end
  end
end

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

Yields:

  • (_self)

Yield Parameters:



15
16
17
# File 'lib/telegram/bot/runner.rb', line 15

def run
  yield self
end