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

Class Method Summary collapse

Instance Method Summary collapse

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.



14
15
16
17
18
19
20
# File 'lib/telegram/bot/updates_controller/commands.rb', line 14

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.



25
26
27
# File 'lib/telegram/bot/updates_controller/commands.rb', line 25

def action_for_command(cmd)
  "#{cmd.downcase}!"
end

#action_for_messageObject 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.



34
35
36
37
38
# File 'lib/telegram/bot/updates_controller/commands.rb', line 34

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