rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.77k stars 1.22k forks source link

TelegramLongPollingCommandBot: automatically register commands #1102

Open centralhardware opened 2 years ago

centralhardware commented 2 years ago

Is your feature request related to a problem? Please describe.

it's to complicated to update command list via botFather every time when anyone change source code.

Describe the solution you'd like

automatically update command list via https://core.telegram.org/bots/api#setmycommands for all registered commands.

Describe alternatives you've considered

- Additional context Add any other context or screenshots about the feature request here.

Chase22 commented 2 years ago

I'd set this as a separate option or method on the command register. Not automatically since this would just override any settings made by the user.

centralhardware commented 2 years ago

TelegramLongPollingCommandBot doesn't allow to extend any register methods. IMHO it would be extremely helpfull if user can change behavior of TelegramLongPollingCommandBot

Chase22 commented 2 years ago

I'm working on implementing this as a separate method that can be called from the CommandBot. Basically after you register all your commands, call this method once to sync them all to telegram.

VEINHORN commented 1 year ago

@centralhardware I use telegrambots-spring-boot-starter module and automatically register all commands and update their description for the menu button like in the code below:

@Slf4j
@Component
public class WordifyTelegramBot extends TelegramLongPollingCommandBot {
    // ...    

    @PostConstruct
    public void registerCommands() {
        var commands = context.getBeansOfType(IBotCommand.class).values();

        var botCommands = commands
                .stream()
                .collect(Collectors.toMap(IBotCommand::getCommandIdentifier, IBotCommand::getDescription))
                .entrySet()
                .stream()
                .map(entry -> new BotCommand(entry.getKey(), entry.getValue()))
                .toList();

        var setCommands = SetMyCommands
                .builder()
                .commands(botCommands)
                .build();

        try {
            execute(setCommands);
        } catch (TelegramApiException e) {
            log.error("Couldn't update commands for the menu button", e);
        }

        registerAll(commands.toArray(IBotCommand[]::new));
    }

    // ...
}