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 olgram.models.models import Bot
from aiohttp import web
import asyncio
import aiohttp
from asyncio import get_event_loop
from olgram.settings import ServerSettings
import logging
logger = logging.getLogger(__name__)
@ -25,7 +24,6 @@ async def register_token(bot: Bot) -> bool:
"""
await unregister_token(bot.token)
logger.info(f"register token {bot.name}")
a_bot = AioBot(bot.token)
res = await a_bot.set_webhook(url_for_bot(bot))
await a_bot.session.close()
@ -39,7 +37,6 @@ async def unregister_token(token: str):
:param token: токен
:return:
"""
logger.info(f"unregister token {token}")
bot = AioBot(token)
await bot.delete_webhook()
await bot.session.close()
@ -47,7 +44,6 @@ async def unregister_token(token: str):
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}',
reply_to_message_id=message.message_id)
@ -61,8 +57,6 @@ class CustomRequestHandler(WebhookRequestHandler):
async def _create_dispatcher(self):
key = self.request.url.path[1:]
logger.info(f"create dispatcher {key}")
bot = await Bot.filter(code=key).first()
if not bot:
return None
@ -75,7 +69,6 @@ class CustomRequestHandler(WebhookRequestHandler):
async def post(self):
# TODO: refactor
logger.info(f"post request {self.request.url.path}")
self._dispatcher = await self._create_dispatcher()
res = await super(CustomRequestHandler, self).post()
self._dispatcher = None
@ -87,18 +80,17 @@ class CustomRequestHandler(WebhookRequestHandler):
:return: :class:`aiogram.Dispatcher`
"""
logger.info("get dispatcher")
return self._dispatcher
def main():
loop = asyncio.get_event_loop()
loop = get_event_loop()
app = web.Application()
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())
logger.info("server initialization done")
site = aiohttp.web.TCPSite(runner, host=ServerSettings.app_host(), port=ServerSettings.app_port())
logger.info("Server initialization done")
site = web.TCPSite(runner, host=ServerSettings.app_host(), port=ServerSettings.app_port())
return site