forked from love-apples/maxapi
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlow_level.py
More file actions
63 lines (46 loc) · 1.77 KB
/
low_level.py
File metadata and controls
63 lines (46 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import asyncio
import logging
try:
from fastapi import Request
from fastapi.responses import JSONResponse
except ImportError:
raise ImportError(
'\n\t Не установлен fastapi!'
'\n\t Выполните команду для установки fastapi: '
'\n\t pip install fastapi>=0.68.0'
'\n\t Или сразу все зависимости для работы вебхука:'
'\n\t pip install maxapi[webhook]'
)
from maxapi import Bot, Dispatcher
from maxapi.methods.types.getted_updates import process_update_webhook
from maxapi.types import MessageCreated
logging.basicConfig(level=logging.INFO)
bot = Bot('тут_ваш_токен')
dp = Dispatcher()
@dp.message_created()
async def handle_message(event: MessageCreated):
await event.message.answer('Бот работает через вебхук!')
# Регистрация обработчика
# для вебхука
@dp.webhook_post('/')
async def _(request: Request):
# Сериализация полученного запроса
event_json = await request.json()
# Десериализация полученного запроса
# в pydantic
event_object = await process_update_webhook(
event_json=event_json,
bot=bot
)
# ...свой код
print(f'Информация из вебхука: {event_json}')
# ...свой код
# Окончательная обработка запроса
await dp.handle(event_object)
# Ответ вебхука
return JSONResponse(content={'ok': True}, status_code=200)
async def main():
# Запуск сервера
await dp.init_serve(bot, log_level='critical')
if __name__ == '__main__':
asyncio.run(main())