TelegramBots
TelegramBots copied to clipboard
Need capability to restrict access of bots for some users based on userid
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
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?
@rubenlagus I'd like to pick this up
Not sure what exactly is needed here, any bot can implement the necessary filters for users in their onUpdateReceived method
@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;
...
}
...
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
@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 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 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.
@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!
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