Class: Telegraphist::Bot

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/telegraphist/bot.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/telegraphist/bot.rb', line 5

def client
  @client
end

#first_nameObject

Returns the value of attribute first_name.



5
6
7
# File 'lib/telegraphist/bot.rb', line 5

def first_name
  @first_name
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/telegraphist/bot.rb', line 5

def id
  @id
end

#usernameObject

Returns the value of attribute username.



5
6
7
# File 'lib/telegraphist/bot.rb', line 5

def username
  @username
end

Class Method Details

.create(authentication_token) ⇒ Object



7
8
9
10
# File 'lib/telegraphist/bot.rb', line 7

def self.create(authentication_token)
  client = Client.new(authentication_token)
  new(client.get(:getMe)['result'].merge(client: client))
end

Instance Method Details

#send_message(chat_id, text) ⇒ Object



29
30
31
# File 'lib/telegraphist/bot.rb', line 29

def send_message(chat_id, text)
  client.post(:sendMessage, query: {chat_id: chat_id, text: text})
end

#update_loop(&block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/telegraphist/bot.rb', line 12

def update_loop(&block)
  last_update = nil
  loop do
    updates(last_update).try(:each) do |update|
      block.call(update)
      last_update = [last_update, update.update_id + 1].compact.max
    end
    sleep 1
  end
end

#updates(offset = nil) ⇒ Object



23
24
25
26
27
# File 'lib/telegraphist/bot.rb', line 23

def updates(offset=nil)
  (client.get(:getUpdates, query: {offset: offset})['result'] || []).map do |update|
    Model::Update.new(update)
  end
end