mirror of
https://github.com/civsocit/olgram.git
synced 2023-07-22 01:29:12 +03:00
test
This commit is contained in:
parent
8066aed530
commit
54f5553ee3
@ -2,14 +2,18 @@ from aiogram import Bot as AioBot, Dispatcher
|
|||||||
from aiogram.dispatcher.webhook import WebhookRequestHandler
|
from aiogram.dispatcher.webhook import WebhookRequestHandler
|
||||||
from aiogram.dispatcher.webhook import SendMessage
|
from aiogram.dispatcher.webhook import SendMessage
|
||||||
from aiogram import types
|
from aiogram import types
|
||||||
|
from contextvars import ContextVar
|
||||||
|
from aiohttp.web_exceptions import HTTPNotFound
|
||||||
from olgram.models.models import Bot
|
from olgram.models.models import Bot
|
||||||
|
|
||||||
|
db_bot_instance: ContextVar[Bot] = ContextVar('db_bot_instance')
|
||||||
|
|
||||||
|
|
||||||
async def message_handler(message, *args, **kwargs):
|
async def message_handler(message, *args, **kwargs):
|
||||||
if message.text and message.text.startswith("/start"):
|
if message.text and message.text.startswith("/start"):
|
||||||
# На команду start нужно ответить, не пересылая сообщение никуда
|
# На команду start нужно ответить, не пересылая сообщение никуда
|
||||||
bot = AioBot.get_current()
|
return SendMessage(chat_id=message.chat.id,
|
||||||
return SendMessage(chat_id=message.chat.id, text=f'Hi from webhook {args} {kwargs} {bot}')
|
text=db_bot_instance.get().start_text)
|
||||||
|
|
||||||
|
|
||||||
class CustomRequestHandler(WebhookRequestHandler):
|
class CustomRequestHandler(WebhookRequestHandler):
|
||||||
@ -18,14 +22,13 @@ class CustomRequestHandler(WebhookRequestHandler):
|
|||||||
self._dispatcher = None
|
self._dispatcher = None
|
||||||
super(CustomRequestHandler, self).__init__(*args, **kwargs)
|
super(CustomRequestHandler, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
async def _create_dispatcher(self):
|
async def _create_dispatcher(self):
|
||||||
key = self.request.url.path[1:]
|
key = self.request.url.path[1:]
|
||||||
|
|
||||||
bot = await Bot.filter(code=key).first()
|
bot = await Bot.filter(code=key).first()
|
||||||
if not bot:
|
if not bot:
|
||||||
return None
|
return None
|
||||||
|
db_bot_instance.set(bot)
|
||||||
dp = Dispatcher(AioBot(bot.token))
|
dp = Dispatcher(AioBot(bot.token))
|
||||||
|
|
||||||
dp.register_message_handler(message_handler, content_types=[types.ContentType.TEXT,
|
dp.register_message_handler(message_handler, content_types=[types.ContentType.TEXT,
|
||||||
@ -42,6 +45,9 @@ class CustomRequestHandler(WebhookRequestHandler):
|
|||||||
|
|
||||||
async def post(self):
|
async def post(self):
|
||||||
dispatcher = await self._create_dispatcher()
|
dispatcher = await self._create_dispatcher()
|
||||||
|
if not dispatcher:
|
||||||
|
raise HTTPNotFound()
|
||||||
|
|
||||||
Dispatcher.set_current(dispatcher)
|
Dispatcher.set_current(dispatcher)
|
||||||
AioBot.set_current(dispatcher.bot)
|
AioBot.set_current(dispatcher.bot)
|
||||||
return await super(CustomRequestHandler, self).post()
|
return await super(CustomRequestHandler, self).post()
|
||||||
|
Loading…
Reference in New Issue
Block a user