module Telegram::Bot::UpdatesController::Commands

Support for parsing commands

Constants

CMD_REGEX

Public Class Methods

command_from_text(text, username = nil) click to toggle source

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.

# 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

Public Instance Methods

action_for_channel_post()
Alias for: action_for_message
action_for_command(cmd) click to toggle source

Override it to filter or transform commands. Default implementation is to downcase and add `!` suffix.

# File lib/telegram/bot/updates_controller/commands.rb, line 25
def action_for_command(cmd)
  "#{cmd.downcase}!"
end
action_for_message() click to toggle source

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.

# 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
Also aliased as: action_for_channel_post