Class: Telegram::Bot::Api
- Inherits:
-
Object
- Object
- Telegram::Bot::Api
- Defined in:
- lib/telegram/bot/api.rb
Constant Summary collapse
- ENDPOINTS =
%w[ getUpdates setWebhook deleteWebhook getWebhookInfo getMe sendMessage forwardMessage sendPhoto sendAudio sendDocument sendVideo sendVoice sendVideoNote sendMediaGroup sendLocation editMessageLiveLocation stopMessageLiveLocation sendVenue sendContact sendChatAction getUserProfilePhotos getFile kickChatMember unbanChatMember restrictChatMember promoteChatMember leaveChat getChat getChatAdministrators exportChatInviteLink setChatPhoto deleteChatPhoto setChatTitle setChatDescription pinChatMessage unpinChatMessage getChatMembersCount getChatMember setChatStickerSet deleteChatStickerSet answerCallbackQuery editMessageText editMessageCaption editMessageReplyMarkup deleteMessage sendSticker getStickerSet uploadStickerFile createNewStickerSet addStickerToSet setStickerPositionInSet deleteStickerFromSet answerInlineQuery sendInvoice answerShippingQuery answerPreCheckoutQuery sendGame setGameScore getGameHighScores setPassportDataErrors editMessageMedia sendAnimation sendPoll stopPoll setChatPermissions setChatAdministratorCustomTitle sendDice getMyCommands setMyCommands deleteMyCommands setStickerSetThumb logOut close copyMessage createChatInviteLink editChatInviteLink revokeChatInviteLink approveChatJoinRequest declineChatJoinRequest banChatSenderChat unbanChatSenderChat answerWebAppQuery setChatMenuButton getChatMenuButton setMyDefaultAdministratorRights getMyDefaultAdministratorRights createInvoiceLink ].freeze
Instance Attribute Summary collapse
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #call(endpoint, raw_params = {}) ⇒ Object
-
#initialize(token, url: 'https://api.telegram.org', environment: :production) ⇒ Api
constructor
A new instance of Api.
- #method_missing(method_name, *args, &block) ⇒ Object
- #respond_to_missing?(*args) ⇒ Boolean
Constructor Details
#initialize(token, url: 'https://api.telegram.org', environment: :production) ⇒ Api
Returns a new instance of Api.
34 35 36 37 38 |
# File 'lib/telegram/bot/api.rb', line 34 def initialize(token, url: 'https://api.telegram.org', environment: :production) @token = token @url = url @environment = environment.downcase.to_sym end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/telegram/bot/api.rb', line 40 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
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
32 33 34 |
# File 'lib/telegram/bot/api.rb', line 32 def environment @environment end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
32 33 34 |
# File 'lib/telegram/bot/api.rb', line 32 def token @token end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
32 33 34 |
# File 'lib/telegram/bot/api.rb', line 32 def url @url end |
Instance Method Details
#call(endpoint, raw_params = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/telegram/bot/api.rb', line 54 def call(endpoint, raw_params = {}) params = build_params(raw_params) path = build_path(endpoint) response = conn.post(path, params) unless response.status == 200 raise Exceptions::ResponseError.new(response), 'Telegram API has returned the error.' end JSON.parse(response.body) end |
#respond_to_missing?(*args) ⇒ Boolean
47 48 49 50 51 52 |
# File 'lib/telegram/bot/api.rb', line 47 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 |