This commit is contained in:
mihalin 2021-09-10 01:06:56 +03:00
parent dad7fe66c9
commit 7e89fcdd94

View File

@ -4,9 +4,9 @@ from olgram.models.models import Bot
from aiohttp import web from aiohttp import web
import asyncio import asyncio
import aiohttp import aiohttp
from olgram.settings import ServerSettings from olgram.settings import ServerSettings
import logging
logger = logging.getLogger(__name__)
def path_for_bot(bot: Bot) -> str: def path_for_bot(bot: Bot) -> str:
@ -24,6 +24,8 @@ async def register_token(bot: Bot) -> bool:
:return: получилось ли :return: получилось ли
""" """
await unregister_token(bot.token) await unregister_token(bot.token)
logger.info(f"register token {bot.name}")
a_bot = AioBot(bot.token) a_bot = AioBot(bot.token)
res = await a_bot.set_webhook(url_for_bot(bot)) res = await a_bot.set_webhook(url_for_bot(bot))
await a_bot.session.close() await a_bot.session.close()
@ -36,12 +38,14 @@ async def unregister_token(token: str):
:param token: токен :param token: токен
:return: :return:
""" """
logger.info(f"unregister token {token}")
bot = AioBot(token) bot = AioBot(token)
await bot.session.close() await bot.session.close()
await bot.delete_webhook() await bot.delete_webhook()
async def cmd_start(message, *args, **kwargs): async def cmd_start(message, *args, **kwargs):
logger.info("cmd start")
return SendMessage(chat_id=message.chat.id, text=f'Hi from webhook, bot {message.via_bot}', return SendMessage(chat_id=message.chat.id, text=f'Hi from webhook, bot {message.via_bot}',
reply_to_message_id=message.message_id) reply_to_message_id=message.message_id)
@ -55,6 +59,8 @@ class CustomRequestHandler(WebhookRequestHandler):
async def _create_dispatcher(self): async def _create_dispatcher(self):
key = self.request.url.path[1:] key = self.request.url.path[1:]
logger.info(f"create dispatcher {key}")
bot = await Bot.filter(code=key).first() bot = await Bot.filter(code=key).first()
if not bot: if not bot:
return None return None
@ -67,6 +73,7 @@ class CustomRequestHandler(WebhookRequestHandler):
async def post(self): async def post(self):
# TODO: refactor # TODO: refactor
logger.info(f"post request {self.request.url.path}")
self._dispatcher = await self._create_dispatcher() self._dispatcher = await self._create_dispatcher()
res = await super(CustomRequestHandler, self).post() res = await super(CustomRequestHandler, self).post()
self._dispatcher = None self._dispatcher = None
@ -78,6 +85,7 @@ class CustomRequestHandler(WebhookRequestHandler):
:return: :class:`aiogram.Dispatcher` :return: :class:`aiogram.Dispatcher`
""" """
logger.info("get dispatcher")
return self._dispatcher return self._dispatcher
@ -89,5 +97,6 @@ def main():
runner = aiohttp.web.AppRunner(app) runner = aiohttp.web.AppRunner(app)
loop.run_until_complete(runner.setup()) loop.run_until_complete(runner.setup())
logger.info("server initialization done")
site = aiohttp.web.TCPSite(runner, host=ServerSettings.app_host(), port=ServerSettings.app_port()) site = aiohttp.web.TCPSite(runner, host=ServerSettings.app_host(), port=ServerSettings.app_port())
return site return site