diff --git a/telegram/bot.py b/telegram/bot.py index ef66cf7dff5..252a6190927 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -1311,10 +1311,15 @@ def get_file(self, file_id, timeout=None, **kwargs): moment, bots can download files of up to 20MB in size. The file can then be downloaded with :attr:`telegram.File.download`. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by - calling getFile again. + calling get_file again. Args: - file_id (:obj:`str`): File identifier to get info about. + file_id (:obj:`str` | :class:`telegram.Audio` | :class:`telegram.Document` | \ + :class:`telegram.PhotoSize` | :class:`telegram.Sticker` | \ + :class:`telegram.Video` | :class:`telegram.VideoNote` | \ + :class:`telegram.Voice`): + Either the file identifier or an object that has a file_id attribute + to get file information about. timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as the read timeout from the server (instead of the one specified during creation of the connection pool). @@ -1329,6 +1334,11 @@ def get_file(self, file_id, timeout=None, **kwargs): """ url = '{0}/getFile'.format(self.base_url) + try: + file_id = file_id.file_id + except AttributeError: + pass + data = {'file_id': file_id} data.update(kwargs) diff --git a/telegram/chat.py b/telegram/chat.py index b1db4dca1d1..c9ed9832df6 100644 --- a/telegram/chat.py +++ b/telegram/chat.py @@ -212,3 +212,107 @@ def unban_member(self, *args, **kwargs): """ return self.bot.unban_chat_member(self.id, *args, **kwargs) + + def send_message(self, *args, **kwargs): + """Shortcut for:: + + bot.send_message(Chat.chat_id, *args, **kwargs) + + Where Chat is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_message(chat_id=self.id, *args, **kwargs) + + def send_photo(self, *args, **kwargs): + """Shortcut for:: + + bot.send_photo(Chat.chat_id, *args, **kwargs) + + Where Chat is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_photo(chat_id=self.id, *args, **kwargs) + + def send_audio(self, *args, **kwargs): + """Shortcut for:: + + bot.send_audio(Chat.chat_id, *args, **kwargs) + + Where Chat is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_audio(chat_id=self.id, *args, **kwargs) + + def send_document(self, *args, **kwargs): + """Shortcut for:: + + bot.send_document(Chat.chat_id, *args, **kwargs) + + Where Chat is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_document(chat_id=self.id, *args, **kwargs) + + def send_sticker(self, *args, **kwargs): + """Shortcut for:: + + bot.send_sticker(Chat.chat_id, *args, **kwargs) + + Where Chat is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_sticker(chat_id=self.id, *args, **kwargs) + + def send_video(self, *args, **kwargs): + """Shortcut for:: + + bot.send_video(Chat.chat_id, *args, **kwargs) + + Where Chat is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_video(chat_id=self.id, *args, **kwargs) + + def send_video_note(self, *args, **kwargs): + """Shortcut for:: + + bot.send_video_note(Chat.chat_id, *args, **kwargs) + + Where Chat is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_video_note(chat_id=self.id, *args, **kwargs) + + def send_voice(self, *args, **kwargs): + """Shortcut for:: + + bot.send_voice(Chat.chat_id, *args, **kwargs) + + Where Chat is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_voice(chat_id=self.id, *args, **kwargs) diff --git a/telegram/files/audio.py b/telegram/files/audio.py index 03913e52d55..9be69666560 100644 --- a/telegram/files/audio.py +++ b/telegram/files/audio.py @@ -32,6 +32,7 @@ class Audio(TelegramObject): title (:obj:`str`): Optional. Title of the audio as defined by sender or by audio tags. mime_type (:obj:`str`): Optional. MIME type of the file as defined by sender. file_size (:obj:`int`): Optional. File size. + bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods. Args: file_id (:obj:`str`): Unique identifier for this file. @@ -41,6 +42,7 @@ class Audio(TelegramObject): title (:obj:`str`, optional): Title of the audio as defined by sender or by audio tags. mime_type (:obj:`str`, optional): MIME type of the file as defined by sender. file_size (:obj:`int`, optional): File size. + bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods. **kwargs (:obj:`dict`): Arbitrary keyword arguments. """ @@ -52,6 +54,7 @@ def __init__(self, title=None, mime_type=None, file_size=None, + bot=None, **kwargs): # Required self.file_id = str(file_id) @@ -61,6 +64,7 @@ def __init__(self, self.title = title self.mime_type = mime_type self.file_size = file_size + self.bot = bot self._id_attrs = (self.file_id,) @@ -69,4 +73,22 @@ def de_json(cls, data, bot): if not data: return None - return cls(**data) + return cls(bot=bot, **data) + + def get_file(self, timeout=None, **kwargs): + """Convenience wrapper over :attr:`telegram.Bot.get_file` + + Args: + timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as + the read timeout from the server (instead of the one specified during creation of + the connection pool). + **kwargs (:obj:`dict`): Arbitrary keyword arguments. + + Returns: + :class:`telegram.File` + + Raises: + :class:`telegram.TelegramError` + + """ + return self.bot.get_file(self.file_id, timeout=timeout, **kwargs) diff --git a/telegram/files/document.py b/telegram/files/document.py index 73b27ea728a..85b58882581 100644 --- a/telegram/files/document.py +++ b/telegram/files/document.py @@ -30,6 +30,7 @@ class Document(TelegramObject): file_name (:obj:`str`): Original filename. mime_type (:obj:`str`): Optional. MIME type of the file. file_size (:obj:`int`): Optional. File size. + bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods. Args: file_id (:obj:`str`): Unique file identifier @@ -37,6 +38,7 @@ class Document(TelegramObject): file_name (:obj:`str`, optional): Original filename as defined by sender. mime_type (:obj:`str`, optional): MIME type of the file as defined by sender. file_size (:obj:`int`, optional): File size. + bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods. **kwargs (:obj:`dict`): Arbitrary keyword arguments. """ @@ -48,6 +50,7 @@ def __init__(self, file_name=None, mime_type=None, file_size=None, + bot=None, **kwargs): # Required self.file_id = str(file_id) @@ -56,6 +59,7 @@ def __init__(self, self.file_name = file_name self.mime_type = mime_type self.file_size = file_size + self.bot = bot self._id_attrs = (self.file_id,) @@ -68,4 +72,22 @@ def de_json(cls, data, bot): data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot) - return cls(**data) + return cls(**data, bot=bot) + + def get_file(self, timeout=None, **kwargs): + """Convenience wrapper over :attr:`telegram.Bot.get_file` + + Args: + timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as + the read timeout from the server (instead of the one specified during creation of + the connection pool). + **kwargs (:obj:`dict`): Arbitrary keyword arguments. + + Returns: + :class:`telegram.File` + + Raises: + :class:`telegram.TelegramError` + + """ + return self.bot.get_file(self.file_id, timeout=timeout, **kwargs) diff --git a/telegram/files/photosize.py b/telegram/files/photosize.py index fabd339520c..561fed5b436 100644 --- a/telegram/files/photosize.py +++ b/telegram/files/photosize.py @@ -29,23 +29,26 @@ class PhotoSize(TelegramObject): width (:obj:`int`): Photo width. height (:obj:`int`): Photo height. file_size (:obj:`int`): Optional. File size. + bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods. Args: file_id (:obj:`str`): Unique identifier for this file. width (:obj:`int`): Photo width. height (:obj:`int`): Photo height. file_size (:obj:`int`, optional): File size. + bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods. **kwargs (:obj:`dict`): Arbitrary keyword arguments. """ - def __init__(self, file_id, width, height, file_size=None, **kwargs): + def __init__(self, file_id, width, height, file_size=None, bot=None, **kwargs): # Required self.file_id = str(file_id) self.width = int(width) self.height = int(height) # Optionals self.file_size = file_size + self.bot = bot self._id_attrs = (self.file_id,) @@ -54,7 +57,7 @@ def de_json(cls, data, bot): if not data: return None - return cls(**data) + return cls(bot=bot, **data) @classmethod def de_list(cls, data, bot): @@ -66,3 +69,21 @@ def de_list(cls, data, bot): photos.append(cls.de_json(photo, bot)) return photos + + def get_file(self, timeout=None, **kwargs): + """Convenience wrapper over :attr:`telegram.Bot.get_file` + + Args: + timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as + the read timeout from the server (instead of the one specified during creation of + the connection pool). + **kwargs (:obj:`dict`): Arbitrary keyword arguments. + + Returns: + :class:`telegram.File` + + Raises: + :class:`telegram.TelegramError` + + """ + return self.bot.get_file(self.file_id, timeout=timeout, **kwargs) diff --git a/telegram/files/sticker.py b/telegram/files/sticker.py index cec8cd72baf..27444d8eaab 100644 --- a/telegram/files/sticker.py +++ b/telegram/files/sticker.py @@ -35,6 +35,7 @@ class Sticker(TelegramObject): mask_position (:class:`telegram.MaskPosition`): Optional. For mask stickers, the position where the mask should be placed. file_size (:obj:`int`): Optional. File size. + bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods. Args: file_id (:obj:`str`): Unique identifier for this file. @@ -48,7 +49,8 @@ class Sticker(TelegramObject): mask_position (:class:`telegram.MaskPosition`, optional): For mask stickers, the position where the mask should be placed. file_size (:obj:`int`, optional): File size. - **kwargs (obj:`dict`): Arbitrary keyword arguments. + **kwargs (obj:`dict`): Arbitrary keyword arguments.7 + bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods. """ @@ -61,6 +63,7 @@ def __init__(self, file_size=None, set_name=None, mask_position=None, + bot=None, **kwargs): # Required self.file_id = str(file_id) @@ -72,6 +75,7 @@ def __init__(self, self.file_size = file_size self.set_name = set_name self.mask_position = mask_position + self.bot = bot self._id_attrs = (self.file_id,) @@ -85,7 +89,7 @@ def de_json(cls, data, bot): data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot) data['mask_position'] = MaskPosition.de_json(data.get('mask_position'), bot) - return cls(**data) + return cls(bot=bot, **data) @classmethod def de_list(cls, data, bot): @@ -94,6 +98,24 @@ def de_list(cls, data, bot): return [cls.de_json(d, bot) for d in data] + def get_file(self, timeout=None, **kwargs): + """Convenience wrapper over :attr:`telegram.Bot.get_file` + + Args: + timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as + the read timeout from the server (instead of the one specified during creation of + the connection pool). + **kwargs (:obj:`dict`): Arbitrary keyword arguments. + + Returns: + :class:`telegram.File` + + Raises: + :class:`telegram.TelegramError` + + """ + return self.bot.get_file(self.file_id, timeout=timeout, **kwargs) + class StickerSet(TelegramObject): """This object represents a sticker set. diff --git a/telegram/files/video.py b/telegram/files/video.py index c53b873fcd4..f3c3efcdac6 100644 --- a/telegram/files/video.py +++ b/telegram/files/video.py @@ -32,6 +32,7 @@ class Video(TelegramObject): thumb (:class:`telegram.PhotoSize`): Optional. Video thumbnail. mime_type (:obj:`str`): Optional. Mime type of a file as defined by sender. file_size (:obj:`int`): Optional. File size. + bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods. Args: file_id (:obj:`str`): Unique identifier for this file. @@ -41,6 +42,7 @@ class Video(TelegramObject): thumb (:class:`telegram.PhotoSize`, optional): Video thumbnail. mime_type (:obj:`str`, optional): Mime type of a file as defined by sender. file_size (:obj:`int`, optional): File size. + bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods. **kwargs (:obj:`dict`): Arbitrary keyword arguments. """ @@ -53,6 +55,7 @@ def __init__(self, thumb=None, mime_type=None, file_size=None, + bot=None, **kwargs): # Required self.file_id = str(file_id) @@ -63,6 +66,7 @@ def __init__(self, self.thumb = thumb self.mime_type = mime_type self.file_size = file_size + self.bot = bot self._id_attrs = (self.file_id,) @@ -75,4 +79,22 @@ def de_json(cls, data, bot): data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot) - return cls(**data) + return cls(bot=bot, **data) + + def get_file(self, timeout=None, **kwargs): + """Convenience wrapper over :attr:`telegram.Bot.get_file` + + Args: + timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as + the read timeout from the server (instead of the one specified during creation of + the connection pool). + **kwargs (:obj:`dict`): Arbitrary keyword arguments. + + Returns: + :class:`telegram.File` + + Raises: + :class:`telegram.TelegramError` + + """ + return self.bot.get_file(self.file_id, timeout=timeout, **kwargs) diff --git a/telegram/files/videonote.py b/telegram/files/videonote.py index 2f17fdbbdf7..95bb0238752 100644 --- a/telegram/files/videonote.py +++ b/telegram/files/videonote.py @@ -30,6 +30,7 @@ class VideoNote(TelegramObject): duration (:obj:`int`): Duration of the video in seconds as defined by sender. thumb (:class:`telegram.PhotoSize`): Optional. Video thumbnail. file_size (:obj:`int`): Optional. File size. + bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods. Args: file_id (:obj:`str`): Unique identifier for this file. @@ -37,11 +38,12 @@ class VideoNote(TelegramObject): duration (:obj:`int`): Duration of the video in seconds as defined by sender. thumb (:class:`telegram.PhotoSize`, optional): Video thumbnail. file_size (:obj:`int`, optional): File size. + bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods. **kwargs (:obj:`dict`): Arbitrary keyword arguments. """ - def __init__(self, file_id, length, duration, thumb=None, file_size=None, **kwargs): + def __init__(self, file_id, length, duration, thumb=None, file_size=None, bot=None, **kwargs): # Required self.file_id = str(file_id) self.length = int(length) @@ -49,6 +51,7 @@ def __init__(self, file_id, length, duration, thumb=None, file_size=None, **kwar # Optionals self.thumb = thumb self.file_size = file_size + self.bot = bot self._id_attrs = (self.file_id,) @@ -61,4 +64,22 @@ def de_json(cls, data, bot): data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot) - return cls(**data) + return cls(bot=bot, **data) + + def get_file(self, timeout=None, **kwargs): + """Convenience wrapper over :attr:`telegram.Bot.get_file` + + Args: + timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as + the read timeout from the server (instead of the one specified during creation of + the connection pool). + **kwargs (:obj:`dict`): Arbitrary keyword arguments. + + Returns: + :class:`telegram.File` + + Raises: + :class:`telegram.TelegramError` + + """ + return self.bot.get_file(self.file_id, timeout=timeout, **kwargs) diff --git a/telegram/files/voice.py b/telegram/files/voice.py index bc324a3f3e3..4f2f870964a 100644 --- a/telegram/files/voice.py +++ b/telegram/files/voice.py @@ -29,23 +29,26 @@ class Voice(TelegramObject): duration (:obj:`int`): Duration of the audio in seconds as defined by sender. mime_type (:obj:`str`): Optional. MIME type of the file as defined by sender. file_size (:obj:`int`): Optional. File size. + bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods. Args: file_id (:obj:`str`): Unique identifier for this file. duration (:obj:`int`, optional): Duration of the audio in seconds as defined by sender. mime_type (:obj:`str`, optional): MIME type of the file as defined by sender. file_size (:obj:`int`, optional): File size. + bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods. **kwargs (:obj:`dict`): Arbitrary keyword arguments. """ - def __init__(self, file_id, duration, mime_type=None, file_size=None, **kwargs): + def __init__(self, file_id, duration, mime_type=None, file_size=None, bot=None, **kwargs): # Required self.file_id = str(file_id) self.duration = int(duration) # Optionals self.mime_type = mime_type self.file_size = file_size + self.bot = bot self._id_attrs = (self.file_id,) @@ -56,4 +59,22 @@ def de_json(cls, data, bot): data = super(Voice, cls).de_json(data, bot) - return cls(**data) + return cls(bot=bot, **data) + + def get_file(self, timeout=None, **kwargs): + """Convenience wrapper over :attr:`telegram.Bot.get_file` + + Args: + timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as + the read timeout from the server (instead of the one specified during creation of + the connection pool). + **kwargs (:obj:`dict`): Arbitrary keyword arguments. + + Returns: + :class:`telegram.File` + + Raises: + :class:`telegram.TelegramError` + + """ + return self.bot.get_file(self.file_id, timeout=timeout, **kwargs) diff --git a/telegram/user.py b/telegram/user.py index 66024250e06..f56e23fa4df 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -20,8 +20,8 @@ """This module contains an object that represents a Telegram User.""" from telegram import TelegramObject -from telegram.utils.helpers import mention_markdown as util_mention_markdown from telegram.utils.helpers import mention_html as util_mention_html +from telegram.utils.helpers import mention_markdown as util_mention_markdown class User(TelegramObject): @@ -146,3 +146,107 @@ def mention_html(self, name=None): return util_mention_html(self.id, self.name) else: return util_mention_html(self.id, name) + + def send_message(self, *args, **kwargs): + """Shortcut for:: + + bot.send_message(User.chat_id, *args, **kwargs) + + Where User is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_message(chat_id=self.id, *args, **kwargs) + + def send_photo(self, *args, **kwargs): + """Shortcut for:: + + bot.send_photo(User.chat_id, *args, **kwargs) + + Where User is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_photo(chat_id=self.id, *args, **kwargs) + + def send_audio(self, *args, **kwargs): + """Shortcut for:: + + bot.send_audio(User.chat_id, *args, **kwargs) + + Where User is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_audio(chat_id=self.id, *args, **kwargs) + + def send_document(self, *args, **kwargs): + """Shortcut for:: + + bot.send_document(User.chat_id, *args, **kwargs) + + Where User is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_document(chat_id=self.id, *args, **kwargs) + + def send_sticker(self, *args, **kwargs): + """Shortcut for:: + + bot.send_sticker(User.chat_id, *args, **kwargs) + + Where User is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_sticker(chat_id=self.id, *args, **kwargs) + + def send_video(self, *args, **kwargs): + """Shortcut for:: + + bot.send_video(User.chat_id, *args, **kwargs) + + Where User is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_video(chat_id=self.id, *args, **kwargs) + + def send_video_note(self, *args, **kwargs): + """Shortcut for:: + + bot.send_video_note(User.chat_id, *args, **kwargs) + + Where User is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_video_note(chat_id=self.id, *args, **kwargs) + + def send_voice(self, *args, **kwargs): + """Shortcut for:: + + bot.send_voice(User.chat_id, *args, **kwargs) + + Where User is the current instance. + + Returns: + :class:`telegram.Message`: On success, instance representing the message posted. + + """ + return self.bot.send_voice(chat_id=self.id, *args, **kwargs) diff --git a/tests/test_audio.py b/tests/test_audio.py index efad15925f7..610b9d7b385 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -165,6 +165,13 @@ def test_error_send_without_required_args(self, bot, chat_id): with pytest.raises(TypeError): bot.send_audio(chat_id=chat_id) + def test_get_file_instance_method(self, monkeypatch, audio): + def test(*args, **kwargs): + return args[1] == audio.file_id + + monkeypatch.setattr('telegram.Bot.get_file', test) + assert audio.get_file() + def test_equality(self, audio): a = Audio(audio.file_id, audio.duration) b = Audio(audio.file_id, audio.duration) diff --git a/tests/test_chat.py b/tests/test_chat.py index dd4e3ee22fb..5655679cab1 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -124,6 +124,55 @@ def test(*args, **kwargs): monkeypatch.setattr('telegram.Bot.unban_chat_member', test) assert chat.unban_member(42) + def test_instance_method_send_message(self, monkeypatch, chat): + def test(*args, **kwargs): + return kwargs['chat_id'] == chat.id and args[1] == 'test' + + monkeypatch.setattr('telegram.Bot.send_message', test) + assert chat.send_message('test') + + def test_instance_method_send_audio(self, monkeypatch, chat): + def test(*args, **kwargs): + return kwargs['chat_id'] == chat.id and kwargs['audio'] == 'test_audio' + + monkeypatch.setattr('telegram.Bot.send_audio', test) + assert chat.send_audio(audio='test_audio') + + def test_instance_method_send_document(self, monkeypatch, chat): + def test(*args, **kwargs): + return kwargs['chat_id'] == chat.id and kwargs['document'] == 'test_document' + + monkeypatch.setattr('telegram.Bot.send_document', test) + assert chat.send_document(document='test_document') + + def test_instance_method_send_sticker(self, monkeypatch, chat): + def test(*args, **kwargs): + return kwargs['chat_id'] == chat.id and kwargs['sticker'] == 'test_sticker' + + monkeypatch.setattr('telegram.Bot.send_sticker', test) + assert chat.send_sticker(sticker='test_sticker') + + def test_instance_method_send_video(self, monkeypatch, chat): + def test(*args, **kwargs): + return kwargs['chat_id'] == chat.id and kwargs['video'] == 'test_video' + + monkeypatch.setattr('telegram.Bot.send_video', test) + assert chat.send_video(video='test_video') + + def test_instance_method_send_video_note(self, monkeypatch, chat): + def test(*args, **kwargs): + return kwargs['chat_id'] == chat.id and kwargs['video_note'] == 'test_video_note' + + monkeypatch.setattr('telegram.Bot.send_video_note', test) + assert chat.send_video_note(video_note='test_video_note') + + def test_instance_method_send_voice(self, monkeypatch, chat): + def test(*args, **kwargs): + return kwargs['chat_id'] == chat.id and kwargs['voice'] == 'test_voice' + + monkeypatch.setattr('telegram.Bot.send_voice', test) + assert chat.send_voice(voice='test_voice') + def test_equality(self): a = Chat(self.id, self.title, self.type) b = Chat(self.id, self.title, self.type) diff --git a/tests/test_document.py b/tests/test_document.py index ef92203a972..0dfc645741a 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -162,6 +162,13 @@ def test_error_send_without_required_args(self, bot, chat_id): with pytest.raises(TypeError): bot.send_document(chat_id=chat_id) + def test_get_file_instance_method(self, monkeypatch, document): + def test(*args, **kwargs): + return args[1] == document.file_id + + monkeypatch.setattr('telegram.Bot.get_file', test) + assert document.get_file() + def test_equality(self, document): a = Document(document.file_id) b = Document(document.file_id) diff --git a/tests/test_photo.py b/tests/test_photo.py index 0563c485b1e..28564d0c410 100644 --- a/tests/test_photo.py +++ b/tests/test_photo.py @@ -246,6 +246,13 @@ def test_error_without_required_args(self, bot, chat_id): with pytest.raises(TypeError): bot.send_photo(chat_id=chat_id) + def test_get_file_instance_method(self, monkeypatch, photo): + def test(*args, **kwargs): + return args[1] == photo.file_id + + monkeypatch.setattr('telegram.Bot.get_file', test) + assert photo.get_file() + def test_equality(self, photo): a = PhotoSize(photo.file_id, self.width, self.height) b = PhotoSize(photo.file_id, self.width, self.height) diff --git a/tests/test_sticker.py b/tests/test_sticker.py index d6aa68b23e6..bbe3ec42df6 100644 --- a/tests/test_sticker.py +++ b/tests/test_sticker.py @@ -277,6 +277,13 @@ def test_bot_methods_3(self, bot, sticker_set): file_id = sticker_set.stickers[-1].file_id assert bot.delete_sticker_from_set(file_id) + def test_get_file_instance_method(self, monkeypatch, sticker): + def test(*args, **kwargs): + return args[1] == sticker.file_id + + monkeypatch.setattr('telegram.Bot.get_file', test) + assert sticker.get_file() + def test_equality(self): a = StickerSet(self.name, self.title, self.contains_masks, self.stickers) b = StickerSet(self.name, self.title, self.contains_masks, self.stickers) diff --git a/tests/test_user.py b/tests/test_user.py index 14ddab2e2fb..8bf275a28ec 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -90,7 +90,7 @@ def test_name(self, user): assert user.name == 'first_name' user.username = self.username assert user.name == '@username' - + def test_full_name(self, user): assert user.full_name == 'first_name last_name' user.last_name = None @@ -103,6 +103,55 @@ def test(_, *args, **kwargs): monkeypatch.setattr('telegram.Bot.get_user_profile_photos', test) assert user.get_profile_photos() + def test_instance_method_send_message(self, monkeypatch, user): + def test(*args, **kwargs): + return kwargs['chat_id'] == user.id and args[1] == 'test' + + monkeypatch.setattr('telegram.Bot.send_message', test) + assert user.send_message('test') + + def test_instance_method_send_audio(self, monkeypatch, user): + def test(*args, **kwargs): + return kwargs['chat_id'] == user.id and kwargs['audio'] == 'test_audio' + + monkeypatch.setattr('telegram.Bot.send_audio', test) + assert user.send_audio(audio='test_audio') + + def test_instance_method_send_document(self, monkeypatch, user): + def test(*args, **kwargs): + return kwargs['chat_id'] == user.id and kwargs['document'] == 'test_document' + + monkeypatch.setattr('telegram.Bot.send_document', test) + assert user.send_document(document='test_document') + + def test_instance_method_send_sticker(self, monkeypatch, user): + def test(*args, **kwargs): + return kwargs['chat_id'] == user.id and kwargs['sticker'] == 'test_sticker' + + monkeypatch.setattr('telegram.Bot.send_sticker', test) + assert user.send_sticker(sticker='test_sticker') + + def test_instance_method_send_video(self, monkeypatch, user): + def test(*args, **kwargs): + return kwargs['chat_id'] == user.id and kwargs['video'] == 'test_video' + + monkeypatch.setattr('telegram.Bot.send_video', test) + assert user.send_video(video='test_video') + + def test_instance_method_send_video_note(self, monkeypatch, user): + def test(*args, **kwargs): + return kwargs['chat_id'] == user.id and kwargs['video_note'] == 'test_video_note' + + monkeypatch.setattr('telegram.Bot.send_video_note', test) + assert user.send_video_note(video_note='test_video_note') + + def test_instance_method_send_voice(self, monkeypatch, user): + def test(*args, **kwargs): + return kwargs['chat_id'] == user.id and kwargs['voice'] == 'test_voice' + + monkeypatch.setattr('telegram.Bot.send_voice', test) + assert user.send_voice(voice='test_voice') + def test_equality(self): a = User(self.id, self.first_name, self.is_bot, self.last_name) b = User(self.id, self.first_name, self.is_bot, self.last_name) diff --git a/tests/test_video.py b/tests/test_video.py index 9eec87cb10a..32a477eb013 100644 --- a/tests/test_video.py +++ b/tests/test_video.py @@ -183,6 +183,13 @@ def test_error_without_required_args(self, bot, chat_id): with pytest.raises(TypeError): bot.send_video(chat_id=chat_id) + def test_get_file_instance_method(self, monkeypatch, video): + def test(*args, **kwargs): + return args[1] == video.file_id + + monkeypatch.setattr('telegram.Bot.get_file', test) + assert video.get_file() + def test_equality(self, video): a = Video(video.file_id, self.width, self.height, self.duration) b = Video(video.file_id, self.width, self.height, self.duration) diff --git a/tests/test_videonote.py b/tests/test_videonote.py index 3e386642ff1..2998885fa2a 100644 --- a/tests/test_videonote.py +++ b/tests/test_videonote.py @@ -146,6 +146,13 @@ def test_error_without_required_args(self, bot, chat_id): with pytest.raises(TypeError): bot.send_video_note(chat_id=chat_id) + def test_get_file_instance_method(self, monkeypatch, video_note): + def test(*args, **kwargs): + return args[1] == video_note.file_id + + monkeypatch.setattr('telegram.Bot.get_file', test) + assert video_note.get_file() + def test_equality(self, video_note): a = VideoNote(video_note.file_id, self.length, self.duration) b = VideoNote(video_note.file_id, self.length, self.duration) diff --git a/tests/test_voice.py b/tests/test_voice.py index e12106eb565..fa676cca270 100644 --- a/tests/test_voice.py +++ b/tests/test_voice.py @@ -150,6 +150,13 @@ def test_error_without_required_args(self, bot, chat_id): with pytest.raises(TypeError): bot.sendVoice(chat_id) + def test_get_file_instance_method(self, monkeypatch, voice): + def test(*args, **kwargs): + return args[1] == voice.file_id + + monkeypatch.setattr('telegram.Bot.get_file', test) + assert voice.get_file() + def test_equality(self, voice): a = Voice(voice.file_id, self.duration) b = Voice(voice.file_id, self.duration) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy