F
F
flameyolo2022-01-28 08:03:27
Python
flameyolo, 2022-01-28 08:03:27

How to make whitelist for telegram bot using python aiogram?

It is required to make a whitelist for the users of my telegram bot, so that without me adding the user to the allowed list, the bot will answer him that he is not in the whitelist.

whitelist = []

@dp.message_handler(func=lambda message: message.chat.id not in whitelist)
def checker(message):
   bot.send_message(message.chat.id, "Вы не находитесь в Whitelist!")


I tried to do something on my knee, nothing works, it gives an error at the moment the bot is launched.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LXSTVAYNE, 2022-01-28
@flameyolo

First, the decorated function must be marked as async . Secondly , aiogram is an asynchronous library, which means that all its network functions must be await . The code works if all this is corrected.

V
Vladimir, 2022-01-28
@lunat1k1

To do this, it’s better to create a database with a username column (it will be message.from_user.username), and create a decorator for the command you want (example: /add). And to add a person to the white list, you need to add him to the database. You make a variable let's say you assign user_add_in_db in the function where the decorator makes a slice [4:] when you enter /add {username of the person} in the bot, you add it to the database. And when a person sends a command, you check if it is in the database, then it is in the White list, otherwise it is not there. If something is not clear, write it down.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question