Вот код
main.py:
from aiogram import Bot, Dispatcher
from aiogram.types import Message
import asyncio
import logging
from core.handlers.basic import get_start
from core.settings import settings
async def start_bot(bot: Bot):
await bot.send_message(settings.bots.admin_id, text='Бот запущен!')
async def stop_bot(bot: Bot):
await bot.send_message(settings.bots.admin_id, text='Бот остановлен!')
async def start():
logging.basicConfig(level=logging.INFO,
format="%(asctime)s - [%(levelname)s] - %(name)s - "
"(%(filename)s).%(funcName)s(%(lineno)d) - %(message)s")
bot = Bot(token=settings.bots.bot_token, parse_mode='HTML')
dp = Dispatcher()
dp.startup.register(start_bot)
dp.shutdown.register(stop_bot)
dp.message.register(get_start)
try:
await dp.start_polling(bot)
finally:
await bot.session.close()
if __name__ == '__main__':
asyncio.run(start())
core/settings.py:
from environs import Env
from dataclasses import dataclass
@dataclass
class Bots:
bot_token: str
admin_id: int
@dataclass
class Settings:
bots: Bots
def get_settings(path: str):
env = Env()
env.read_env(path)
return Settings(
bots=Bots(
bot_token=env.str("BOT_TOKEN"),
admin_id=env.int("ADMIN_ID")))
settings = get_settings('input')
print(settings)
input:
BOT_TOKEN=2654264325:Lu4QhnkGu-UQpF-WNkO_yg
ADMIN_ID=6374296403
(всё не настоящее)
ошибка:
Traceback (most recent call last):
File "C:\Users\Дамир\Desktop\TelegramBOT\bot\main.py", line 6, in <module>
from core.settings import settings
File "C:\Users\Дамир\Desktop\TelegramBOT\bot\core\settings.py", line 22, in <module>
settings = get_settings('input')
File "C:\Users\Дамир\Desktop\TelegramBOT\bot\core\settings.py", line 19, in get_settings
bot_token=env.str("BOT_TOKEN"),
File "P:\Python_3.10.8\lib\site-packages\environs\__init__.py", line 116, in method
raise EnvError('Environment variable "{}" not set'.format(proxied_key or parsed_key))
environs.EnvError: Environment variable "BOT_TOKEN" not set
Process finished with exit code 1