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, hash = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(token, hash = {})
  @options = default_options.merge(hash)
  @api = Api.new(token, url: options.delete(:url))
  @logger = options.delete(:logger)
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



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

def api
  @api
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.run(*args, &block) ⇒ Object



9
10
11
# File 'lib/telegram/bot/client.rb', line 9

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

Instance Method Details

#fetch_updatesObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/telegram/bot/client.rb', line 31

def fetch_updates
  response = api.getUpdates(options)
  return unless response['ok']

  response['result'].each do |data|
    yield handle_update(Types::Update.new(data))
  end
rescue Faraday::TimeoutError, Faraday::ConnectionFailed
  retry
end

#handle_update(update) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/telegram/bot/client.rb', line 42

def handle_update(update)
  @options[:offset] = update.update_id.next
  message = update.current_message
  log_incoming_message(message)

  message
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

Yields:

  • (_self)

Yield Parameters:



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

def run
  yield self
end