Created
April 19, 2019 06:50
-
-
Save JrooTJunior/a8547cc0c4c5d100d789f8bcb393eae5 to your computer and use it in GitHub Desktop.
Extended IsAdminFilter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import os | |
from aiogram import Bot, Dispatcher, types | |
from aiogram.contrib.middlewares.logging import LoggingMiddleware | |
from aiogram.dispatcher.filters import BoundFilter | |
from aiogram.utils import executor | |
logging.basicConfig(level=logging.INFO) | |
TOKEN = os.getenv('TELEGRAM_TOKEN') | |
bot = Bot(TOKEN) | |
dp = Dispatcher(bot) | |
dp.middleware.setup(LoggingMiddleware()) | |
ADMINS = [ | |
61043901 | |
] | |
class IsAdminFilter(BoundFilter): | |
key = 'is_admin' | |
def __init__(self, is_admin: bool): | |
self.is_admin = is_admin | |
async def check(self, message: types.Message) -> bool: | |
user = message.from_user.id | |
if self.is_admin: | |
return user in ADMINS | |
return user not in ADMINS | |
dp.filters_factory.bind(IsAdminFilter) | |
@dp.message_handler(is_admin=False, commands=['isNotAdmin']) | |
async def cmd_is_not_admin(message: types.Message): | |
return await message.answer('Ok') | |
@dp.message_handler(is_admin=True, commands=['isAdmin']) | |
async def cmd_is_admin(message: types.Message): | |
return await message.answer('Ok') | |
@dp.message_handler(commands=['allUsers']) | |
async def cmd_all_users(message: types.Message): | |
return await message.answer('Ok') | |
if __name__ == '__main__': | |
executor.start_polling(dp, skip_updates=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment