Skip to content

Commit ebcc40a

Browse files
jsmnbomtsnoam
authored andcommitted
More instance methods (python-telegram-bot#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
1 parent dcb510e commit ebcc40a

19 files changed

+531
-15
lines changed

telegram/bot.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,10 +1346,15 @@ def get_file(self, file_id, timeout=None, **kwargs):
13461346
moment, bots can download files of up to 20MB in size. The file can then be downloaded
13471347
with :attr:`telegram.File.download`. It is guaranteed that the link will be
13481348
valid for at least 1 hour. When the link expires, a new one can be requested by
1349-
calling getFile again.
1349+
calling get_file again.
13501350
13511351
Args:
1352-
file_id (:obj:`str`): File identifier to get info about.
1352+
file_id (:obj:`str` | :class:`telegram.Audio` | :class:`telegram.Document` | \
1353+
:class:`telegram.PhotoSize` | :class:`telegram.Sticker` | \
1354+
:class:`telegram.Video` | :class:`telegram.VideoNote` | \
1355+
:class:`telegram.Voice`):
1356+
Either the file identifier or an object that has a file_id attribute
1357+
to get file information about.
13531358
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
13541359
the read timeout from the server (instead of the one specified during creation of
13551360
the connection pool).
@@ -1364,6 +1369,11 @@ def get_file(self, file_id, timeout=None, **kwargs):
13641369
"""
13651370
url = '{0}/getFile'.format(self.base_url)
13661371

1372+
try:
1373+
file_id = file_id.file_id
1374+
except AttributeError:
1375+
pass
1376+
13671377
data = {'file_id': file_id}
13681378
data.update(kwargs)
13691379

telegram/chat.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,107 @@ def unban_member(self, *args, **kwargs):
212212
213213
"""
214214
return self.bot.unban_chat_member(self.id, *args, **kwargs)
215+
216+
def send_message(self, *args, **kwargs):
217+
"""Shortcut for::
218+
219+
bot.send_message(Chat.chat_id, *args, **kwargs)
220+
221+
Where Chat is the current instance.
222+
223+
Returns:
224+
:class:`telegram.Message`: On success, instance representing the message posted.
225+
226+
"""
227+
return self.bot.send_message(chat_id=self.id, *args, **kwargs)
228+
229+
def send_photo(self, *args, **kwargs):
230+
"""Shortcut for::
231+
232+
bot.send_photo(Chat.chat_id, *args, **kwargs)
233+
234+
Where Chat is the current instance.
235+
236+
Returns:
237+
:class:`telegram.Message`: On success, instance representing the message posted.
238+
239+
"""
240+
return self.bot.send_photo(chat_id=self.id, *args, **kwargs)
241+
242+
def send_audio(self, *args, **kwargs):
243+
"""Shortcut for::
244+
245+
bot.send_audio(Chat.chat_id, *args, **kwargs)
246+
247+
Where Chat is the current instance.
248+
249+
Returns:
250+
:class:`telegram.Message`: On success, instance representing the message posted.
251+
252+
"""
253+
return self.bot.send_audio(chat_id=self.id, *args, **kwargs)
254+
255+
def send_document(self, *args, **kwargs):
256+
"""Shortcut for::
257+
258+
bot.send_document(Chat.chat_id, *args, **kwargs)
259+
260+
Where Chat is the current instance.
261+
262+
Returns:
263+
:class:`telegram.Message`: On success, instance representing the message posted.
264+
265+
"""
266+
return self.bot.send_document(chat_id=self.id, *args, **kwargs)
267+
268+
def send_sticker(self, *args, **kwargs):
269+
"""Shortcut for::
270+
271+
bot.send_sticker(Chat.chat_id, *args, **kwargs)
272+
273+
Where Chat is the current instance.
274+
275+
Returns:
276+
:class:`telegram.Message`: On success, instance representing the message posted.
277+
278+
"""
279+
return self.bot.send_sticker(chat_id=self.id, *args, **kwargs)
280+
281+
def send_video(self, *args, **kwargs):
282+
"""Shortcut for::
283+
284+
bot.send_video(Chat.chat_id, *args, **kwargs)
285+
286+
Where Chat is the current instance.
287+
288+
Returns:
289+
:class:`telegram.Message`: On success, instance representing the message posted.
290+
291+
"""
292+
return self.bot.send_video(chat_id=self.id, *args, **kwargs)
293+
294+
def send_video_note(self, *args, **kwargs):
295+
"""Shortcut for::
296+
297+
bot.send_video_note(Chat.chat_id, *args, **kwargs)
298+
299+
Where Chat is the current instance.
300+
301+
Returns:
302+
:class:`telegram.Message`: On success, instance representing the message posted.
303+
304+
"""
305+
return self.bot.send_video_note(chat_id=self.id, *args, **kwargs)
306+
307+
def send_voice(self, *args, **kwargs):
308+
"""Shortcut for::
309+
310+
bot.send_voice(Chat.chat_id, *args, **kwargs)
311+
312+
Where Chat is the current instance.
313+
314+
Returns:
315+
:class:`telegram.Message`: On success, instance representing the message posted.
316+
317+
"""
318+
return self.bot.send_voice(chat_id=self.id, *args, **kwargs)

telegram/files/audio.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Audio(TelegramObject):
3232
title (:obj:`str`): Optional. Title of the audio as defined by sender or by audio tags.
3333
mime_type (:obj:`str`): Optional. MIME type of the file as defined by sender.
3434
file_size (:obj:`int`): Optional. File size.
35+
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.
3536
3637
Args:
3738
file_id (:obj:`str`): Unique identifier for this file.
@@ -41,6 +42,7 @@ class Audio(TelegramObject):
4142
title (:obj:`str`, optional): Title of the audio as defined by sender or by audio tags.
4243
mime_type (:obj:`str`, optional): MIME type of the file as defined by sender.
4344
file_size (:obj:`int`, optional): File size.
45+
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
4446
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
4547
4648
"""
@@ -52,6 +54,7 @@ def __init__(self,
5254
title=None,
5355
mime_type=None,
5456
file_size=None,
57+
bot=None,
5558
**kwargs):
5659
# Required
5760
self.file_id = str(file_id)
@@ -61,6 +64,7 @@ def __init__(self,
6164
self.title = title
6265
self.mime_type = mime_type
6366
self.file_size = file_size
67+
self.bot = bot
6468

6569
self._id_attrs = (self.file_id,)
6670

@@ -69,4 +73,22 @@ def de_json(cls, data, bot):
6973
if not data:
7074
return None
7175

72-
return cls(**data)
76+
return cls(bot=bot, **data)
77+
78+
def get_file(self, timeout=None, **kwargs):
79+
"""Convenience wrapper over :attr:`telegram.Bot.get_file`
80+
81+
Args:
82+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
83+
the read timeout from the server (instead of the one specified during creation of
84+
the connection pool).
85+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
86+
87+
Returns:
88+
:class:`telegram.File`
89+
90+
Raises:
91+
:class:`telegram.TelegramError`
92+
93+
"""
94+
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)

telegram/files/document.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ class Document(TelegramObject):
3030
file_name (:obj:`str`): Original filename.
3131
mime_type (:obj:`str`): Optional. MIME type of the file.
3232
file_size (:obj:`int`): Optional. File size.
33+
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.
3334
3435
Args:
3536
file_id (:obj:`str`): Unique file identifier
3637
thumb (:class:`telegram.PhotoSize`, optional): Document thumbnail as defined by sender.
3738
file_name (:obj:`str`, optional): Original filename as defined by sender.
3839
mime_type (:obj:`str`, optional): MIME type of the file as defined by sender.
3940
file_size (:obj:`int`, optional): File size.
41+
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
4042
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
4143
4244
"""
@@ -48,6 +50,7 @@ def __init__(self,
4850
file_name=None,
4951
mime_type=None,
5052
file_size=None,
53+
bot=None,
5154
**kwargs):
5255
# Required
5356
self.file_id = str(file_id)
@@ -56,6 +59,7 @@ def __init__(self,
5659
self.file_name = file_name
5760
self.mime_type = mime_type
5861
self.file_size = file_size
62+
self.bot = bot
5963

6064
self._id_attrs = (self.file_id,)
6165

@@ -68,4 +72,22 @@ def de_json(cls, data, bot):
6872

6973
data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)
7074

71-
return cls(**data)
75+
return cls(**data, bot=bot)
76+
77+
def get_file(self, timeout=None, **kwargs):
78+
"""Convenience wrapper over :attr:`telegram.Bot.get_file`
79+
80+
Args:
81+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
82+
the read timeout from the server (instead of the one specified during creation of
83+
the connection pool).
84+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
85+
86+
Returns:
87+
:class:`telegram.File`
88+
89+
Raises:
90+
:class:`telegram.TelegramError`
91+
92+
"""
93+
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)

telegram/files/photosize.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,26 @@ class PhotoSize(TelegramObject):
2929
width (:obj:`int`): Photo width.
3030
height (:obj:`int`): Photo height.
3131
file_size (:obj:`int`): Optional. File size.
32+
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.
3233
3334
Args:
3435
file_id (:obj:`str`): Unique identifier for this file.
3536
width (:obj:`int`): Photo width.
3637
height (:obj:`int`): Photo height.
3738
file_size (:obj:`int`, optional): File size.
39+
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
3840
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
3941
4042
"""
4143

42-
def __init__(self, file_id, width, height, file_size=None, **kwargs):
44+
def __init__(self, file_id, width, height, file_size=None, bot=None, **kwargs):
4345
# Required
4446
self.file_id = str(file_id)
4547
self.width = int(width)
4648
self.height = int(height)
4749
# Optionals
4850
self.file_size = file_size
51+
self.bot = bot
4952

5053
self._id_attrs = (self.file_id,)
5154

@@ -54,7 +57,7 @@ def de_json(cls, data, bot):
5457
if not data:
5558
return None
5659

57-
return cls(**data)
60+
return cls(bot=bot, **data)
5861

5962
@classmethod
6063
def de_list(cls, data, bot):
@@ -66,3 +69,21 @@ def de_list(cls, data, bot):
6669
photos.append(cls.de_json(photo, bot))
6770

6871
return photos
72+
73+
def get_file(self, timeout=None, **kwargs):
74+
"""Convenience wrapper over :attr:`telegram.Bot.get_file`
75+
76+
Args:
77+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
78+
the read timeout from the server (instead of the one specified during creation of
79+
the connection pool).
80+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
81+
82+
Returns:
83+
:class:`telegram.File`
84+
85+
Raises:
86+
:class:`telegram.TelegramError`
87+
88+
"""
89+
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)

telegram/files/sticker.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Sticker(TelegramObject):
3535
mask_position (:class:`telegram.MaskPosition`): Optional. For mask stickers, the position
3636
where the mask should be placed.
3737
file_size (:obj:`int`): Optional. File size.
38+
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.
3839
3940
Args:
4041
file_id (:obj:`str`): Unique identifier for this file.
@@ -48,7 +49,8 @@ class Sticker(TelegramObject):
4849
mask_position (:class:`telegram.MaskPosition`, optional): For mask stickers, the
4950
position where the mask should be placed.
5051
file_size (:obj:`int`, optional): File size.
51-
**kwargs (obj:`dict`): Arbitrary keyword arguments.
52+
**kwargs (obj:`dict`): Arbitrary keyword arguments.7
53+
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
5254
5355
"""
5456

@@ -61,6 +63,7 @@ def __init__(self,
6163
file_size=None,
6264
set_name=None,
6365
mask_position=None,
66+
bot=None,
6467
**kwargs):
6568
# Required
6669
self.file_id = str(file_id)
@@ -72,6 +75,7 @@ def __init__(self,
7275
self.file_size = file_size
7376
self.set_name = set_name
7477
self.mask_position = mask_position
78+
self.bot = bot
7579

7680
self._id_attrs = (self.file_id,)
7781

@@ -85,7 +89,7 @@ def de_json(cls, data, bot):
8589
data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)
8690
data['mask_position'] = MaskPosition.de_json(data.get('mask_position'), bot)
8791

88-
return cls(**data)
92+
return cls(bot=bot, **data)
8993

9094
@classmethod
9195
def de_list(cls, data, bot):
@@ -94,6 +98,24 @@ def de_list(cls, data, bot):
9498

9599
return [cls.de_json(d, bot) for d in data]
96100

101+
def get_file(self, timeout=None, **kwargs):
102+
"""Convenience wrapper over :attr:`telegram.Bot.get_file`
103+
104+
Args:
105+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
106+
the read timeout from the server (instead of the one specified during creation of
107+
the connection pool).
108+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
109+
110+
Returns:
111+
:class:`telegram.File`
112+
113+
Raises:
114+
:class:`telegram.TelegramError`
115+
116+
"""
117+
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)
118+
97119

98120
class StickerSet(TelegramObject):
99121
"""This object represents a sticker set.

0 commit comments

Comments
 (0)