From 1bbe12e9c14c316c1c3f25d548ed047859529039 Mon Sep 17 00:00:00 2001 From: latand Date: Thu, 7 Jan 2021 13:58:34 +0200 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D1=88=D0=B0=D0=B1=D0=BB=D0=BE=D0=BD=D0=B0:=201.?= =?UTF-8?q?=20.env.dist=20-=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD=D0=BE=D0=B9,?= =?UTF-8?q?=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=B0=D0=B9=D0=B4?= =?UTF-8?q?=D0=B8=20=D0=B0=D0=B4=D0=BC=D0=B8=D0=BD=D0=BE=D0=B2=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=D0=BF=D0=B8=D1=81=D1=8B=D0=B2=D0=B0=D1=8E=D1=82?= =?UTF-8?q?=D1=81=D1=8F=20=D1=81=D1=8E=D0=B4=D0=B0=20=D1=87=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=B7=20=D0=B7=D0=B0=D0=BF=D1=8F=D1=82=D1=83=D1=8E=202.?= =?UTF-8?q?=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20.env=20?= =?UTF-8?q?=D0=B2=20.gitignore=203.=20=D0=92=D1=8B=D0=BD=D0=B5=D1=81=20?= =?UTF-8?q?=D0=BF=D0=B0=D1=80=D1=83=20=D0=B8=D0=BC=D0=BF=D0=BE=D1=80=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=B8=D0=B7=20=D1=84=D1=83=D0=BD=D0=BA=D1=86?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=BA=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=20=D0=BE=D1=82=D1=81=D0=BB=D0=B5?= =?UTF-8?q?=D0=B4=D0=B8=D1=82=D1=8C=20=D0=BD=D0=B5=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=81=D0=BE?= =?UTF-8?q?=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F.=206.=20=D0=98=D0=B7?= =?UTF-8?q?=20help.py=20=D1=83=D0=B1=D1=80=D0=B0=D0=BB=20=D1=82=D1=80?= =?UTF-8?q?=D0=BE=D1=82=D1=82=D0=BB=D0=B8=D0=BD=D0=B3=207.=20=D0=9E=D0=B1?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=B8=D0=BB=20requirements.txt=20=D1=87?= =?UTF-8?q?=D1=82=D0=BE=D0=B1=D1=8B=20=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B6=D0=B0=D1=82=D1=8C=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE?= =?UTF-8?q?=20=D0=BD=D0=B5=D0=BE=D0=B1=D1=85=D0=BE=D0=B4=D0=B8=D0=BC=D1=8B?= =?UTF-8?q?=D0=B5=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5=D0=BA?= =?UTF-8?q?=D0=B8=208.=20=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20(=D0=B2=D0=BE?= =?UTF-8?q?=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE)=20=D0=BD=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=87=D0=B8=D0=B9=20=D0=BA=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=B8=D0=B7=20throttling.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.dist | 4 +++- .gitignore | 1 + app.py | 16 ++++++++++------ data/config.py | 18 +++++++----------- handlers/users/echo.py | 18 ++++++++++++++++-- handlers/users/help.py | 2 -- middlewares/throttling.py | 11 ----------- requirements.txt | 6 ++---- utils/misc/logging.py | 2 +- utils/notify_admins.py | 4 ++-- 10 files changed, 42 insertions(+), 40 deletions(-) diff --git a/.env.dist b/.env.dist index f42c367..66435c3 100644 --- a/.env.dist +++ b/.env.dist @@ -1,3 +1,5 @@ -ADMIN_ID=12345678 +# ЭТО ПРИМЕР ФАЙЛА .env !! ВАМ НАДО ЭТОТ ФАЙЛ ПЕРЕИМЕНОВАТЬ И ВСТАВИТЬ ТУДА ЗНАЧЕНИЯ + +ADMINS=12345678,12345677,12345676 BOT_TOKEN=123452345243:Asdfasdfasf ip=localhost \ No newline at end of file diff --git a/.gitignore b/.gitignore index a66dec6..913d692 100644 --- a/.gitignore +++ b/.gitignore @@ -128,3 +128,4 @@ dmypy.json # Pyre type checker .pyre/ .idea/* +.env \ No newline at end of file diff --git a/app.py b/app.py index 08f8cf4..1c9cb0f 100644 --- a/app.py +++ b/app.py @@ -1,15 +1,19 @@ -async def on_startup(dp): +from aiogram import executor + +from utils.notify_admins import on_startup_notify + + +async def on_startup(dispatcher): import filters import middlewares - filters.setup(dp) - middlewares.setup(dp) - from utils.notify_admins import on_startup_notify - await on_startup_notify(dp) + filters.setup(dispatcher) + middlewares.setup(dispatcher) + + await on_startup_notify(dispatcher) if __name__ == '__main__': - from aiogram import executor from handlers import dp executor.start_polling(dp, on_startup=on_startup) diff --git a/data/config.py b/data/config.py index eb58ca0..e216714 100644 --- a/data/config.py +++ b/data/config.py @@ -1,13 +1,9 @@ -import os +from environs import Env -from dotenv import load_dotenv +# Теперь используем вместо библиотеки python-dotenv библиотеку environs +env = Env() +env.read_env() -load_dotenv() - -BOT_TOKEN = os.getenv("BOT_TOKEN") - -admins = [ - os.getenv("ADMIN_ID"), -] - -ip = os.getenv("ip") +BOT_TOKEN = env.str("BOT_TOKEN") # Забираем значение типа str +ADMINS = env.list("ADMINS") # Тут у нас будет список из админов +IP = env.str("ip") # Тоже str, но для айпи адреса хоста diff --git a/handlers/users/echo.py b/handlers/users/echo.py index 93cd2fe..ede7e41 100644 --- a/handlers/users/echo.py +++ b/handlers/users/echo.py @@ -1,7 +1,21 @@ from aiogram import types +from aiogram.dispatcher import FSMContext + from loader import dp -@dp.message_handler() +# Эхо хендлер, куда летят текстовые сообщения без указанного состояния +@dp.message_handler(state=None) async def bot_echo(message: types.Message): - await message.answer(message.text) + await message.answer(f"Эхо без состояния." + f"Сообщение:\n" + f"{message.text}") + + +# Эхо хендлер, куда летят ВСЕ сообщения с указанным состоянием +@dp.message_handler(state="*", content_types=types.ContentTypes.ANY) +async def bot_echo_all(message: types.Message, state: FSMContext): + s = await state.get_state() + await message.answer(f"Эхо в состоянии {s}.\n" + f"\nСодержание сообщения:\n" + f"{message}") diff --git a/handlers/users/help.py b/handlers/users/help.py index 7226861..6bb0360 100644 --- a/handlers/users/help.py +++ b/handlers/users/help.py @@ -2,10 +2,8 @@ from aiogram import types from aiogram.dispatcher.filters.builtin import CommandHelp from loader import dp -from utils.misc import rate_limit -@rate_limit(5, 'help') @dp.message_handler(CommandHelp()) async def bot_help(message: types.Message): text = [ diff --git a/middlewares/throttling.py b/middlewares/throttling.py index 9013d47..5f54e0b 100644 --- a/middlewares/throttling.py +++ b/middlewares/throttling.py @@ -33,16 +33,5 @@ class ThrottlingMiddleware(BaseMiddleware): raise CancelHandler() async def message_throttled(self, message: types.Message, throttled: Throttled): - handler = current_handler.get() - dispatcher = Dispatcher.get_current() - if handler: - key = getattr(handler, 'throttling_key', f"{self.prefix}_{handler.__name__}") - else: - key = f"{self.prefix}_message" - delta = throttled.rate - throttled.delta if throttled.exceeded_count <= 2: await message.reply('Too many requests! ') - await asyncio.sleep(delta) - thr = await dispatcher.check_key(key) - if thr.exceeded_count == throttled.exceeded_count: - await message.reply('Unlocked.') diff --git a/requirements.txt b/requirements.txt index 44a84cb..9723d39 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,2 @@ -aiogram==2.11.2 -aiohttp>=3.6.2 -aioredis==1.3.1 -python-dotenv +aiogram>=2.11.2,<3.0 +environs~=8.0.0 \ No newline at end of file diff --git a/utils/misc/logging.py b/utils/misc/logging.py index f7d49e9..e5b24b9 100644 --- a/utils/misc/logging.py +++ b/utils/misc/logging.py @@ -2,5 +2,5 @@ import logging logging.basicConfig(format=u'%(filename)s [LINE:%(lineno)d] #%(levelname)-8s [%(asctime)s] %(message)s', level=logging.INFO, - # level=logging.DEBUG, + # level=logging.DEBUG, # Можно заменить на другой уровень логгирования. ) diff --git a/utils/notify_admins.py b/utils/notify_admins.py index c920196..97e1309 100644 --- a/utils/notify_admins.py +++ b/utils/notify_admins.py @@ -2,11 +2,11 @@ import logging from aiogram import Dispatcher -from data.config import admins +from data.config import ADMINS async def on_startup_notify(dp: Dispatcher): - for admin in admins: + for admin in ADMINS: try: await dp.bot.send_message(admin, "Бот Запущен")