Module: Telegram::Bot::UpdatesController::Commands
- Included in:
- Telegram::Bot::UpdatesController
- Defined in:
- lib/telegram/bot/updates_controller/commands.rb
Overview
Support for parsing commands
Constant Summary collapse
- CMD_REGEX =
%r{\A/([a-z\d_]{,31})(@(\S+))?(\s|$)}i.freeze
Class Method Summary collapse
-
.command_from_text(text, username = nil) ⇒ Object
Fetches command from text message.
Instance Method Summary collapse
-
#action_for_command(cmd) ⇒ Object
Override it to filter or transform commands.
-
#action_for_message ⇒ Object
(also: #action_for_channel_post)
If payload is a message with command, then returned action is an action for this command.
Class Method Details
.command_from_text(text, username = nil) ⇒ Object
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.
16 17 18 19 20 21 22 |
# File 'lib/telegram/bot/updates_controller/commands.rb', line 16 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 |
Instance Method Details
#action_for_command(cmd) ⇒ Object
Override it to filter or transform commands. Default implementation is to downcase and add ‘!` suffix.
27 28 29 |
# File 'lib/telegram/bot/updates_controller/commands.rb', line 27 def action_for_command(cmd) "#{cmd.downcase}!" end |
#action_for_message ⇒ Object Also known as: action_for_channel_post
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.
36 37 38 39 40 |
# File 'lib/telegram/bot/updates_controller/commands.rb', line 36 def cmd, args = Commands.command_from_text(payload['text'], bot_username) return unless cmd [[action_for_command(cmd), {type: :command, command: cmd}], args] end |