Limit Access to a Telegram Bot

Question:

I’ve written a telegram bot using aiogram. I want to restrict it so some certain users can access it. I’ve read this question, which has answers for different telegram bot libraries. But for the aiogram, the only solution I’ve found is to add an "if condition" that checks the sender’s user id and responds with the proper text. for example:

allowed_ids = [111111,2222222,3333333,4444444,5555555]

def handle(msg):
    sender = msg.from_user['id']
    if sender in allowed_ids:
       [...]
    else:
       bot.sendMessage(chat_id, 'Forbidden access!')
       bot.sendMessage(chat_id, sender)

The problem with this solution is that I have to check the sender’s id for each event! and I’ve got like 10 different message_handler for different commands and states. so this would lead to 10 similar if checks. isn’t there any easier way to do this?

I’ve found an optional filters_factory argument in dispatcher’s constructor, is that the correct way to do this? if so, how should I use it?
Thanks

Asked By: Danialz

||

Answers:

I’ve used handler which is triggered for all message content types as first handler and check message.from_user.id in it:

acl = (111111111,)

admin_only = lambda message: message.from_user.id not in acl


@dp.message_handler(admin_only, content_types=['any'])
async def handle_unwanted_users(message: types.Message):
    await config.bot.delete_message(message.chat.id, message.message_id)
    return

After deleting that message bot does nothing if user id not in acl. The order of handlers is important. This one should be the first

Answered By: Nafscript