зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-05 02:35:50 +03:00
New API methods and payload types (up to Bot API 3.2)
Этот коммит содержится в:
@@ -5,7 +5,7 @@ require 'active_support/core_ext/hash/keys'
|
|||||||
|
|
||||||
module Telegram
|
module Telegram
|
||||||
module Bot
|
module Bot
|
||||||
class Client
|
class Client # rubocop:disable ClassLength
|
||||||
URL_TEMPLATE = 'https://api.telegram.org/bot%s/'.freeze
|
URL_TEMPLATE = 'https://api.telegram.org/bot%s/'.freeze
|
||||||
|
|
||||||
autoload :TypedResponse, 'telegram/bot/client/typed_response'
|
autoload :TypedResponse, 'telegram/bot/client/typed_response'
|
||||||
@@ -70,6 +70,8 @@ module Telegram
|
|||||||
setWebhook
|
setWebhook
|
||||||
|
|
||||||
answerCallbackQuery
|
answerCallbackQuery
|
||||||
|
deleteChatPhoto
|
||||||
|
exportChatInviteLink
|
||||||
forwardMessage
|
forwardMessage
|
||||||
getChat
|
getChat
|
||||||
getChatAdministrators
|
getChatAdministrators
|
||||||
@@ -80,6 +82,9 @@ module Telegram
|
|||||||
getUserProfilePhotos
|
getUserProfilePhotos
|
||||||
kickChatMember
|
kickChatMember
|
||||||
leaveChat
|
leaveChat
|
||||||
|
pinChatMessage
|
||||||
|
promoteChatMember
|
||||||
|
restrictChatMember
|
||||||
sendAudio
|
sendAudio
|
||||||
sendChatAction
|
sendChatAction
|
||||||
sendContact
|
sendContact
|
||||||
@@ -87,18 +92,35 @@ module Telegram
|
|||||||
sendLocation
|
sendLocation
|
||||||
sendMessage
|
sendMessage
|
||||||
sendPhoto
|
sendPhoto
|
||||||
sendSticker
|
|
||||||
sendVenue
|
sendVenue
|
||||||
sendVideo
|
sendVideo
|
||||||
|
sendVideoNote
|
||||||
sendVoice
|
sendVoice
|
||||||
|
setChatDescription
|
||||||
|
setChatPhoto
|
||||||
|
setChatTitle
|
||||||
unbanChatMember
|
unbanChatMember
|
||||||
|
unpinChatMessage
|
||||||
|
|
||||||
|
deleteMessage
|
||||||
editMessageCaption
|
editMessageCaption
|
||||||
editMessageReplyMarkup
|
editMessageReplyMarkup
|
||||||
editMessageText
|
editMessageText
|
||||||
|
|
||||||
|
sendSticker
|
||||||
|
getStickerSet
|
||||||
|
uploadStickerFile
|
||||||
|
createNewStickerSet
|
||||||
|
addStickerToSet
|
||||||
|
setStickerPositionInSet
|
||||||
|
deleteStickerFromSet
|
||||||
|
|
||||||
answerInlineQuery
|
answerInlineQuery
|
||||||
|
|
||||||
|
sendInvoice
|
||||||
|
answerShippingQuery
|
||||||
|
answerPreCheckoutQuery
|
||||||
|
|
||||||
getGameHighScores
|
getGameHighScores
|
||||||
sendGame
|
sendGame
|
||||||
setGameScore
|
setGameScore
|
||||||
|
|||||||
@@ -86,6 +86,8 @@ module Telegram
|
|||||||
inline_query
|
inline_query
|
||||||
chosen_inline_result
|
chosen_inline_result
|
||||||
callback_query
|
callback_query
|
||||||
|
shipping_query
|
||||||
|
pre_checkout_query
|
||||||
).freeze
|
).freeze
|
||||||
CMD_REGEX = %r{\A/([a-z\d_]{,31})(@(\S+))?(\s|$)}i
|
CMD_REGEX = %r{\A/([a-z\d_]{,31})(@(\S+))?(\s|$)}i
|
||||||
CONFLICT_CMD_REGEX = Regexp.new("^(#{PAYLOAD_TYPES.join('|')}|\\d)")
|
CONFLICT_CMD_REGEX = Regexp.new("^(#{PAYLOAD_TYPES.join('|')}|\\d)")
|
||||||
@@ -169,15 +171,21 @@ module Telegram
|
|||||||
# Returns array `[is_command?, action, args]`.
|
# Returns array `[is_command?, action, args]`.
|
||||||
def action_for_payload
|
def action_for_payload
|
||||||
if payload_type
|
if payload_type
|
||||||
send("action_for_#{payload_type}") || [false, payload_type, [payload]]
|
send("action_for_#{payload_type}") || action_for_default_payload
|
||||||
else
|
else
|
||||||
[false, :unsupported_payload_type, []]
|
[false, :unsupported_payload_type, []]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def action_for_default_payload
|
||||||
|
[false, payload_type, [payload]]
|
||||||
|
end
|
||||||
|
|
||||||
# If payload is a message with command, then returned action is an
|
# If payload is a message with command, then returned action is an
|
||||||
# action for this command.
|
# action for this command.
|
||||||
# Separate method, so it can be easily overriden (ex. MessageContext).
|
# 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
|
def action_for_message
|
||||||
cmd, args = self.class.command_from_text(payload['text'], bot_username)
|
cmd, args = self.class.command_from_text(payload['text'], bot_username)
|
||||||
cmd &&= self.class.action_for_command(cmd)
|
cmd &&= self.class.action_for_command(cmd)
|
||||||
@@ -185,12 +193,6 @@ module Telegram
|
|||||||
end
|
end
|
||||||
alias_method :action_for_channel_post, :action_for_message
|
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
|
def action_for_inline_query
|
||||||
[false, payload_type, [payload['query'], payload['offset']]]
|
[false, payload_type, [payload['query'], payload['offset']]]
|
||||||
end
|
end
|
||||||
@@ -208,6 +210,11 @@ module Telegram
|
|||||||
def action_missing(*)
|
def action_missing(*)
|
||||||
end
|
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)
|
ActiveSupport.run_load_hooks('telegram.bot.updates_controller', self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ module Telegram
|
|||||||
# because `data` param is controlled by client.
|
# because `data` param is controlled by client.
|
||||||
def action_for_callback_query
|
def action_for_callback_query
|
||||||
context, new_data = context_from_callback_query
|
context, new_data = context_from_callback_query
|
||||||
# binding.pry
|
|
||||||
if context
|
if context
|
||||||
action_name = "#{context}_callback_query"
|
action_name = "#{context}_callback_query"
|
||||||
[false, action_name, [new_data]] if action_method?(action_name)
|
[false, action_name, [new_data]] if action_method?(action_name)
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ RSpec.describe Telegram::Bot::UpdatesController do
|
|||||||
end
|
end
|
||||||
|
|
||||||
%w(message channel_post).each do |type|
|
%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}" }
|
let(:payload_type) { "edited_#{type}" }
|
||||||
it { should eq [false, payload_type, [payload]] }
|
it { should eq [false, payload_type, [payload]] }
|
||||||
end
|
end
|
||||||
@@ -142,6 +142,22 @@ RSpec.describe Telegram::Bot::UpdatesController do
|
|||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context 'when `update` is a virtus model' do
|
context 'when `update` is a virtus model' do
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user