1
from http import HTTPStatus
3
from fastapi import APIRouter, Request
4
from fastapi.responses import JSONResponse
7
BotXMethodCallbackNotFoundError,
8
UnknownBotAccountError,
9
UnsupportedBotAPIVersionError,
10
UnverifiedRequestError,
11
build_bot_disabled_response,
12
build_command_accepted_response,
13
build_unverified_request_response,
15
from pybotx.constants import BOT_API_VERSION
17
from dependencies.bot import bot_dependency
18
from app.logger import logger
19
from settings import settings
24
@router.post("/command")
25
async def command_handler(request: Request, bot: Bot = bot_dependency) -> JSONResponse:
26
"""Receive commands from users. Max timeout - 5 seconds."""
29
bot.async_execute_raw_bot_command(
31
request_headers=request.headers,
34
error_label = "Bot command validation error"
37
logger.exception(error_label)
39
logger.warning(error_label)
42
build_bot_disabled_response(error_label),
43
status_code=HTTPStatus.SERVICE_UNAVAILABLE,
45
except UnknownBotAccountError as exc:
46
error_label = f"No credentials for bot {exc.bot_id}"
47
logger.warning(error_label)
50
build_bot_disabled_response(error_label),
51
status_code=HTTPStatus.SERVICE_UNAVAILABLE,
53
except UnsupportedBotAPIVersionError as exc:
55
f"Unsupported Bot API version: `{exc.version}`. "
56
f"Set protocol version to `{BOT_API_VERSION}` in Admin panel."
58
logger.warning(error_label)
61
build_bot_disabled_response(error_label),
62
status_code=HTTPStatus.SERVICE_UNAVAILABLE,
64
except UnverifiedRequestError as exc:
65
logger.warning(f"UnverifiedRequestError: {exc.args[0]}")
67
content=build_unverified_request_response(
68
status_message=exc.args[0],
70
status_code=HTTPStatus.UNAUTHORIZED,
74
build_command_accepted_response(), status_code=HTTPStatus.ACCEPTED
79
async def status_handler(request: Request, bot: Bot = bot_dependency) -> JSONResponse:
80
"""Show bot status and commands list."""
83
status = await bot.raw_get_status(
84
dict(request.query_params),
85
request_headers=request.headers,
87
except UnknownBotAccountError as exc:
88
error_label = f"Unknown bot_id: {exc.bot_id}"
91
build_bot_disabled_response(error_label),
92
status_code=HTTPStatus.SERVICE_UNAVAILABLE,
95
error_label = "Invalid params"
96
logger.warning(error_label)
98
build_bot_disabled_response(error_label), status_code=HTTPStatus.BAD_REQUEST
100
except UnverifiedRequestError as exc:
101
logger.warning(f"UnverifiedRequestError: {exc.args[0]}")
103
content=build_unverified_request_response(
104
status_message=exc.args[0],
106
status_code=HTTPStatus.UNAUTHORIZED,
109
return JSONResponse(status)
112
@router.post("/notification/callback")
113
async def callback_handler(request: Request, bot: Bot = bot_dependency) -> JSONResponse:
114
"""Process BotX methods callbacks."""
117
await bot.set_raw_botx_method_result(
118
await request.json(),
119
verify_request=False,
121
except BotXMethodCallbackNotFoundError as exc:
122
error_label = f"Unexpected callback with sync_id: {exc.sync_id}"
123
logger.warning(error_label)
126
build_bot_disabled_response(error_label),
127
status_code=HTTPStatus.SERVICE_UNAVAILABLE,
131
build_command_accepted_response(),
132
status_code=HTTPStatus.ACCEPTED,