1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-08-28 15:26:18 +03:00

New API methods and payload types (up to Bot API 3.2)

Этот коммит содержится в:
Max Melentiev
2017-08-11 14:21:59 +03:00
родитель 88a4a9aab3
Коммит aaba27eb70
4 изменённых файлов: 55 добавлений и 11 удалений

Просмотреть файл

@@ -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

Просмотреть файл

@@ -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

Просмотреть файл

@@ -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)

Просмотреть файл

@@ -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