From aaba27eb70ec38443c48331e925a76f57bec93fc Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Fri, 11 Aug 2017 14:21:59 +0300 Subject: [PATCH] New API methods and payload types (up to Bot API 3.2) --- lib/telegram/bot/client.rb | 26 +++++++++++++++++-- lib/telegram/bot/updates_controller.rb | 21 ++++++++++----- .../callback_query_context.rb | 1 - spec/telegram/bot/updates_controller_spec.rb | 18 ++++++++++++- 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/lib/telegram/bot/client.rb b/lib/telegram/bot/client.rb index 699c24c..a8006ea 100644 --- a/lib/telegram/bot/client.rb +++ b/lib/telegram/bot/client.rb @@ -5,7 +5,7 @@ require 'active_support/core_ext/hash/keys' module Telegram module Bot - class Client + class Client # rubocop:disable ClassLength URL_TEMPLATE = 'https://api.telegram.org/bot%s/'.freeze autoload :TypedResponse, 'telegram/bot/client/typed_response' @@ -70,6 +70,8 @@ module Telegram setWebhook answerCallbackQuery + deleteChatPhoto + exportChatInviteLink forwardMessage getChat getChatAdministrators @@ -80,6 +82,9 @@ module Telegram getUserProfilePhotos kickChatMember leaveChat + pinChatMessage + promoteChatMember + restrictChatMember sendAudio sendChatAction sendContact @@ -87,18 +92,35 @@ module Telegram sendLocation sendMessage sendPhoto - sendSticker sendVenue sendVideo + sendVideoNote sendVoice + setChatDescription + setChatPhoto + setChatTitle unbanChatMember + unpinChatMessage + deleteMessage editMessageCaption editMessageReplyMarkup editMessageText + sendSticker + getStickerSet + uploadStickerFile + createNewStickerSet + addStickerToSet + setStickerPositionInSet + deleteStickerFromSet + answerInlineQuery + sendInvoice + answerShippingQuery + answerPreCheckoutQuery + getGameHighScores sendGame setGameScore diff --git a/lib/telegram/bot/updates_controller.rb b/lib/telegram/bot/updates_controller.rb index bb7f3a4..a633e69 100644 --- a/lib/telegram/bot/updates_controller.rb +++ b/lib/telegram/bot/updates_controller.rb @@ -86,6 +86,8 @@ module Telegram inline_query chosen_inline_result callback_query + shipping_query + pre_checkout_query ).freeze CMD_REGEX = %r{\A/([a-z\d_]{,31})(@(\S+))?(\s|$)}i CONFLICT_CMD_REGEX = Regexp.new("^(#{PAYLOAD_TYPES.join('|')}|\\d)") @@ -169,15 +171,21 @@ module Telegram # Returns array `[is_command?, action, args]`. def action_for_payload if payload_type - send("action_for_#{payload_type}") || [false, payload_type, [payload]] + send("action_for_#{payload_type}") || action_for_default_payload else [false, :unsupported_payload_type, []] end end + def action_for_default_payload + [false, payload_type, [payload]] + end + # If payload is a message with command, then returned action is an # action for this command. # Separate method, so it can be easily overriden (ex. MessageContext). + # + # This is not used for edited messages/posts. It process them as basic updates. def action_for_message cmd, args = self.class.command_from_text(payload['text'], bot_username) cmd &&= self.class.action_for_command(cmd) @@ -185,12 +193,6 @@ module Telegram end alias_method :action_for_channel_post, :action_for_message - # It doesn't extract commands from edited messages. Just process - # them as usual ones. - def action_for_edited_message - end - alias_method :action_for_edited_channel_post, :action_for_edited_message - def action_for_inline_query [false, payload_type, [payload['query'], payload['offset']]] end @@ -208,6 +210,11 @@ module Telegram def action_missing(*) end + PAYLOAD_TYPES.each do |type| + method = :"action_for_#{type}" + alias_method method, :action_for_default_payload unless instance_methods.include?(method) + end + ActiveSupport.run_load_hooks('telegram.bot.updates_controller', self) end end diff --git a/lib/telegram/bot/updates_controller/callback_query_context.rb b/lib/telegram/bot/updates_controller/callback_query_context.rb index 5dde2f7..21a1eeb 100644 --- a/lib/telegram/bot/updates_controller/callback_query_context.rb +++ b/lib/telegram/bot/updates_controller/callback_query_context.rb @@ -15,7 +15,6 @@ module Telegram # because `data` param is controlled by client. def action_for_callback_query context, new_data = context_from_callback_query - # binding.pry if context action_name = "#{context}_callback_query" [false, action_name, [new_data]] if action_method?(action_name) diff --git a/spec/telegram/bot/updates_controller_spec.rb b/spec/telegram/bot/updates_controller_spec.rb index a24d021..0491866 100644 --- a/spec/telegram/bot/updates_controller_spec.rb +++ b/spec/telegram/bot/updates_controller_spec.rb @@ -108,7 +108,7 @@ RSpec.describe Telegram::Bot::UpdatesController do end %w(message channel_post).each do |type| - context 'when payload is edited_message' do + context "when payload is edited_#{type}" do let(:payload_type) { "edited_#{type}" } it { should eq [false, payload_type, [payload]] } end @@ -142,6 +142,22 @@ RSpec.describe Telegram::Bot::UpdatesController do end end end + + custom_payload_types = %w( + message + edited_message + channel_post + edited_channel_post + inline_query + chosen_inline_result + callback_query + ) + (described_class::PAYLOAD_TYPES - custom_payload_types).each do |type| + context "when payload is #{type}" do + let(:payload_type) { type } + it { should eq [false, payload_type, [payload]] } + end + end end context 'when `update` is a virtus model' do