Class: Telebot::Bot

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

Constant Summary collapse

DEFAULT_UPDATE_TIME =

Default update time

2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, update_time: DEFAULT_UPDATE_TIME) ⇒ Bot

Returns a new instance of Bot.



8
9
10
11
12
# File 'lib/telebot/bot.rb', line 8

def initialize(token, update_time: DEFAULT_UPDATE_TIME)
  @client = Client.new(token)
  @processed_update_ids = []
  @update_time = update_time
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#processed_update_idsObject (readonly)

Returns the value of attribute processed_update_ids.



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

def processed_update_ids
  @processed_update_ids
end

#update_timeObject (readonly)

Returns the value of attribute update_time.



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

def update_time
  @update_time
end

Instance Method Details

#run(&block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/telebot/bot.rb', line 14

def run(&block)
  loop do
    # Note: https://core.telegram.org/bots/api#getupdates
    # To mark an update as confirmed need to use offset+1
    offset = @processed_update_ids.last && @processed_update_ids.last + 1
    updates = @client.get_updates(offset: offset)

    updates.each do |update|
      next if @processed_update_ids.include?(update.update_id)
      process_update(update, block)
      @processed_update_ids << update.update_id
    end

    sleep(@update_time)
  end
end