зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-04 02:05:52 +03:00
Use bang-methods as actions for commands
Этот коммит содержится в:
@@ -17,8 +17,12 @@ module Telegram
|
||||
context, new_data = context_from_callback_query
|
||||
if context
|
||||
action_name = "#{context}_callback_query"
|
||||
[false, action_name, [new_data]] if action_method?(action_name)
|
||||
end || super
|
||||
if action_method?(action_name)
|
||||
action_options = {type: :callback_query_context, context: context}
|
||||
return [[action_name, action_options], [new_data]]
|
||||
end
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
def context_from_callback_query
|
||||
|
||||
44
lib/telegram/bot/updates_controller/commands.rb
Обычный файл
44
lib/telegram/bot/updates_controller/commands.rb
Обычный файл
@@ -0,0 +1,44 @@
|
||||
module Telegram
|
||||
module Bot
|
||||
class UpdatesController
|
||||
# Support for parsing commands
|
||||
module Commands
|
||||
CMD_REGEX = %r{\A/([a-z\d_]{,31})(@(\S+))?(\s|$)}i
|
||||
|
||||
class << self
|
||||
# Fetches command from text message. All subsequent words are returned
|
||||
# as arguments.
|
||||
# If command has mention (eg. `/test@SomeBot`), it returns commands only
|
||||
# for specified username. Set `username` to `true` to accept
|
||||
# any commands.
|
||||
def command_from_text(text, username = nil)
|
||||
return unless text
|
||||
match = text.match(CMD_REGEX)
|
||||
return unless match
|
||||
mention = match[3]
|
||||
[match[1], text.split.drop(1)] if username == true || !mention || mention == username
|
||||
end
|
||||
end
|
||||
|
||||
# Override it to filter or transform commands.
|
||||
# Default implementation is to downcase and add `!` suffix.
|
||||
def action_for_command(cmd)
|
||||
"#{cmd.downcase}!"
|
||||
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 = Commands.command_from_text(payload['text'], bot_username)
|
||||
return unless cmd
|
||||
[[action_for_command(cmd), type: :command, command: cmd], args]
|
||||
end
|
||||
|
||||
alias_method :action_for_channel_post, :action_for_message
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -48,7 +48,7 @@ module Telegram
|
||||
end
|
||||
|
||||
# Action to clear context.
|
||||
def cancel
|
||||
def cancel!
|
||||
# Context is already cleared in action_for_message
|
||||
end
|
||||
|
||||
@@ -71,7 +71,10 @@ module Telegram
|
||||
@context = val && val.to_sym
|
||||
super || context && begin
|
||||
handler = handler_for_context
|
||||
[true, handler, payload['text'].try!(:split) || []] if handler
|
||||
if handler
|
||||
action_options = {type: :message_context, context: context}
|
||||
[[handler, action_options], payload['text'].try!(:split) || []]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user