Class: Telegram::Bot::Api

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty
Defined in:
lib/telegram/bot/api.rb

Constant Summary collapse

ENDPOINTS =
%w(
  getMe sendMessage forwardMessage sendPhoto sendAudio sendDocument
  sendSticker sendVideo sendVoice sendLocation sendChatAction
  getUserProfilePhotos getUpdates setWebhook getFile answerInlineQuery
).freeze
REPLY_MARKUP_TYPES =
[
  Telegram::Bot::Types::ReplyKeyboardMarkup,
  Telegram::Bot::Types::ReplyKeyboardHide,
  Telegram::Bot::Types::ForceReply
].freeze
INLINE_QUERY_RESULT_TYPES =
[
  Telegram::Bot::Types::InlineQueryResultArticle,
  Telegram::Bot::Types::InlineQueryResultGif,
  Telegram::Bot::Types::InlineQueryResultMpeg4Gif,
  Telegram::Bot::Types::InlineQueryResultPhoto,
  Telegram::Bot::Types::InlineQueryResultVideo
].freeze
POOL_SIZE =
ENV.fetch('TELEGRAM_BOT_POOL_SIZE', 1).to_i.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Api

Returns a new instance of Api.



32
33
34
# File 'lib/telegram/bot/api.rb', line 32

def initialize(token)
  @token = token
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



36
37
38
39
40
41
# File 'lib/telegram/bot/api.rb', line 36

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

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



25
26
27
# File 'lib/telegram/bot/api.rb', line 25

def token
  @token
end

Instance Method Details

#call(endpoint, raw_params = {}) ⇒ Object



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

def call(endpoint, raw_params = {})
  params = build_params(raw_params)
  response = self.class.post("/bot#{token}/#{endpoint}", query: params)
  if response.code == 200
    response.to_hash
  else
    fail Exceptions::ResponseError.new(response),
         'Telegram API has returned the error.'
  end
end

#respond_to_missing?(*args) ⇒ Boolean

Returns:

  • (Boolean)


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

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