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 sendLocation sendChatAction getUserProfilePhotos
  getUpdates setWebhook
).freeze
REPLY_MARKUP_TYPES =
[
  Telegram::Bot::Types::ReplyKeyboardMarkup,
  Telegram::Bot::Types::ReplyKeyboardHide,
  Telegram::Bot::Types::ForceReply
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Api

Returns a new instance of Api.



22
23
24
# File 'lib/telegram/bot/api.rb', line 22

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



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

def method_missing(method_name, *args, &block)
  ENDPOINTS.include?(method_name.to_s) ? call(method_name, *args) : super
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



17
18
19
# File 'lib/telegram/bot/api.rb', line 17

def token
  @token
end

Instance Method Details

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



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

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_h
  else
    fail Exceptions::ResponseError.new(response),
         'Telegram API has returned the error.'
  end
end