From 68107caae41e600e24dc21d4cb181632e6084527 Mon Sep 17 00:00:00 2001 From: mihalin Date: Fri, 10 Sep 2021 21:32:06 +0300 Subject: [PATCH] test --- server/custom.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ server/server.py | 46 +++++------------------------------------ 2 files changed, 59 insertions(+), 41 deletions(-) create mode 100644 server/custom.py diff --git a/server/custom.py b/server/custom.py new file mode 100644 index 0000000..7eca260 --- /dev/null +++ b/server/custom.py @@ -0,0 +1,54 @@ +from aiogram import Bot as AioBot, Dispatcher +from aiogram.dispatcher.webhook import WebhookRequestHandler +from aiogram.dispatcher.webhook import SendMessage +from aiogram import types +from olgram.models.models import Bot + + +async def message_handler(message, *args, **kwargs): + if message.text and message.text.startswith("/start"): + # На команду start нужно ответить, не пересылая сообщение никуда + return SendMessage(chat_id=message.chat.id, text=f'Hi from webhook {args} {kwargs}') + + +class CustomRequestHandler(WebhookRequestHandler): + + def __init__(self, *args, **kwargs): + self._dispatcher = None + super(CustomRequestHandler, self).__init__(*args, **kwargs) + + async def _create_dispatcher(self): + key = self.request.url.path[1:] + + bot = await Bot.filter(code=key).first() + if not bot: + return None + + dp = Dispatcher(AioBot(bot.token)) + + dp.register_message_handler(message_handler, content_types=[types.ContentType.TEXT, + types.ContentType.CONTACT, + types.ContentType.ANIMATION, + types.ContentType.AUDIO, + types.ContentType.DOCUMENT, + types.ContentType.PHOTO, + types.ContentType.STICKER, + types.ContentType.VIDEO, + types.ContentType.VOICE]) + + return dp + + async def post(self): + # TODO: refactor + self._dispatcher = await self._create_dispatcher() + res = await super(CustomRequestHandler, self).post() + self._dispatcher = None + return res + + def get_dispatcher(self): + """ + Get Dispatcher instance from environment + + :return: :class:`aiogram.Dispatcher` + """ + return self._dispatcher diff --git a/server/server.py b/server/server.py index d785aee..86c23bf 100644 --- a/server/server.py +++ b/server/server.py @@ -1,10 +1,15 @@ from aiogram import Bot as AioBot, Dispatcher from aiogram.dispatcher.webhook import SendMessage, WebhookRequestHandler +from aiogram import types from olgram.models.models import Bot from aiohttp import web from asyncio import get_event_loop from olgram.settings import ServerSettings +from custom import CustomRequestHandler + import logging + + logger = logging.getLogger(__name__) @@ -43,47 +48,6 @@ async def unregister_token(token: str): del bot -async def cmd_start(message, *args, **kwargs): - return SendMessage(chat_id=message.chat.id, text=f'Hi from webhook, bot {message.via_bot}', - reply_to_message_id=message.message_id) - - -class CustomRequestHandler(WebhookRequestHandler): - - def __init__(self, *args, **kwargs): - self._dispatcher = None - super(CustomRequestHandler, self).__init__(*args, **kwargs) - - async def _create_dispatcher(self): - key = self.request.url.path[1:] - - bot = await Bot.filter(code=key).first() - if not bot: - return None - - dp = Dispatcher(AioBot(bot.token)) - - dp.register_message_handler(cmd_start, commands=['start']) - - return dp - - async def post(self): - # TODO: refactor - logger.info(f"post request to {self}") - self._dispatcher = await self._create_dispatcher() - res = await super(CustomRequestHandler, self).post() - self._dispatcher = None - return res - - def get_dispatcher(self): - """ - Get Dispatcher instance from environment - - :return: :class:`aiogram.Dispatcher` - """ - return self._dispatcher - - def main(): loop = get_event_loop()