From 4739b8963abe4acbcad81305c87f222f61c49f4d Mon Sep 17 00:00:00 2001 From: Alexey Sokolov Date: Thu, 10 Feb 2022 15:56:57 +0300 Subject: [PATCH] Moved telegram part to telegram package --- config.py | 18 +++++++++--------- db/__init__.py | 0 start.py | 23 +++++++++-------------- telegram/__init__.py | 9 +++++++++ web/__init__.py | 0 5 files changed, 27 insertions(+), 23 deletions(-) create mode 100644 db/__init__.py create mode 100644 telegram/__init__.py create mode 100644 web/__init__.py diff --git a/config.py b/config.py index 2b5c391..c36c977 100644 --- a/config.py +++ b/config.py @@ -8,11 +8,11 @@ class Config: Notifier config dataclass. * `base_url`: url of domain - * `api_token`: telegram api token. + * `teletoken`: telegram api token. * `param bot_owner`: telegram id of bot owner. """ base_url: str - api_token: str + teletoken: str bot_owner: int @@ -20,13 +20,13 @@ class ConfigError(Exception): pass -_api_token = getenv('LTLNOTIFIER_API_TOKEN') -print(_api_token[9]) +_teletoken = getenv('LTLNOTIFIER_TELETOKEN') +print(_teletoken[9]) # Check api token -if not _api_token: - raise ConfigError('virtual environment LTLNOTIFIER_API_TOKEN not set.') -if len(_api_token) != 45 or _api_token[9] != ':' or not _api_token[:9].isdigit(): - raise ConfigError('virtual environment LTLNOTIFIER_API_TOKEN incorrect.') +if not _teletoken: + raise ConfigError('virtual environment LTLNOTIFIER_TELETOKEN not set.') +if len(_teletoken) != 45 or _teletoken[9] != ':' or not _teletoken[:9].isdigit(): + raise ConfigError('virtual environment LTLNOTIFIER_TELETOKEN incorrect.') _bot_owner = getenv('LTLNOTIFIER_BOT_OWNER') print(_bot_owner) @@ -45,6 +45,6 @@ _base_url = getenv('LTLNOTIFIER_BASE_URL') if not _base_url: raise ConfigError('virtual environment LTLNOTIFIER_BASE_URL not set') -config = Config(api_token=_api_token, bot_owner=_bot_owner, base_url=_base_url) +config = Config(teletoken=_teletoken, bot_owner=_bot_owner, base_url=_base_url) __all__ = ['Config', 'config'] diff --git a/db/__init__.py b/db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/start.py b/start.py index 3a05296..0fab8a4 100644 --- a/start.py +++ b/start.py @@ -1,37 +1,32 @@ import logging -from aiogram import Bot, Dispatcher from aiogram.dispatcher.webhook import configure_app -from aiogram.contrib.middlewares.logging import LoggingMiddleware from aiohttp import web from config import config - +from telegram import telegram_bot, telegram_dispatcher WEBAPP_PORT = 3001 -TELEGRAM_WEBHOOK_URL = f'{config.base_url}{config.api_token}' +TELEGRAM_WEBHOOK_URL = f'{config.base_url}/{config.teletoken}' logging.basicConfig(level=logging.INFO) -bot = Bot(token=config.api_token) -dp = Dispatcher(bot) -dp.middleware.setup(LoggingMiddleware()) - async def test_route(_): return web.json_response({'test': 'passed'}, status=200) async def on_startup(_): - await bot.set_webhook(TELEGRAM_WEBHOOK_URL) + await telegram_bot.set_webhook(TELEGRAM_WEBHOOK_URL) async def on_shutdown(_): logging.warning('Shutting down') - await bot.delete_webhook() - await dp.storage.close() - await dp.storage.wait_closed() - await bot.session.close() + await telegram_bot.delete_webhook() + await telegram_dispatcher.storage.close() + await telegram_dispatcher.storage.wait_closed() + session = await telegram_bot.get_session() + await session.close() logging.warning('Bye!..') @@ -39,7 +34,7 @@ app = web.Application() app.add_routes([web.get('/test', test_route)]) app.on_startup.append(on_startup) app.on_shutdown.append(on_shutdown) -configure_app(dp, app, '/'+config.api_token) +configure_app(telegram_dispatcher, app, '/'+config.teletoken) if __name__ == '__main__': web.run_app(app, port=WEBAPP_PORT) diff --git a/telegram/__init__.py b/telegram/__init__.py new file mode 100644 index 0000000..92d33eb --- /dev/null +++ b/telegram/__init__.py @@ -0,0 +1,9 @@ +from aiogram import Bot, Dispatcher +from aiogram.contrib.middlewares.logging import LoggingMiddleware + +from config import config + + +telegram_bot = Bot(token=config.teletoken) +telegram_dispatcher = Dispatcher(telegram_bot) +telegram_dispatcher.middleware.setup(LoggingMiddleware()) diff --git a/web/__init__.py b/web/__init__.py new file mode 100644 index 0000000..e69de29