diff --git a/README.md b/README.md index 779b2a3..1b9b98e 100644 --- a/README.md +++ b/README.md @@ -131,9 +131,10 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController # There are `chat` & `from` shortcut methods. response = from ? "Hello #{from['username']}!" : 'Hi there!' - # There is `reply_with` helper to set basic fields - # like `reply_to_message` & `chat_id`. - reply_with :message, text: response + # There is `respond_with` helper to set `chat_id` from received message: + respond_with :message, text: response + # `reply_with` also sets `reply_to_message_id`: + reply_with :photo, photo: File.open('party.jpg') end private @@ -188,7 +189,7 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController end def read - reply_with :message, text: session[:text] + respond_with :message, text: session[:text] end private @@ -214,23 +215,23 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController def rename(*) # set context for the next message save_context :rename - reply_with :message, text: 'What name do you like?' + respond_with :message, text: 'What name do you like?' end # register context handlers to handle this context context_handler :rename do |*words| update_name words[0] - reply_with :message, text: 'Renamed!' + respond_with :message, text: 'Renamed!' end # You can do it in other way: def rename(name = nil, *) if name update_name name - reply_with :message, text: 'Renamed!' + respond_with :message, text: 'Renamed!' else save_context :rename - reply_with :message, text: 'What name do you like?' + respond_with :message, text: 'What name do you like?' end end diff --git a/lib/telegram/bot/updates_controller.rb b/lib/telegram/bot/updates_controller.rb index ae7528d..e6709ec 100644 --- a/lib/telegram/bot/updates_controller.rb +++ b/lib/telegram/bot/updates_controller.rb @@ -21,7 +21,7 @@ module Telegram # end # # def help(*) - # reply_with :message, text: + # respond_with :message, text: # end # # To process plain text messages (without commands) or other updates just @@ -29,7 +29,7 @@ module Telegram # as an argument. # # def message(message) - # reply_with :message, text: "Echo: #{message['text']}" + # respond_with :message, text: "Echo: #{message['text']}" # end # # def inline_query(query) diff --git a/lib/telegram/bot/updates_controller/instrumentation.rb b/lib/telegram/bot/updates_controller/instrumentation.rb index 89d0e1a..9337a52 100644 --- a/lib/telegram/bot/updates_controller/instrumentation.rb +++ b/lib/telegram/bot/updates_controller/instrumentation.rb @@ -35,13 +35,15 @@ module Telegram end end - def reply_with(type, *) - Instrumentation.instrument(:reply_with, type: type) { super } + %i(respond_with reply_with).each do |method| + define_method(method) do |type, *args| + Instrumentation.instrument(:respond_with, type: type) { super(type, *args) } + end end %i(answer_callback_query answer_inline_query).each do |type| define_method(type) do |*args| - Instrumentation.instrument(:reply_with, type: type) { super(*args) } + Instrumentation.instrument(:respond_with, type: type) { super(*args) } end end diff --git a/lib/telegram/bot/updates_controller/log_subscriber.rb b/lib/telegram/bot/updates_controller/log_subscriber.rb index 584a838..eb7be10 100644 --- a/lib/telegram/bot/updates_controller/log_subscriber.rb +++ b/lib/telegram/bot/updates_controller/log_subscriber.rb @@ -22,8 +22,8 @@ module Telegram end end - def reply_with(event) - info { "Replied with #{event.payload[:type]}" } + def respond_with(event) + info { "Responded with #{event.payload[:type]}" } end def halted_callback(event) diff --git a/lib/telegram/bot/updates_controller/reply_helpers.rb b/lib/telegram/bot/updates_controller/reply_helpers.rb index 958d201..9c00c7a 100644 --- a/lib/telegram/bot/updates_controller/reply_helpers.rb +++ b/lib/telegram/bot/updates_controller/reply_helpers.rb @@ -2,21 +2,23 @@ module Telegram module Bot class UpdatesController module ReplyHelpers - # Helper to call bot's `send_#{type}` method with already set `chat_id` and - # `reply_to_message_id`: + # Helper to call bot's `send_#{type}` method with already set `chat_id`: # - # reply_with :message, text: 'Hello!' - # reply_with :message, text: '__Hello!__', parse_mode: :Markdown - # reply_with :photo, photo: File.open(photo_to_send), caption: "It's incredible!" - def reply_with(type, params) - method = "send_#{type}" + # respond_with :message, text: 'Hello!' + # respond_with :message, text: '__Hello!__', parse_mode: :Markdown + # respond_with :photo, photo: File.open(photo_to_send), caption: "It's incredible!" + def respond_with(type, params) 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 - params = params.merge( - chat_id: (chat && chat['id'] or raise 'Can not reply_with when chat is not present'), - reply_to_message_id: payload && payload['message_id'], - ) - bot.public_send(method, params) + message_id = payload && payload['message_id'] + params = params.merge(reply_to_message_id: message_id) if message_id + respond_with(type, params) end # Same as reply_with, but for inline queries. diff --git a/spec/telegram/bot/updates_controller/reply_helpers_spec.rb b/spec/telegram/bot/updates_controller/reply_helpers_spec.rb index a2675f2..53537ca 100644 --- a/spec/telegram/bot/updates_controller/reply_helpers_spec.rb +++ b/spec/telegram/bot/updates_controller/reply_helpers_spec.rb @@ -1,36 +1,49 @@ RSpec.describe Telegram::Bot::UpdatesController do 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 - subject { controller.reply_with type, params } - let(:params) { {arg: 1, 'other_arg' => 2} } - let(:type) { :photo } - let(:result) { double(:result) } - let(:payload_type) { :message } - let(:payload) { {message_id: double(:message_id)} } - let(:chat) { {'id' => double(:chat_id)} } + subject { controller.reply_with respond_type, params } + include_examples 'missing chat' - it 'sets chat_id & reply_to_message' do + it 'sets chat_id & reply_to_message_id' do 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'], reply_to_message_id: payload[:message_id], )) { result } should eq result 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 let(:update) { {chat: chat} } - it 'sets chat_id & reply_to_message' do - expect(bot).to receive("send_#{type}").with(params.merge( - chat_id: chat['id'], - reply_to_message_id: nil, - )) { result } + it 'sets chat_id' do + expect(bot).to receive("send_#{respond_type}"). + with(params.merge(chat_id: chat['id'])) { result } should eq result end end