Class: Telegram::Bot::Client

Inherits:
Object
  • Object
show all
Includes:
Async, ApiHelper, DebugClient
Defined in:
lib/telegram/bot/client.rb,
lib/telegram/bot/client/api_helper.rb,
lib/telegram/bot/client/typed_response.rb

Direct Known Subclasses

ClientStub

Defined Under Namespace

Modules: ApiHelper, TypedResponse

Constant Summary collapse

SERVER =
'https://api.telegram.org'.freeze
URL_TEMPLATE =
'%<server>s/bot%<token>s/'.freeze

Constants included from ApiHelper

ApiHelper::METHODS_LIST_FILE

Constants included from Async

Async::MISSING_VALUE

Instance Attribute Summary collapse

Attributes included from Async

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiHelper

define_helpers, methods_list

Methods included from DebugClient

#debug!, #debug_off!

Methods included from Async

#async, #async=, prepare_hash, prepended, thread_store

Constructor Details

#initialize(token = nil, username = nil, server: SERVER, **options) ⇒ Client

Returns a new instance of Client.



65
66
67
68
69
70
# File 'lib/telegram/bot/client.rb', line 65

def initialize(token = nil, username = nil, server: SERVER, **options)
  @client = HTTPClient.new
  @token = token || options[:token]
  @username = username || options[:username]
  @base_uri = format(URL_TEMPLATE, server: server, token: self.token)
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



63
64
65
# File 'lib/telegram/bot/client.rb', line 63

def base_uri
  @base_uri
end

#clientObject (readonly)

Returns the value of attribute client.



63
64
65
# File 'lib/telegram/bot/client.rb', line 63

def client
  @client
end

#tokenObject (readonly)

Returns the value of attribute token.



63
64
65
# File 'lib/telegram/bot/client.rb', line 63

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username.



63
64
65
# File 'lib/telegram/bot/client.rb', line 63

def username
  @username
end

Class Method Details

.by_id(id) ⇒ Object



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

def by_id(id)
  Telegram.bots[id]
end

.error_for_response(response) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/telegram/bot/client.rb', line 50

def error_for_response(response)
  result = JSON.parse(response.body) rescue nil # rubocop:disable RescueModifier
  return Error.new(response.reason) unless result
  message = result['description'] || '-'
  # This errors are raised only for valid responses from Telegram
  case response.status
  when 403 then Forbidden.new(message)
  when 404 then NotFound.new(message)
  else Error.new("#{response.reason}: #{message}")
  end
end

.prepare_async_args(action, body = {}) ⇒ Object



46
47
48
# File 'lib/telegram/bot/client.rb', line 46

def prepare_async_args(action, body = {})
  [action.to_s, Async.prepare_hash(prepare_body(body))]
end

.prepare_body(body) ⇒ Object

Encodes nested hashes as json.



39
40
41
42
43
44
# File 'lib/telegram/bot/client.rb', line 39

def prepare_body(body)
  body = body.dup
  body.each do |k, val|
    body[k] = val.to_json if val.is_a?(Hash) || val.is_a?(Array)
  end
end

.typed_response!Object

Prepend TypedResponse module.



34
35
36
# File 'lib/telegram/bot/client.rb', line 34

def typed_response!
  prepend TypedResponse
end

.wrap(input, **options) ⇒ Object

Accepts different options to initialize bot.



20
21
22
23
24
25
26
27
# File 'lib/telegram/bot/client.rb', line 20

def wrap(input, **options)
  case input
  when Symbol then by_id(input) or raise "#{name} #{input.inspect} not configured"
  when self   then input
  when Hash   then new(**input.symbolize_keys, **options)
  else        new(input, **options)
  end
end

Instance Method Details

#http_request(uri, body) ⇒ Object

Endpoint for low-level request. For easy host highjacking & instrumentation. Params are not used directly but kept for instrumentation purpose. You probably don’t want to use this method directly.



81
82
83
# File 'lib/telegram/bot/client.rb', line 81

def http_request(uri, body)
  client.post(uri, body)
end

#inspectObject



85
86
87
# File 'lib/telegram/bot/client.rb', line 85

def inspect
  "#<#{self.class.name}##{object_id}(#{@username})>"
end

#request(action, body = {}) ⇒ Object



72
73
74
75
76
# File 'lib/telegram/bot/client.rb', line 72

def request(action, body = {})
  response = http_request("#{base_uri}#{action}", self.class.prepare_body(body))
  raise self.class.error_for_response(response) if response.status >= 300
  JSON.parse(response.body)
end