コード例 #1
0
ファイル: bot.py プロジェクト: devtud/atb
    async def send_message(
            self,
            chat_id: Union[int, str],
            text: str,
            parse_mode: Optional[str] = None,
            disable_web_page_preview: Optional[bool] = None,
            disable_notification: Optional[bool] = None,
            reply_to_message_id: Optional[int] = None,
            reply_markup: Optional[ReplyMarkupTypes] = None) -> Message:

        payload = {
            'chat_id': chat_id,
            'text': text,
        }

        if parse_mode:
            payload['parse_mode'] = parse_mode
        if disable_web_page_preview:
            payload['disable_web_page_preview'] = disable_web_page_preview
        if disable_notification:
            payload['disable_notification'] = disable_notification
        if reply_to_message_id:
            payload['reply_to_message_id'] = reply_to_message_id
        if reply_markup:
            payload['reply_markup'] = reply_markup.json(skip_defaults=True)

        result = await self.request(APIMethod.sendMessage, payload)

        return Message(**result)
コード例 #2
0
async def test_sendMessage(client_tgbot_pydantic, mock_aioresponse, error_code,
                           txt, json_response):
    if "_" in txt:
        # wrong markdown causes second attempt to call POST
        mock_aioresponse.post(
            f"https://api.telegram.org/bot{client_tgbot_pydantic.token}/sendMessage",
            status=400,
            payload={
                'ok':
                False,
                'error_code':
                400,
                'description':
                "Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 3"
            })
    mock_aioresponse.post(
        f"https://api.telegram.org/bot{client_tgbot_pydantic.token}/sendMessage",
        status=error_code,
        payload=json_response)

    if "_" in txt:
        # wrong markdown causes second attempt to call POST
        mock_aioresponse.post(
            f"https://api.telegram.org/bot{client_tgbot_pydantic.token}/sendMessage",
            status=400,
            payload={
                'ok':
                False,
                'error_code':
                400,
                'description':
                "Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 3"
            })
    mock_aioresponse.post(
        f"https://api.telegram.org/bot{client_tgbot_pydantic.token}/sendMessage",
        status=error_code,
        payload=json_response)

    response1 = await client_tgbot_pydantic.sendMessage(435627225, txt)
    response2 = await client_tgbot_pydantic.sendMessage(435627225,
                                                        txt,
                                                        parse_mode="markdown")

    assert type(response1) == Message
    assert response1 == Message.parse_obj(json_response["result"])
    assert type(response2) == Message
    assert response2 == Message.parse_obj(json_response["result"])
コード例 #3
0
 async def sendMessage(self, chat_id: int, text: str, **kwargs) -> Message:
     """
     Use this method to send text messages. On success, the sent Message is returned.
     :param chat_id:
     :param text:
     :param kwargs:
     :raises TgException
     :return:
     """
     response = await self._tg.sendMessage(chat_id, text, **kwargs)
     self._try_parse_result(response)
     return Message.parse_obj(response.payload["result"])
コード例 #4
0
 async def sendVideo(self, chat_id: int, **kwargs) -> Message:
     """
     Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document).
     On success, the sent Message is returned.
     Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future.
     :param chat_id:
     :param kwargs:
     :raises TgException
     :return:
     """
     response = await self._tg.sendVideo(chat_id, **kwargs)
     self._try_parse_result(response)
     return Message.parse_obj(response.payload["result"])
コード例 #5
0
 async def sendAnimation(self, chat_id: int, **kwargs) -> Message:
     """
     Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound).
     On success, the sent Message is returned.
     Bots can currently send animation files of up to 50 MB in size, this limit may be changed in the future.
     :param chat_id:
     :param kwargs:
     :raises TgException
     :return:
     """
     response = await self._tg.sendAnimation(chat_id, **kwargs)
     self._try_parse_result(response)
     return Message.parse_obj(response.payload["result"])
コード例 #6
0
 async def sendPhoto(self,
                     chat_id: int,
                     caption: str = "",
                     **kwargs) -> Message:
     """
     Use this method to send photos. On success, the sent Message is returned.
     :param chat_id:
     :param caption:
     :param kwargs:
     :raises TgException
     :return:
     """
     response = await self._tg.sendPhoto(chat_id, caption, **kwargs)
     self._try_parse_result(response)
     return Message.parse_obj(response.payload["result"])
コード例 #7
0
 async def sendMediaGroup(self, chat_id: int, media: list,
                          **kwargs) -> List[Message]:
     """
     Use this method to send a group of photos, videos, documents or audios as an album.
     Documents and audio files can be only grouped in an album with messages of the same type.
     On success, an array of Messages that were sent is returned.
     :param chat_id:
     :param media:
     :param kwargs:
     :raises TgException
     :return:
     """
     response = await self._tg.sendMediaGroup(chat_id, media, **kwargs)
     self._try_parse_result(response)
     return [Message.parse_obj(obj) for obj in response.payload["result"]]
コード例 #8
0
 async def editMessageText(self, chat_id, message_id, text,
                           **kwargs) -> Message:
     """
     Use this method to edit text and game messages.
     TODO: On success, if the edited message is not an inline message, the edited Message is returned, otherwise True is returne
     :param chat_id:
     :param message_id:
     :param text:
     :param kwargs:
     :raises TgException
     :return:
     """
     response = await self._tg.editMessageText(chat_id, message_id, text,
                                               **kwargs)
     self._try_parse_result(response)
     return Message.parse_obj(response.payload["result"])
コード例 #9
0
 async def editMessageMedia(self, chat_id: int, message_id: int,
                            media: dict, **kwargs) -> Message:
     """
     Use this method to edit animation, audio, document, photo, or video messages.
     If a message is part of a message album, then it can be edited only to an audio for audio albums,
     only to a document for document albums and to a photo or a video otherwise.
     When an inline message is edited, a new file can't be uploaded.
     Use a previously uploaded file via its file_id or specify a URL.
     TODO: On success, if the edited message was sent by the bot, the edited Message is returned, otherwise True is returned.
     :param chat_id:
     :param message_id:
     :param media:
     :param kwargs:
     :raises TgException
     :return:
     """
     response = await self._tg.editMessageMedia(chat_id, message_id, media,
                                                **kwargs)
     self._try_parse_result(response)
     return Message.parse_obj(response.payload["result"])