Skip to content

Creating your first command

Mikhail edited this page Jan 11, 2021 · 5 revisions

Creating your first command

Creating commands is the similar to handlers creating process, but but there is one thing: command method return type should be Task or ValueTask.

Let's define constant string which we will use as an identifier for all command handlers and command processor builder which is related to Message handling:

public string const MessagesCommandProcessorId = "Messages";

Example of Ping command:

[MessageCommandHandler("ping")]
public async Task Ping(ICommandContext<Message> commandContext)
{
    await commandContext.BotClient.SendTextMessageAsync(commandContext.Entity.Chat, "Pong!");
}

Where MessageCommandHandlerAttribute is:

public class MessageCommandHandlerAttribute : CommandHandlerAttributeBase
{
    public string CommandFormat { get; }

    public MessageCommandHandlerAttribute(string commandFormat) : base(MessagesCommandProcessorId, UpdateType.Message)
    {
        CommandFormat = commandFormat;
    }
}