diff --git a/config.py b/config.py index 19d922d..6ff2657 100644 --- a/config.py +++ b/config.py @@ -47,7 +47,7 @@ _base_url = getenv('LTLNOTIFIER_BASE_URL') if not _base_url: raise ConfigError('virtual environment LTLNOTIFIER_BASE_URL not set') -_port = getenv('LTLNOTIFIER_PORT') +_port = getenv('LTLNOTIFIER_PORT', default='') _port = int(_port) if _port.isdigit() else 3001 config = Config(teletoken=_teletoken, diff --git a/start.py b/start.py index 0fab8a4..3753179 100644 --- a/start.py +++ b/start.py @@ -1,40 +1,13 @@ import logging -from aiogram.dispatcher.webhook import configure_app from aiohttp import web from config import config -from telegram import telegram_bot, telegram_dispatcher +from web import app -WEBAPP_PORT = 3001 -TELEGRAM_WEBHOOK_URL = f'{config.base_url}/{config.teletoken}' logging.basicConfig(level=logging.INFO) -async def test_route(_): - return web.json_response({'test': 'passed'}, status=200) - - -async def on_startup(_): - await telegram_bot.set_webhook(TELEGRAM_WEBHOOK_URL) - - -async def on_shutdown(_): - logging.warning('Shutting down') - 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!..') - - -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(telegram_dispatcher, app, '/'+config.teletoken) - if __name__ == '__main__': - web.run_app(app, port=WEBAPP_PORT) + web.run_app(app, port=config.port) diff --git a/telegram/__init__.py b/telegram/__init__.py index 1a354e9..7a625cc 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -1,4 +1,5 @@ from aiogram import Bot, Dispatcher +from aiogram.dispatcher.webhook import SendMessage from aiogram.contrib.middlewares.logging import LoggingMiddleware from config import config @@ -9,4 +10,9 @@ telegram_dispatcher = Dispatcher(telegram_bot) telegram_dispatcher.middleware.setup(LoggingMiddleware()) +@telegram_dispatcher.message_handler(commands=['test']) +async def test_telegram(message): + return SendMessage(message.chat.id, 'passed') + + __all__ = ('telegram_bot', telegram_dispatcher) \ No newline at end of file diff --git a/web/__init__.py b/web/__init__.py index e69de29..0e428a9 100644 --- a/web/__init__.py +++ b/web/__init__.py @@ -0,0 +1,34 @@ +import logging +from aiogram.dispatcher.webhook import configure_app +from aiohttp import web + +from config import config +from telegram import telegram_bot, telegram_dispatcher +from .routes import routes, TELEGRAM_WEBHOOK_ROUTE + + +TELEGRAM_WEBHOOK_URL = f'{config.base_url}{TELEGRAM_WEBHOOK_ROUTE}' + + +async def on_startup(_): + await telegram_bot.set_webhook(TELEGRAM_WEBHOOK_URL) + + +async def on_shutdown(_): + logging.warning('Shutting down') + 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!..') + + +app = web.Application() +app.add_routes(routes) +app.on_startup.append(on_startup) +app.on_shutdown.append(on_shutdown) +configure_app(telegram_dispatcher, app, TELEGRAM_WEBHOOK_ROUTE) + + +__all__ = ('app',) diff --git a/web/routes.py b/web/routes.py index e69de29..0aa55d0 100644 --- a/web/routes.py +++ b/web/routes.py @@ -0,0 +1,14 @@ +from aiohttp import web + +from config import config +from .views import test_route + + +TELEGRAM_WEBHOOK_ROUTE = '/' + config.teletoken + +routes = [ + web.get('/test', test_route) +] + + +__all__ = ('TELEGRAM_WEBHOOK_ROUTE', 'routes') diff --git a/web/views.py b/web/views.py index e69de29..996621c 100644 --- a/web/views.py +++ b/web/views.py @@ -0,0 +1,8 @@ +from aiohttp import web + + +async def test_route(_): + return web.json_response({'test': 'passed'}, status=200) + + +__all__ = ('test_route',)