TelegramBotBoilerplate

Flexible, session-based and documentated console telegram bot boilerplate.

View project on GitHub

How to use command argument parser?

Command arguments are the text parts that the user passes with a command:

Command example

In TelegramBotBoilerplate command argument parsing is built-in with simple utility.

Step-by-step Guide

  1. Find Run() method in the command that you want to enhance with arguments:
        public override string Run(Context context, CancellationToken cancellationToken)
        {
            return "Coming soon...";
        }
  1. Use ArgumentParser to validate message text for some number of arguments:
        public override string Run(Context context, CancellationToken cancellationToken)
        {
            var message = context.Update.Message;
            if (!ArgumentParser.Validate()) {
               return "no arguments provided";
            }
            if (ArgumentParser.Validate(message.Text, 1)) {
               return "one argument provided";
            }
            if (ArgumentParser.Validate(message.Text, 2, 4)) {
               return "Minimum 2 and maximum 4 arguments provided";
            }
            if (ArgumentParser.Validate(message.Text, 5, CommandParserValidateOptions.EqualsOrGreater)) {
               return "five or more arguments provided";
            }
            return "This return will never emit.";
        }
  1. Use ArgumentParser to parse message text and make some business logic with ParsedCommand object:
        public override string Run(Context context, CancellationToken cancellationToken)
        {
            var message = context.Update.Message;
            if (!ArgumentParser.Validate()) {
               return "no arguments provided";
            }
            ParsedCommand args = ArgumentParser.Parse(message.Text);
            if (ArgumentParser.Validate(message.Text, 1)) {
               return $"one argument provided - {args.Arguments[0]}";
            }
            if (ArgumentParser.Validate(message.Text, 2, 4)) {
               return $"Minimum 2 and maximum 4 arguments provided ({args.Arguments.Length})";
            }
            if (ArgumentParser.Validate(message.Text, 5, CommandParserValidateOptions.EqualsOrGreater)) {
               return $"five or more arguments provided - \"{args.ArgumentsText}\"";
            }
            return "This return will never emit.";
        }
  1. Complete! Now you can start your bot and test your new command with arguments!