class Telegram::Bot::Api

Constants

ENDPOINTS
INLINE_QUERY_RESULT_TYPES
REPLY_MARKUP_TYPES

Attributes

token[R]

Public Class Methods

new(token) click to toggle source
# File lib/telegram/bot/api.rb, line 52
def initialize(token)
  @token = token
end

Public Instance Methods

call(endpoint, raw_params = {}, force = false) click to toggle source
# File lib/telegram/bot/api.rb, line 70
def call(endpoint, raw_params = {}, force = false)
  params = force ? raw_params : build_params(raw_params)
  response = conn.post("/bot#{token}/#{endpoint}", params.to_json)
  if response.status == 200
    JSON.parse(response.body)
  elsif response.finished?
    raise Exceptions::ResponseError.new(response),
          'Telegram API has returned the error.'
  else
    response          
  end
end
in_parallel(&block) click to toggle source
# File lib/telegram/bot/api.rb, line 83
def in_parallel(&block)
  conn.in_parallel(&block)
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/telegram/bot/api.rb, line 56
def method_missing(method_name, *args, &block)
  endpoint = method_name.to_s
  endpoint = camelize(endpoint) if endpoint.include?('_')

  ENDPOINTS.include?(endpoint) ? call(endpoint, *args) : super
end
respond_to_missing?(*args) click to toggle source
Calls superclass method
# File lib/telegram/bot/api.rb, line 63
def respond_to_missing?(*args)
  method_name = args[0].to_s
  method_name = camelize(method_name) if method_name.include?('_')

  ENDPOINTS.include?(method_name) || super
end

Private Instance Methods

build_params(h) click to toggle source
# File lib/telegram/bot/api.rb, line 89
def build_params(h)
  h.each_with_object({}) do |(key, value), params|
    params[key] = sanitize_value(value)
  end
end
camelize(method_name) click to toggle source
# File lib/telegram/bot/api.rb, line 111
def camelize(method_name)
  words = method_name.split('_')
  words.drop(1).map(&:capitalize!)
  words.join
end
conn() click to toggle source
# File lib/telegram/bot/api.rb, line 117
def conn
  @conn ||=
    case ENV['BotProxyType']
    when 'https'
      set_http_proxy_connection('https')
    when 'http'
      set_http_proxy_connection('http') 
    when 'socks5'
      set_socks5_connection
    else
      set_default_connection
    end
end
jsonify_inline_query_results(value) click to toggle source
# File lib/telegram/bot/api.rb, line 104
def jsonify_inline_query_results(value)
  return value unless
    value.is_a?(Array) &&
    value.all? { |i| INLINE_QUERY_RESULT_TYPES.include?(i.class) }
  value.map { |i| i.to_compact_hash.select { |_, v| v } }.to_json
end
jsonify_reply_markup(value) click to toggle source
# File lib/telegram/bot/api.rb, line 99
def jsonify_reply_markup(value)
  return value unless REPLY_MARKUP_TYPES.include?(value.class)
  value.to_compact_hash.to_json
end
sanitize_value(value) click to toggle source
# File lib/telegram/bot/api.rb, line 95
def sanitize_value(value)
  jsonify_inline_query_results(jsonify_reply_markup(value))
end
set_default_connection() click to toggle source
# File lib/telegram/bot/api.rb, line 141
def set_default_connection
  Faraday.new(url: 'https://api.telegram.org', ssl: {verify:false}) do |faraday|
    faraday.request :multipart
    faraday.request :url_encoded
    faraday.adapter Telegram::Bot.configuration.adapter
  end
end
set_http_proxy_connection(proto) click to toggle source
# File lib/telegram/bot/api.rb, line 131
def set_http_proxy_connection(proto)
  host = "#{proto}://#{ENV['BotProxyHost']}:#{ENV['BotProxyPort']}"
  Faraday.new(url: 'https://api.telegram.org', ssl: {verify:false}) do |faraday|
    faraday.request :multipart
    faraday.request :url_encoded
    faraday.adapter Telegram::Bot.configuration.adapter
    faraday.proxy URI.parse(host)
  end
end
set_socks5_connection() click to toggle source
# File lib/telegram/bot/api.rb, line 149
def set_socks5_connection
  require 'telegram/bot/adapters/socks5'

  host = "https://#{ENV['BotProxyHost']}:#{ENV['BotProxyPort']}"                                            
  proxy_opts = {
      uri: URI.parse(host),
      user: ENV['BotProxyLogin'],
      password: ENV['BotProxyPass'],
      socks: true
  }

  Faraday.new(url: 'https://api.telegram.org',
                           ssl: {verify:false},
                           request: { proxy: proxy_opts }) do |c|
    c.request :multipart
    c.request :url_encoded
    c.headers['Content-Type'] = 'application/json'
    Faraday::Adapter.register_middleware socks5: Telegram::Bot::Adapters::Socks5
    c.adapter :socks5
  end
end