1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-09-07 19:35:52 +03:00

Controller#respond_with to reply without reply_to_message_id

Этот коммит содержится в:
Max Melentiev
2016-06-02 20:51:24 +03:00
родитель 7393b1a1f3
Коммит 0205891985
6 изменённых файлов: 64 добавлений и 46 удалений

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

@@ -131,9 +131,10 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
# There are `chat` & `from` shortcut methods. # There are `chat` & `from` shortcut methods.
response = from ? "Hello #{from['username']}!" : 'Hi there!' response = from ? "Hello #{from['username']}!" : 'Hi there!'
# There is `reply_with` helper to set basic fields # There is `respond_with` helper to set `chat_id` from received message:
# like `reply_to_message` & `chat_id`. respond_with :message, text: response
reply_with :message, text: response # `reply_with` also sets `reply_to_message_id`:
reply_with :photo, photo: File.open('party.jpg')
end end
private private
@@ -188,7 +189,7 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
end end
def read def read
reply_with :message, text: session[:text] respond_with :message, text: session[:text]
end end
private private
@@ -214,23 +215,23 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
def rename(*) def rename(*)
# set context for the next message # set context for the next message
save_context :rename save_context :rename
reply_with :message, text: 'What name do you like?' respond_with :message, text: 'What name do you like?'
end end
# register context handlers to handle this context # register context handlers to handle this context
context_handler :rename do |*words| context_handler :rename do |*words|
update_name words[0] update_name words[0]
reply_with :message, text: 'Renamed!' respond_with :message, text: 'Renamed!'
end end
# You can do it in other way: # You can do it in other way:
def rename(name = nil, *) def rename(name = nil, *)
if name if name
update_name name update_name name
reply_with :message, text: 'Renamed!' respond_with :message, text: 'Renamed!'
else else
save_context :rename save_context :rename
reply_with :message, text: 'What name do you like?' respond_with :message, text: 'What name do you like?'
end end
end end

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

@@ -21,7 +21,7 @@ module Telegram
# end # end
# #
# def help(*) # def help(*)
# reply_with :message, text: # respond_with :message, text:
# end # end
# #
# To process plain text messages (without commands) or other updates just # To process plain text messages (without commands) or other updates just
@@ -29,7 +29,7 @@ module Telegram
# as an argument. # as an argument.
# #
# def message(message) # def message(message)
# reply_with :message, text: "Echo: #{message['text']}" # respond_with :message, text: "Echo: #{message['text']}"
# end # end
# #
# def inline_query(query) # def inline_query(query)

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

@@ -35,13 +35,15 @@ module Telegram
end end
end end
def reply_with(type, *) %i(respond_with reply_with).each do |method|
Instrumentation.instrument(:reply_with, type: type) { super } define_method(method) do |type, *args|
Instrumentation.instrument(:respond_with, type: type) { super(type, *args) }
end
end end
%i(answer_callback_query answer_inline_query).each do |type| %i(answer_callback_query answer_inline_query).each do |type|
define_method(type) do |*args| define_method(type) do |*args|
Instrumentation.instrument(:reply_with, type: type) { super(*args) } Instrumentation.instrument(:respond_with, type: type) { super(*args) }
end end
end end

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

@@ -22,8 +22,8 @@ module Telegram
end end
end end
def reply_with(event) def respond_with(event)
info { "Replied with #{event.payload[:type]}" } info { "Responded with #{event.payload[:type]}" }
end end
def halted_callback(event) def halted_callback(event)

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

@@ -2,21 +2,23 @@ module Telegram
module Bot module Bot
class UpdatesController class UpdatesController
module ReplyHelpers module ReplyHelpers
# Helper to call bot's `send_#{type}` method with already set `chat_id` and # Helper to call bot's `send_#{type}` method with already set `chat_id`:
# `reply_to_message_id`:
# #
# reply_with :message, text: 'Hello!' # respond_with :message, text: 'Hello!'
# reply_with :message, text: '__Hello!__', parse_mode: :Markdown # respond_with :message, text: '__Hello!__', parse_mode: :Markdown
# reply_with :photo, photo: File.open(photo_to_send), caption: "It's incredible!" # respond_with :photo, photo: File.open(photo_to_send), caption: "It's incredible!"
def reply_with(type, params) def respond_with(type, params)
method = "send_#{type}"
chat = self.chat chat = self.chat
chat_id = chat && chat['id'] or raise 'Can not respond_with when chat is not present'
bot.public_send("send_#{type}", params.merge(chat_id: chat_id))
end
# Same as respond_with but also sets `reply_to_message_id`.
def reply_with(type, params)
payload = self.payload payload = self.payload
params = params.merge( message_id = payload && payload['message_id']
chat_id: (chat && chat['id'] or raise 'Can not reply_with when chat is not present'), params = params.merge(reply_to_message_id: message_id) if message_id
reply_to_message_id: payload && payload['message_id'], respond_with(type, params)
)
bot.public_send(method, params)
end end
# Same as reply_with, but for inline queries. # Same as reply_with, but for inline queries.

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

@@ -1,36 +1,49 @@
RSpec.describe Telegram::Bot::UpdatesController do RSpec.describe Telegram::Bot::UpdatesController do
include_context 'telegram/bot/updates_controller' include_context 'telegram/bot/updates_controller'
let(:params) { {arg: 1, 'other_arg' => 2} }
let(:respond_type) { :photo }
let(:result) { double(:result) }
let(:payload_type) { :message }
let(:payload) { {message_id: double(:message_id)} }
let(:chat) { {'id' => double(:chat_id)} }
shared_examples 'missing chat' do
context 'when chat is missing' do
let(:payload_type) { :some_type }
it { expect { subject }.to raise_error(/chat/) }
end
end
describe '#respond_with' do
subject { controller.respond_with respond_type, params }
include_examples 'missing chat'
it 'sets chat_id & reply_to_message_id' do
expect(controller).to receive(:chat) { chat }
expect(bot).to receive("send_#{respond_type}").
with(params.merge(chat_id: chat['id'])) { result }
should eq result
end
end
describe '#reply_with' do describe '#reply_with' do
subject { controller.reply_with type, params } subject { controller.reply_with respond_type, params }
let(:params) { {arg: 1, 'other_arg' => 2} } include_examples 'missing chat'
let(:type) { :photo }
let(:result) { double(:result) }
let(:payload_type) { :message }
let(:payload) { {message_id: double(:message_id)} }
let(:chat) { {'id' => double(:chat_id)} }
it 'sets chat_id & reply_to_message' do it 'sets chat_id & reply_to_message_id' do
expect(controller).to receive(:chat) { chat } expect(controller).to receive(:chat) { chat }
expect(bot).to receive("send_#{type}").with(params.merge( expect(bot).to receive("send_#{respond_type}").with(params.merge(
chat_id: chat['id'], chat_id: chat['id'],
reply_to_message_id: payload[:message_id], reply_to_message_id: payload[:message_id],
)) { result } )) { result }
should eq result should eq result
end end
context 'when chat is missing' do
let(:payload_type) { :some_type }
it { expect { subject }.to raise_error(/chat/) }
end
context 'when update is not set' do context 'when update is not set' do
let(:update) { {chat: chat} } let(:update) { {chat: chat} }
it 'sets chat_id & reply_to_message' do it 'sets chat_id' do
expect(bot).to receive("send_#{type}").with(params.merge( expect(bot).to receive("send_#{respond_type}").
chat_id: chat['id'], with(params.merge(chat_id: chat['id'])) { result }
reply_to_message_id: nil,
)) { result }
should eq result should eq result
end end
end end