Class: Telegram::Bot::Api
- Inherits:
-
Object
- Object
- Telegram::Bot::Api
- Defined in:
- lib/telegram/bot/api.rb,
lib/telegram/bot/api/endpoints.rb
Constant Summary collapse
- ENDPOINTS =
{ 'getUpdates' => Types::Array.of(Types::Update), 'setWebhook' => Types::Bool, 'deleteWebhook' => Types::Bool, 'getWebhookInfo' => Types::WebhookInfo, 'getMe' => Types::User, 'sendMessage' => Types::Message, 'forwardMessage' => Types::Message, 'forwardMessages' => Types::Array.of(Types::MessageId), 'sendPhoto' => Types::Message, 'sendAudio' => Types::Message, 'sendDocument' => Types::Message, 'sendVideo' => Types::Message, 'sendVoice' => Types::Message, 'sendVideoNote' => Types::Message, 'sendMediaGroup' => Types::Array.of(Types::Message), 'sendLocation' => Types::Message, 'editMessageLiveLocation' => Types::Message | Types::Bool, 'stopMessageLiveLocation' => Types::Message | Types::Bool, 'sendVenue' => Types::Message, 'sendContact' => Types::Message, 'sendChatAction' => Types::Bool, 'setMessageReaction' => Types::Bool, 'getUserProfilePhotos' => Types::UserProfilePhotos, 'getFile' => Types::File, 'banChatMember' => Types::Bool, 'unbanChatMember' => Types::Bool, 'restrictChatMember' => Types::Bool, 'promoteChatMember' => Types::Bool, 'leaveChat' => Types::Bool, 'getChat' => Types::ChatFullInfo, 'getChatAdministrators' => Types::Array.of(Types::ChatMember), 'exportChatInviteLink' => Types::String, 'setChatPhoto' => Types::Bool, 'deleteChatPhoto' => Types::Bool, 'setChatTitle' => Types::Bool, 'setChatDescription' => Types::Bool, 'pinChatMessage' => Types::Bool, 'unpinChatMessage' => Types::Bool, 'getChatMembersCount' => Types::Integer, 'getChatMember' => Types::ChatMember, 'setChatStickerSet' => Types::Bool, 'deleteChatStickerSet' => Types::Bool, 'answerCallbackQuery' => Types::Bool, 'getUserChatBoosts' => Types::UserChatBoosts, 'editMessageText' => Types::Message | Types::Bool, 'editMessageCaption' => Types::Message | Types::Bool, 'editMessageReplyMarkup' => Types::Message | Types::Bool, 'deleteMessage' => Types::Bool, 'deleteMessages' => Types::Bool, 'sendSticker' => Types::Message, 'getStickerSet' => Types::StickerSet, 'uploadStickerFile' => Types::File, 'createNewStickerSet' => Types::Bool, 'addStickerToSet' => Types::Bool, 'setStickerPositionInSet' => Types::Bool, 'deleteStickerFromSet' => Types::Bool, 'answerInlineQuery' => Types::Bool, 'sendInvoice' => Types::Message, 'answerShippingQuery' => Types::Bool, 'answerPreCheckoutQuery' => Types::Bool, 'sendGame' => Types::Message, 'setGameScore' => Types::Message | Types::Bool, 'getGameHighScores' => Types::Array.of(Types::GameHighScore), 'setPassportDataErrors' => Types::Bool, 'editMessageMedia' => Types::Message | Types::Bool, 'sendAnimation' => Types::Message, 'sendPoll' => Types::Message, 'stopPoll' => Types::Poll, 'setChatPermissions' => Types::Bool, 'setChatAdministratorCustomTitle' => Types::Bool, 'sendDice' => Types::Message, 'getMyCommands' => Types::Array.of(Types::BotCommand), 'setMyCommands' => Types::Bool, 'deleteMyCommands' => Types::Bool, 'setStickerSetThumbnail' => Types::Bool, 'logOut' => Types::Bool, 'close' => Types::Bool, 'copyMessage' => Types::MessageId, 'copyMessages' => Types::Array.of(Types::MessageId), 'createChatInviteLink' => Types::ChatInviteLink, 'editChatInviteLink' => Types::ChatInviteLink, 'revokeChatInviteLink' => Types::ChatInviteLink, 'approveChatJoinRequest' => Types::Bool, 'declineChatJoinRequest' => Types::Bool, 'banChatSenderChat' => Types::Bool, 'unbanChatSenderChat' => Types::Bool, 'answerWebAppQuery' => Types::SentWebAppMessage, 'setChatMenuButton' => Types::Bool, 'getChatMenuButton' => Types::MenuButtonDefault | Types::MenuButtonCommands | Types::MenuButtonWebApp, 'setMyDefaultAdministratorRights' => Types::Bool, 'getMyDefaultAdministratorRights' => Types::ChatAdministratorRights, 'createInvoiceLink' => Types::String, 'editGeneralForumTopic' => Types::Bool, 'closeGeneralForumTopic' => Types::Bool, 'reopenGeneralForumTopic' => Types::Bool, 'hideGeneralForumTopic' => Types::Bool, 'unhideGeneralForumTopic' => Types::Bool, 'unpinAllGeneralForumTopicMessages' => Types::Bool, 'setMyName' => Types::Bool, 'getMyName' => Types::BotName, 'setMyDescription' => Types::Bool, 'getMyDescription' => Types::BotDescription, 'setMyShortDescription' => Types::Bool, 'getMyShortDescription' => Types::BotShortDescription, 'refundStarPayment' => Types::Bool, 'getStarTransactions' => Types::StarTransactions, 'sendPaidMedia' => Types::Message, 'createChatSubscriptionInviteLink' => Types::ChatInviteLink, 'editChatSubscriptionInviteLink' => Types::ChatInviteLink, 'getAvailableGifts' => Types::Gifts, 'sendGift' => Types::Bool, 'savePreparedInlineMessage' => Types::PreparedInlineMessage, 'editUserStarSubscription' => Types::Bool, 'setUserEmojiStatus' => Types::Bool }.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
- #connection ⇒ 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.
8 9 10 11 12 |
# File 'lib/telegram/bot/api.rb', line 8 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
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/telegram/bot/api.rb', line 24 def method_missing(method_name, *args, &block) endpoint = method_name.to_s endpoint = camelize(endpoint) if endpoint.include?('_') return super unless ENDPOINTS.key?(endpoint) result = call(endpoint, *args) return result['ok'] unless (result = result['result']) ENDPOINTS[endpoint].call(result) end |
Instance Attribute Details
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
6 7 8 |
# File 'lib/telegram/bot/api.rb', line 6 def environment @environment end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
6 7 8 |
# File 'lib/telegram/bot/api.rb', line 6 def token @token end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
6 7 8 |
# File 'lib/telegram/bot/api.rb', line 6 def url @url end |
Instance Method Details
#call(endpoint, raw_params = {}) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/telegram/bot/api.rb', line 44 def call(endpoint, raw_params = {}) params = build_params(raw_params) path = build_path(endpoint) response = connection.post(path, params) raise Exceptions::ResponseError.new(response: response) unless response.status == 200 JSON.parse(response.body) end |
#connection ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/telegram/bot/api.rb', line 14 def connection @connection ||= Faraday.new(url: url) do |faraday| faraday.request :multipart faraday.request :url_encoded faraday.adapter Telegram::Bot.configuration.adapter faraday..timeout = Telegram::Bot.configuration.connection_timeout faraday..open_timeout = Telegram::Bot.configuration.connection_open_timeout end end |
#respond_to_missing?(*args) ⇒ Boolean
37 38 39 40 41 42 |
# File 'lib/telegram/bot/api.rb', line 37 def respond_to_missing?(*args) method_name = args[0].to_s method_name = camelize(method_name) if method_name.include?('_') ENDPOINTS.key?(method_name) || super end |