rubenlagus / TelegramBots

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

Listing commands with /{nameOfTheCommand} #436

Closed GhsVilela closed 6 years ago

GhsVilela commented 6 years ago

I want to type /commands and then it return the message like this:

captura de tela de 2018-04-16 16-10-46

with the /

bvn13 commented 6 years ago

it is implemented in ability bot. use it.

GhsVilela commented 6 years ago

yes there is a command called commands that use the ability function inside AbilityBot class but it list the commands without the /

e-magon commented 6 years ago

You have to use BotFather, you can set which commands the bot accepts and the description of every one of it

GhsVilela commented 6 years ago

i don't think any of you guys understood what i want but ok. I wanna use the API in Java, there is nothing related to bot father.

e-magon commented 6 years ago

I think you have to implement it yourself then, using the normal sendMessage and building your own message

GhsVilela commented 6 years ago

For what i have tested the Abilities API recognize every method with public Ability nameOfTheCommand as a command and if you type /commands it list all commands in your java code but it list the commands without the /.

The function that list the commands is native of abilities API. I want to know if there is a way to edit the native method that list the commands.

AzZureman commented 6 years ago

The function that list the commands is native of abilities API

Ahah. @GhsVilela, native functions are functions such as java.lang.Object.hashCode(). If you dive into the sources you will see something like public native int hashCode(); without any implementations, and this is a real native function. And the function that list the commands (public Ability reportCommands()) even has a public access modifier! So, you just can @Override it with your own implementation. But, you can't override this with any working solutions, because you must have access to Map<String, Ability> abilities L119 from your extended class.

Since this point the issue is start.

@rubenlagus So, there are two ways to solve this issue:

  1. Change access modifier of Map<String, Ability> abilities from private to protected AzZureman/TelegramBots@faf4885bb8dc1a8104ac1ef229dc1ed9cc311c66 That will provide access to override public Ability reportCommands() method.

  2. Put the format function in a separate method with protected access modifier AzZureman/TelegramBots@b8b06f1d1d98fe109befc27aa23902a20b41393c That will provide an opportunity to change the command's output line. For example:

    @Override
    public String formatCommandReport(String name, String info) {
        return format("/%s  - %s", name, info);
    }
AzZureman commented 6 years ago

I think the first option from my suggestion is preferable, because it will give opportunity to filter output based on user privileges or may be something more intresting =)

GhsVilela commented 6 years ago

Thanks, you saved me haha. I will try to do that these days, if i have any problems I'll comment here again.

addo47 commented 6 years ago

Hey there everyone. Since a few have weighed in and I'm the creator/maintainer of the AbilityBot module, I figured I'll give my 2 cents on this one.

The original intent of the /commands ability method is to immediately update the commands reported by Telegram by forwarding the message result. That is, if you update/add/remove an ability, forwarding the results of /commands to BotFather would sync the commands that users see when they prompt the bot via the slash character. BotFather requires the format to be: command1 - info1 command2 - info2

Adding a slash would invalidate the commands abilities and its original purpose. Thank you for the proposed solutions @AzZureman, here's my feedback.

Change access modifier of Map<String, Ability> abilities from private to protected. That will provide access to override public Ability reportCommands() method.

This map is very sensitive as it is used and referenced every time an update is ingested. While changing the access modifier would allow for a broader flexibility, it also means less security since the map structure at this stage is mutable.

Put the format function in a separate method with protected access modifier. That will provide an opportunity to change the command's output line.

This is a neat solution. If this solution is implemented solely, then the user will be bound to use /commands for the formatted report. It does solve the issue of having the reporting format modifiable, but it doesn't extend the functionality.

The ideal solution is to add a public formatting method that is "overridable" and have the abilities map become protected and immutable. This way we allow the user to look at the content, add some other unique abilities that might require this visibility, but restrict any privilege to sabotage the ability registry.

GhsVilela commented 6 years ago

Those changes are not in origin master yet? I can't use then.

AzZureman commented 6 years ago

@addo37, I'm working on a solution. And what i see: claim command - what the jokes? I think privacy of this command must be changed to CREATOR. =)

AzZureman commented 6 years ago

@addo37, I think that this solution will satisfy you. The access modifier of abilities is still unchanged. If somebody needs to get abilities registry, then they can get its copy by calling getAbilitiesCopy() method (that will restrict any privilege to sabotage the ability registry).

Also, since opportunity of forwarding message from reportCommands ability to BotFather is necessary only for the bot's creator, then it was modified:

So now only creator can use the report command and only in private chat with the bot. That looks like this: Report result

So creator can easily view all bot's abilities and forward public commands to BotFather (only those that have an info)

Also added abilities publicCommands(), /commands, groupAdminCommands(), /acommands, adminCommands(), /gacommands, with appropriate privacies, for outputting commands, that have an info existing (for all users, for admins and for group admins).

Also, since claim ability is useful only for the creator, then it's privacy changed to CREATOR. (the claim-ban feature is removed)

addo47 commented 6 years ago

Thank you all for your suggestions. A PR has been initiated. You can view it at #466.

GhsVilela commented 6 years ago

when i'll be able to use those changes?

addo47 commented 6 years ago

On next release, @GhsVilela. It has been merged.