class Telegram::Bot::Client

Constants

SERVER
URL_TEMPLATE

Attributes

base_uri[R]
client[R]
token[R]
username[R]

Public Class Methods

by_id(id) click to toggle source
# File lib/telegram/bot/client.rb, line 29
def by_id(id)
  Telegram.bots[id]
end
error_for_response(response) click to toggle source
# 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
new(token = nil, username = nil, server: SERVER, **options) click to toggle source
# 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
prepare_async_args(action, body = {}) click to toggle source
# 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) click to toggle source

Encodes nested hashes as json.

# 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!() click to toggle source

Prepend TypedResponse module.

# File lib/telegram/bot/client.rb, line 34
def typed_response!
  prepend TypedResponse
end
wrap(input, **options) click to toggle source

Accepts different options to initialize bot.

# 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

Public Instance Methods

http_request(uri, body) click to toggle source

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.

# File lib/telegram/bot/client.rb, line 81
def http_request(uri, body)
  client.post(uri, body)
end
inspect() click to toggle source
# File lib/telegram/bot/client.rb, line 85
def inspect
  "#<#{self.class.name}##{object_id}(#{@username})>"
end
request(action, body = {}) click to toggle source
# 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