Перейти к содержанию

SendAction

SendAction(bot, chat_id=None, action=SenderAction.TYPING_ON)

Bases: BaseConnection

Класс для отправки действия пользователя (например, индикатора печати) в чат.

https://dev.max.ru/docs-api/methods/POST/chats/-chatId-/actions

Attributes:

Name Type Description
bot

Экземпляр бота для выполнения запроса.

chat_id

Идентификатор чата. Если None, действие не отправляется.

action

Тип действия. По умолчанию SenderAction.TYPING_ON.

Source code in maxapi/methods/send_action.py
def __init__(
    self,
    bot: "Bot",
    chat_id: int | None = None,
    action: SenderAction | str = SenderAction.TYPING_ON,
):
    super().__init__()

    if not isinstance(action, SenderAction):
        if not isinstance(action, str):
            raise TypeError(
                f"action должен быть SenderAction или str, "
                f"получено: {type(action).__name__}"
            )

        try:
            action = SenderAction(action)
        except ValueError as e:
            allowed = ", ".join(item.value for item in SenderAction)
            raise ValueError(
                f"Неверный action: {action!r}. Ожидается: {allowed}"
            ) from e

    self.bot = bot
    self.chat_id = chat_id
    self.action = action

fetch() async

Выполняет POST-запрос для отправки действия в указанный чат.

Returns:

Name Type Description
SendedAction SendedAction

Результат выполнения запроса.

Source code in maxapi/methods/send_action.py
async def fetch(self) -> SendedAction:
    """
    Выполняет POST-запрос для отправки действия в указанный чат.

    Returns:
        SendedAction: Результат выполнения запроса.
    """

    bot = self._ensure_bot()

    json: dict[str, Any] = {}

    json["action"] = self.action

    response = await super().request(
        method=HTTPMethod.POST,
        path=ApiPath.CHATS + "/" + str(self.chat_id) + ApiPath.ACTIONS,
        model=SendedAction,
        params=bot.params,
        json=json,
    )

    return cast(SendedAction, response)