def __init__(self, media, caption=None, width=None, height=None, duration=None,
                 supports_streaming=None, parse_mode=DEFAULT_NONE, thumb=None):
        self.type = 'video'

        if isinstance(media, Video):
            self.media = media.file_id
            self.width = media.width
            self.height = media.height
            self.duration = media.duration
        elif InputFile.is_file(media):
            self.media = InputFile(media, attach=True)
        else:
            self.media = media

        if thumb:
            self.thumb = thumb
            if InputFile.is_file(self.thumb):
                self.thumb = InputFile(self.thumb, attach=True)

        if caption:
            self.caption = caption
        self.parse_mode = parse_mode
        if width:
            self.width = width
        if height:
            self.height = height
        if duration:
            self.duration = duration
        if supports_streaming:
            self.supports_streaming = supports_streaming
    def __init__(self,
                 media,
                 thumb=None,
                 caption=None,
                 parse_mode=DEFAULT_NONE,
                 width=None,
                 height=None,
                 duration=None):
        self.type = 'animation'

        if isinstance(media, Animation):
            self.media = media.file_id
            self.width = media.width
            self.height = media.height
            self.duration = media.duration
        elif InputFile.is_file(media):
            self.media = InputFile(media, attach=True)
        else:
            self.media = media

        if thumb:
            self.thumb = thumb
            if InputFile.is_file(self.thumb):
                self.thumb = InputFile(self.thumb, attach=True)

        if caption:
            self.caption = caption
        self.parse_mode = parse_mode
        if width:
            self.width = width
        if height:
            self.height = height
        if duration:
            self.duration = duration
    def __init__(self, media, thumb=None, caption=None, parse_mode=DEFAULT_NONE,
                 duration=None, performer=None, title=None):
        self.type = 'audio'

        if isinstance(media, Audio):
            self.media = media.file_id
            self.duration = media.duration
            self.performer = media.performer
            self.title = media.title
        elif InputFile.is_file(media):
            self.media = InputFile(media, attach=True)
        else:
            self.media = media

        if thumb:
            self.thumb = thumb
            if InputFile.is_file(self.thumb):
                self.thumb = InputFile(self.thumb, attach=True)

        if caption:
            self.caption = caption
        self.parse_mode = parse_mode
        if duration:
            self.duration = duration
        if performer:
            self.performer = performer
        if title:
            self.title = title
Пример #4
0
    def __init__(
        self,
        media: Union[str, FileLike, Document],
        thumb: FileLike = None,
        caption: str = None,
        parse_mode: Union[str, DefaultValue] = DEFAULT_NONE,
    ):
        self.type = 'document'

        if isinstance(media, Document):
            self.media: Union[str, InputFile] = media.file_id
        elif InputFile.is_file(media):
            media = cast(IO, media)
            self.media = InputFile(media, attach=True)
        else:
            self.media = media  # type: ignore[assignment]

        if thumb:
            if InputFile.is_file(thumb):
                thumb = cast(IO, thumb)
                self.thumb = InputFile(thumb, attach=True)
            else:
                self.thumb = thumb  # type: ignore[assignment]

        if caption:
            self.caption = caption
        self.parse_mode = parse_mode
Пример #5
0
    def _prepare_request_payload(data, png_sticker=None, tgs_sticker=None):
        if png_sticker is not None:
            if InputFile.is_file(png_sticker):
                png_sticker = InputFile(png_sticker)

            data['png_sticker'] = png_sticker

        if tgs_sticker is not None:
            if InputFile.is_file(tgs_sticker):
                tgs_sticker = InputFile(tgs_sticker)

            data['tgs_sticker'] = tgs_sticker

        return data
    def __init__(self, media, thumb=None, caption=None, parse_mode=DEFAULT_NONE):
        self.type = 'document'

        if isinstance(media, Document):
            self.media = media.file_id
        elif InputFile.is_file(media):
            self.media = InputFile(media, attach=True)
        else:
            self.media = media

        if thumb:
            self.thumb = thumb
            if InputFile.is_file(self.thumb):
                self.thumb = InputFile(self.thumb, attach=True)

        if caption:
            self.caption = caption
        self.parse_mode = parse_mode
Пример #7
0
    def __init__(
        self,
        media: Union[str, FileLike, Video],
        caption: str = None,
        width: int = None,
        height: int = None,
        duration: int = None,
        supports_streaming: bool = None,
        parse_mode: Union[str, DefaultValue] = DEFAULT_NONE,
        thumb: FileLike = None,
    ):
        self.type = 'video'

        if isinstance(media, Video):
            self.media: Union[str, InputFile] = media.file_id
            self.width = media.width
            self.height = media.height
            self.duration = media.duration
        elif InputFile.is_file(media):
            media = cast(IO, media)
            self.media = InputFile(media, attach=True)
        else:
            self.media = media  # type: ignore[assignment]

        if thumb:
            if InputFile.is_file(thumb):
                thumb = cast(IO, thumb)
                self.thumb = InputFile(thumb, attach=True)
            else:
                self.thumb = thumb  # type: ignore[assignment]

        if caption:
            self.caption = caption
        self.parse_mode = parse_mode
        if width:
            self.width = width
        if height:
            self.height = height
        if duration:
            self.duration = duration
        if supports_streaming:
            self.supports_streaming = supports_streaming
    def __init__(self, media, caption=None, parse_mode=DEFAULT_NONE):
        self.type = 'photo'

        if isinstance(media, PhotoSize):
            self.media = media.file_id
        elif InputFile.is_file(media):
            self.media = InputFile(media, attach=True)
        else:
            self.media = media

        if caption:
            self.caption = caption
        self.parse_mode = parse_mode
Пример #9
0
    def __init__(
        self,
        media: Union[str, FileLike, Audio],
        thumb: FileLike = None,
        caption: str = None,
        parse_mode: Union[str, DefaultValue] = DEFAULT_NONE,
        duration: int = None,
        performer: str = None,
        title: str = None,
    ):
        self.type = 'audio'

        if isinstance(media, Audio):
            self.media: Union[str, InputFile] = media.file_id
            self.duration = media.duration
            self.performer = media.performer
            self.title = media.title
        elif InputFile.is_file(media):
            media = cast(IO, media)
            self.media = InputFile(media, attach=True)
        else:
            self.media = media  # type: ignore[assignment]

        if thumb:
            if InputFile.is_file(thumb):
                thumb = cast(IO, thumb)
                self.thumb = InputFile(thumb, attach=True)
            else:
                self.thumb = thumb  # type: ignore[assignment]

        if caption:
            self.caption = caption
        self.parse_mode = parse_mode
        if duration:
            self.duration = duration
        if performer:
            self.performer = performer
        if title:
            self.title = title
Пример #10
0
def parse_file_input(
    file_input: Union[FileInput, 'TelegramObject'],
    tg_type: Type['TelegramObject'] = None,
    attach: bool = None,
    filename: str = None,
) -> Union[str, 'InputFile', Any]:
    """
    Parses input for sending files:

    * For string input, if the input is an absolute path of a local file,
      adds the ``file://`` prefix. If the input is a relative path of a local file, computes the
      absolute path and adds the ``file://`` prefix. Returns the input unchanged, otherwise.
    * :class:`pathlib.Path` objects are treated the same way as strings.
    * For IO and bytes input, returns an :class:`telegram.InputFile`.
    * If :attr:`tg_type` is specified and the input is of that type, returns the ``file_id``
      attribute.

    Args:
        file_input (:obj:`str` | :obj:`bytes` | `filelike object` | Telegram media object): The
            input to parse.
        tg_type (:obj:`type`, optional): The Telegram media type the input can be. E.g.
            :class:`telegram.Animation`.
        attach (:obj:`bool`, optional): Whether this file should be send as one file or is part of
            a collection of files. Only relevant in case an :class:`telegram.InputFile` is
            returned.
        filename (:obj:`str`, optional): The filename. Only relevant in case an
            :class:`telegram.InputFile` is returned.

    Returns:
        :obj:`str` | :class:`telegram.InputFile` | :obj:`object`: The parsed input or the untouched
        :attr:`file_input`, in case it's no valid file input.
    """
    # Importing on file-level yields cyclic Import Errors
    from telegram import InputFile  # pylint: disable=C0415

    if isinstance(file_input, str) and file_input.startswith('file://'):
        return file_input
    if isinstance(file_input, (str, Path)):
        if is_local_file(file_input):
            out = Path(file_input).absolute().as_uri()
        else:
            out = file_input  # type: ignore[assignment]
        return out
    if isinstance(file_input, bytes):
        return InputFile(file_input, attach=attach, filename=filename)
    if InputFile.is_file(file_input):
        file_input = cast(IO, file_input)
        return InputFile(file_input, attach=attach, filename=filename)
    if tg_type and isinstance(file_input, tg_type):
        return file_input.file_id  # type: ignore[attr-defined]
    return file_input
Пример #11
0
    def __init__(self,
                 media: Union[str, FileLike, PhotoSize],
                 caption: str = None,
                 parse_mode: Union[str, DefaultValue] = DEFAULT_NONE):
        self.type = 'photo'

        if isinstance(media, PhotoSize):
            self.media: Union[str, InputFile] = media.file_id
        elif InputFile.is_file(media):
            media = cast(IO, media)
            self.media = InputFile(media, attach=True)
        else:
            self.media = media  # type: ignore[assignment]

        if caption:
            self.caption = caption
        self.parse_mode = parse_mode