TelegramBots icon indicating copy to clipboard operation
TelegramBots copied to clipboard

Need capability to restrict access of bots for some users based on userid

Open arishdhingra opened this issue 3 years ago • 10 comments

Need a capability either via API or file read so that we can restrict the access based on users ids. I don't want a set of users to invoke bots. @rubenlagus

arishdhingra avatar Oct 24 '21 03:10 arishdhingra

I think I can pick this up and work on it if the admin is okay. Is there a process to get approval to work on this one?

bhati3 avatar Oct 24 '21 03:10 bhati3

@rubenlagus I'd like to pick this up

bhati3 avatar Oct 31 '21 21:10 bhati3

Not sure what exactly is needed here, any bot can implement the necessary filters for users in their onUpdateReceived method

rubenlagus avatar Oct 31 '21 21:10 rubenlagus

@arishdhingra A simple example for this:

 final static long[] ALLOWED_CHATS = {0L, 0L...};
    
    static boolean isChatAllowed(final long chatId) {
        for (final long e : ALLOWED_CHATS)
            if (e == chatId)
                return true;

        return false;
    }

    @Override
    public void onUpdateReceived(Update update) {
        ...
        if (!isChatAllowed(update.getMessage().getChatId()))
            return;
        
        ...
    }
...





fulopm avatar Dec 16 '21 22:12 fulopm

I'd not implement this in the library. The telegram api has no way of blocking users and it's in my opinion no task of this library to implement it. This can be trivially implement by the bot itself as shown by @fulopm

Chase22 avatar Dec 16 '21 22:12 Chase22

@fulopm correct me if I'm wrong, but when in a group chat context, update.getMessage().getChatId() will return the ID of the group, not the user that send the message/command?

Not sure if the ticket author meant in a group context or not.

It seems that the telegram API exposes an (optional) user object that might be useful for a bot implementer to block users from invoking bot commands in a group chat context (assuming the User object is available then).

See https://core.telegram.org/bots/api#message

Currently, it does not seem that this attribute is exposed in Java API however (version 5.7.1).

demaniak avatar Feb 25 '22 16:02 demaniak

@demaniak Yes, I believe you're right. I don't use the code I posted above to filter members of a group, it allows everyone from that specific group.

Btw, I think you can test it by printing out the Message instance coming from a group message. Check your id by texting @userinfobot on Telegram. Search for it in the stringified Message instance in your logs, and you'll see if it contains the ID of the actual sender user or not.

fulopm avatar Feb 25 '22 17:02 fulopm

@fulopm correct me if I'm wrong, but when in a group chat context, update.getMessage().getChatId() will return the ID of the group, not the user that send the message/command?

Not sure if the ticket author meant in a group context or not.

It seems that the telegram API exposes an (optional) user object that might be useful for a bot implementer to block users from invoking bot commands in a group chat context (assuming the User object is available then).

See https://core.telegram.org/bots/api#message

Currently, it does not seem that this attribute is exposed in Java API however (version 5.7.1).

It is exposed here.

And for OP, this library contain a module called AbilityBot. This framework gives you features out-of-the-box like /ban and /unban users from using your bot. Take a look at the wiki intros and links.

addo47 avatar Feb 25 '22 19:02 addo47

@fulopm correct me if I'm wrong, but when in a group chat context, update.getMessage().getChatId() will return the ID of the group, not the user that send the message/command? Not sure if the ticket author meant in a group context or not. It seems that the telegram API exposes an (optional) user object that might be useful for a bot implementer to block users from invoking bot commands in a group chat context (assuming the User object is available then). See https://core.telegram.org/bots/api#message Currently, it does not seem that this attribute is exposed in Java API however (version 5.7.1).

It is exposed here.

And for OP, this library contain a module called AbilityBot. This framework gives you features out-of-the-box like /ban and /unban users from using your bot. Take a look at the wiki intros and links.

Thanks @addo47 , I do NOT know how I missed that!

demaniak avatar Feb 28 '22 11:02 demaniak

you can write something likes like https://github.com/rubenlagus/TelegramBots/blob/master/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/timedbot/TimedSendLongPollingBot.java

centralhardware avatar Apr 17 '22 05:04 centralhardware