mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-25 00:27:46 +01:00
More instance methods (#963)
* Bot.get_file now allows passing a file in addition to file_id * Add .get_file() to Audio, Document, PhotoSize, Sticker, Video, VideoNote and Voice * Add .send_*() methods to User and Chat
This commit is contained in:
parent
dcb510e62c
commit
ebcc40ae92
19 changed files with 531 additions and 15 deletions
|
@ -1346,10 +1346,15 @@ class Bot(TelegramObject):
|
|||
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).
|
||||
|
@ -1364,6 +1369,11 @@ class Bot(TelegramObject):
|
|||
"""
|
||||
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)
|
||||
|
||||
|
|
104
telegram/chat.py
104
telegram/chat.py
|
@ -212,3 +212,107 @@ class Chat(TelegramObject):
|
|||
|
||||
"""
|
||||
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)
|
||||
|
|
|
@ -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 @@ class Audio(TelegramObject):
|
|||
title=None,
|
||||
mime_type=None,
|
||||
file_size=None,
|
||||
bot=None,
|
||||
**kwargs):
|
||||
# Required
|
||||
self.file_id = str(file_id)
|
||||
|
@ -61,6 +64,7 @@ class Audio(TelegramObject):
|
|||
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 @@ class Audio(TelegramObject):
|
|||
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)
|
||||
|
|
|
@ -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 @@ class Document(TelegramObject):
|
|||
file_name=None,
|
||||
mime_type=None,
|
||||
file_size=None,
|
||||
bot=None,
|
||||
**kwargs):
|
||||
# Required
|
||||
self.file_id = str(file_id)
|
||||
|
@ -56,6 +59,7 @@ class Document(TelegramObject):
|
|||
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 @@ class Document(TelegramObject):
|
|||
|
||||
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)
|
||||
|
|
|
@ -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 @@ class PhotoSize(TelegramObject):
|
|||
if not data:
|
||||
return None
|
||||
|
||||
return cls(**data)
|
||||
return cls(bot=bot, **data)
|
||||
|
||||
@classmethod
|
||||
def de_list(cls, data, bot):
|
||||
|
@ -66,3 +69,21 @@ class PhotoSize(TelegramObject):
|
|||
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)
|
||||
|
|
|
@ -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 @@ class Sticker(TelegramObject):
|
|||
file_size=None,
|
||||
set_name=None,
|
||||
mask_position=None,
|
||||
bot=None,
|
||||
**kwargs):
|
||||
# Required
|
||||
self.file_id = str(file_id)
|
||||
|
@ -72,6 +75,7 @@ class Sticker(TelegramObject):
|
|||
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 @@ class Sticker(TelegramObject):
|
|||
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 @@ class Sticker(TelegramObject):
|
|||
|
||||
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.
|
||||
|
|
|
@ -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 @@ class Video(TelegramObject):
|
|||
thumb=None,
|
||||
mime_type=None,
|
||||
file_size=None,
|
||||
bot=None,
|
||||
**kwargs):
|
||||
# Required
|
||||
self.file_id = str(file_id)
|
||||
|
@ -63,6 +66,7 @@ class Video(TelegramObject):
|
|||
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 @@ class Video(TelegramObject):
|
|||
|
||||
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)
|
||||
|
|
|
@ -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 @@ class VideoNote(TelegramObject):
|
|||
# Optionals
|
||||
self.thumb = thumb
|
||||
self.file_size = file_size
|
||||
self.bot = bot
|
||||
|
||||
self._id_attrs = (self.file_id,)
|
||||
|
||||
|
@ -61,4 +64,22 @@ class VideoNote(TelegramObject):
|
|||
|
||||
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)
|
||||
|
|
|
@ -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 @@ class Voice(TelegramObject):
|
|||
|
||||
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)
|
||||
|
|
106
telegram/user.py
106
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 @@ class User(TelegramObject):
|
|||
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)
|
||||
|
|
|
@ -166,6 +166,13 @@ class TestAudio(object):
|
|||
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)
|
||||
|
|
|
@ -124,6 +124,55 @@ class TestChat(object):
|
|||
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)
|
||||
|
|
|
@ -163,6 +163,13 @@ class TestDocument(object):
|
|||
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)
|
||||
|
|
|
@ -290,6 +290,13 @@ class TestPhoto(object):
|
|||
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)
|
||||
|
|
|
@ -277,6 +277,13 @@ class TestStickerSet(object):
|
|||
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)
|
||||
|
|
|
@ -90,7 +90,7 @@ class TestUser(object):
|
|||
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 @@ class TestUser(object):
|
|||
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)
|
||||
|
|
|
@ -185,6 +185,13 @@ class TestVideo(object):
|
|||
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)
|
||||
|
|
|
@ -146,6 +146,13 @@ class TestVideoNote(object):
|
|||
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)
|
||||
|
|
|
@ -151,6 +151,13 @@ class TestVoice(object):
|
|||
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)
|
||||
|
|
Loading…
Reference in a new issue