Skip to main content

BotCommands

Trait BotCommands 

Source
pub trait BotCommands: Sized {
    // Required methods
    fn parse(s: &str, bot_username: &str) -> Result<Self, ParseError>;
    fn descriptions() -> CommandDescriptions<'static>;
    fn bot_commands() -> Vec<BotCommand>;
}
Expand description

An enumeration of bot’s commands.

§Example

use teloxide::utils::command::BotCommands;

type UnitOfTime = u8;

#[derive(BotCommands, PartialEq, Debug)]
#[command(rename_rule = "lowercase", parse_with = "split")]
enum AdminCommand {
    Mute(UnitOfTime, char),
    Ban(UnitOfTime, char),
}

let command = AdminCommand::parse("/ban 5 h", "bot_name").unwrap();
assert_eq!(command, AdminCommand::Ban(5, 'h'));

§Enum attributes

  1. #[command(rename_rule = "rule")] Rename all commands by rule. Allowed rules are lowercase, UPPERCASE, PascalCase, camelCase, snake_case, SCREAMING_SNAKE_CASE, kebab-case, and SCREAMING-KEBAB-CASE.

  2. #[command(prefix = "prefix")] Change a prefix for all commands (the default is /).

  3. #[command(description = "description")] and /// description Add a summary description of commands before all commands.

  4. #[command(parse_with = "parser")] Change the parser of arguments. Possible values:

    • default - the same as the unspecified parser. It only puts all text after the first space into the first argument, which must implement FromStr.

§Example

use teloxide::utils::command::BotCommands;

#[derive(BotCommands, PartialEq, Debug)]
#[command(rename_rule = "lowercase")]
enum Command {
    Text(String),
}

let command = Command::parse("/text hello my dear friend!", "").unwrap();
assert_eq!(command, Command::Text("hello my dear friend!".to_string()));
  • split - separates a message by a given separator (the default is the space character) and parses each part into the corresponding arguments, which must implement FromStr.

§Example

use teloxide::utils::command::BotCommands;

#[derive(BotCommands, PartialEq, Debug)]
#[command(rename_rule = "lowercase", parse_with = "split")]
enum Command {
    Nums(u8, u16, i32),
}

let command = Command::parse("/nums 1 32 -5", "").unwrap();
assert_eq!(command, Command::Nums(1, 32, -5));
  1. #[command(separator = "sep")] Specify separator used by the split parser. It will be ignored when accompanied by another type of parsers.

§Example

use teloxide::utils::command::BotCommands;

#[derive(BotCommands, PartialEq, Debug)]
#[command(rename_rule = "lowercase", parse_with = "split", separator = "|")]
enum Command {
    Nums(u8, u16, i32),
}

let command = Command::parse("/nums 1|32|5", "").unwrap();
assert_eq!(command, Command::Nums(1, 32, 5));
  1. #[command(command_separator = "sep")] Specify separator between command and args. Default is a space character.

§Example

use teloxide::utils::command::BotCommands;

#[derive(BotCommands, PartialEq, Debug)]
#[command(
    rename_rule = "lowercase",
    parse_with = "split",
    separator = "_",
    command_separator = "_"
)]
enum Command {
    Nums(u8, u16, i32),
}

let command = Command::parse("/nums_1_32_5", "").unwrap();
assert_eq!(command, Command::Nums(1, 32, 5));

§Variant attributes

All variant attributes override the corresponding enum attributes.

  1. #[command(rename_rule = "rule")] Rename one command by a rule. Allowed rules are lowercase, UPPERCASE, PascalCase, camelCase, snake_case, SCREAMING_SNAKE_CASE, kebab-case, SCREAMING-KEBAB-CASE.

  2. #[command(rename = "name")] Rename one command to name (literal renaming; do not confuse with rename_rule).

  3. #[command(description = "description")] and /// description Give your command a description. It will be shown in the help message.

  4. #[command(parse_with = "parser")] Parse arguments of one command with a given parser. parser must be a function of the signature fn(String) -> Result<Tuple, ParseError>, where Tuple corresponds to the variant’s arguments.

  5. #[command(hide)] Hide a command from the help message. It will still be parsed.

  6. #[command(alias = "alias")] Add an alias to a command. It will be shown in the help message.

  7. #[command(aliases = ["alias1", "alias2"])] Add multiple aliases to a command. They will be shown in the help message.

  8. #[command(hide_aliases)] Hide all aliases of a command from the help message.

§Example

use teloxide::utils::command::{BotCommands, ParseError};

fn accept_two_digits(input: String) -> Result<(u8,), ParseError> {
    match input.len() {
        2 => {
            let num = input.parse::<u8>().map_err(|e| ParseError::IncorrectFormat(e.into()))?;
            Ok((num,))
        }
        len => Err(ParseError::Custom(format!("Only 2 digits allowed, not {}", len).into())),
    }
}

#[derive(BotCommands, PartialEq, Debug)]
#[command(rename_rule = "lowercase")]
enum Command {
    #[command(parse_with = accept_two_digits)]
    Num(u8),
}

let command = Command::parse("/num 12", "").unwrap();
assert_eq!(command, Command::Num(12));
let command = Command::parse("/num 333", "");
assert!(command.is_err());
  1. #[command(prefix = "prefix")]
  2. #[command(separator = "sep")]

These attributes just override the corresponding enum attributes for a specific variant.

Required Methods§

Source

fn parse(s: &str, bot_username: &str) -> Result<Self, ParseError>

Parses a command.

bot_username is required to parse commands like /cmd@username_of_the_bot.

Source

fn descriptions() -> CommandDescriptions<'static>

Returns descriptions of the commands suitable to be shown to the user (for example when /help command is used).

Source

fn bot_commands() -> Vec<BotCommand>

Returns a vector of BotCommand that can be used with set_my_commands.

The derived implementation uses unprefixed names, omits hidden commands and names outside [a-z0-9_]{1,32}, and does not register aliases. Descriptions are trimmed and limited to 256 characters. If a description has fewer than three characters, it uses the command name (or Run NAME for names shorter than three characters). This does not affect parsing or descriptions. Telegram’s command menu sends commands with /, regardless of any custom parsing prefix.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§