This commit is contained in:
mihalin 2021-09-10 01:51:03 +03:00
parent 944cacb973
commit 9297f2b33f

View File

@ -2,8 +2,7 @@ from aiogram import Bot as AioBot, Dispatcher
from aiogram.dispatcher.webhook import SendMessage, WebhookRequestHandler from aiogram.dispatcher.webhook import SendMessage, WebhookRequestHandler
from olgram.models.models import Bot from olgram.models.models import Bot
from aiohttp import web from aiohttp import web
import asyncio from asyncio import get_event_loop
import aiohttp
from olgram.settings import ServerSettings from olgram.settings import ServerSettings
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -25,7 +24,6 @@ async def register_token(bot: Bot) -> bool:
""" """
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()
@ -39,7 +37,6 @@ 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.delete_webhook() await bot.delete_webhook()
await bot.session.close() await bot.session.close()
@ -47,7 +44,6 @@ async def unregister_token(token: str):
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)
@ -61,8 +57,6 @@ 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
@ -75,7 +69,6 @@ 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
@ -87,18 +80,17 @@ class CustomRequestHandler(WebhookRequestHandler):
:return: :class:`aiogram.Dispatcher` :return: :class:`aiogram.Dispatcher`
""" """
logger.info("get dispatcher")
return self._dispatcher return self._dispatcher
def main(): def main():
loop = asyncio.get_event_loop() loop = get_event_loop()
app = web.Application() app = web.Application()
app.router.add_route('*', r"/{name}", CustomRequestHandler, name='webhook_handler') app.router.add_route('*', r"/{name}", CustomRequestHandler, name='webhook_handler')
runner = aiohttp.web.AppRunner(app) runner = web.AppRunner(app)
loop.run_until_complete(runner.setup()) loop.run_until_complete(runner.setup())
logger.info("server initialization done") logger.info("Server initialization done")
site = aiohttp.web.TCPSite(runner, host=ServerSettings.app_host(), port=ServerSettings.app_port()) site = web.TCPSite(runner, host=ServerSettings.app_host(), port=ServerSettings.app_port())
return site return site