Class: Telegrambot::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/telegrambot/api.rb

Constant Summary collapse

REPLY_MARKUP_TYPES =
[
    Telegrambot::Types::ReplyKeyboardMarkup,
    Telegrambot::Types::ReplyKeyboardHide,
    Telegrambot::Types::ForceReply,
    Telegrambot::Types::InlineKeyboardMarkup
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Api

Returns a new instance of Api.



13
14
15
# File 'lib/telegrambot/api.rb', line 13

def initialize(token)
  @token = token
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



4
5
6
# File 'lib/telegrambot/api.rb', line 4

def token
  @token
end

Class Method Details

.snakecase(endpoint) ⇒ Object

Convierte un camelCase a snake_case

Returns:

  • String



20
21
22
23
24
25
26
# File 'lib/telegrambot/api.rb', line 20

def self.snakecase(endpoint)
  endpoint.to_s.gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end

Instance Method Details

#endpointHash

Endpoints de Telegram Bot API (Metaprogramming)

Parameters:

  • raw_params (Hash)

    Hash de opciones de telegram

Returns:

  • (Hash)

Raises:

See Also:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/telegrambot/api.rb', line 34

%w(
    getUpdates setWebhook getMe sendMessage forwardMessage sendPhoto
    sendAudio sendDocument sendSticker sendVideo sendVoice sendLocation
    sendVenue sendContact sendChatAction getUserProfilePhotos getFile
    kickChatMember unbanChatMember answerCallbackQuery editMessageText
    editMessageCaption editMessageReplyMarkup answerInlineQuery getChat
    leaveChat getChatAdministrators getChatMember getChatMembersCount
).map{ |e| snakecase(e) }.each do | endpoint |
  define_method endpoint do | raw_params = {} |
    params = build_params(raw_params)
    response = rest_client.post("/bot#{token}/#{camelcase(endpoint)}", params)
    raise Telegrambot::Exceptions::ResponseError.new(response) unless response.status == 200
    JSON.parse(response.body)
  end
end