MajMcCloud / TelegramBotFramework

This is a context based application framework for the C# TelegramBot library.
https://www.t.me/tgbotbase
MIT License
149 stars 43 forks source link

Keyboard Markup #37

Closed bretbas closed 1 year ago

bretbas commented 1 year ago

How can i handle message when press on keyboard button?

MajMcCloud commented 1 year ago

Which kind of keyboard are you using ? The answer depends on it. If using the ButtonGrid control it does manage everything for you.

bretbas commented 1 year ago

But i want to handle this pressing in the Action method. How can i to do that?

MajMcCloud commented 1 year ago

Which kind of Keyboard are you using ? The Reply one or the Inline one? You have to use the InlineReplyKeboard to get the Action method invoked. Check Readme and Telegram documentaion.

bretbas commented 1 year ago

Ok, but i need Reply keyboard. I try this:

public class StartForm : AutoCleanForm
{
    public StartForm()
    {
        Init += OnInit;
    }

    private async Task OnInit(object sender, InitEventArgs ev)
    {
        var btn = new ButtonForm();
        btn.AddButtonRow(new ButtonBase(KeyboardButtons.SearchTrips, Commands.SearchTrips));
        btn.AddButtonRow(new ButtonBase(KeyboardButtons.SetFilters, Commands.SetFilters));
        btn.AddButtonRow(new ButtonBase(KeyboardButtons.CreateTrip, Commands.CreateTrip));

        var grid = new ButtonGrid(btn)
        {
            KeyboardType = TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard,
            Enabled = true,
            HideKeyboardOnCleanup = true,
            ResizeKeyboard = true
        };

        grid.ButtonClicked += async (s, e) =>
        {
            FormBase form = e.Button.Value switch
            {
                Commands.SearchTrips => new SearchTripsForm(),
                Commands.SetFilters => new SetFiltersForm(),
                Commands.CreateTrip => new CreateTripForm()
            };
            await NavigateTo(form);
        };

        AddControl(grid);
    }
}
  1. Is it a good practice?
  2. Where is it better to unsubscribe from ButtonClicked or is it not needed?
MajMcCloud commented 1 year ago

The main reason, why it didnt worked in the first place was that ReplyKeyboard does only send "Text" back. So like a normal chat message. Thats why the Action method did not get invoked.

  1. I would say that looks fine.
  2. You dont have to, but you can add it to OnClosed event so i gets unsubscribed when navigated somewhere else