1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-09-06 02:55:50 +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 удалений

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

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

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

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

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

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

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

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