Skip to content

More instance methods #963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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)

Expand Down
104 changes: 104 additions & 0 deletions telegram/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bot may be None on some extreme cases. I believe that on these cases we should raise an explicit RuntimeError.

Though you may claim that we don't do that with Message.reply_*() methods. So maybe we should fix there too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the exact point I was gonna make xD
Does it matter what error it throws?
It seems like a lot of code for some superficial error that's unlikely to ever happen (as far as I understand it, it will never happen if users don't do stupid stuff, right?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicit errors are much easier to debug, especially in async systems, rather than deciphering a stack trace.
If you feel that the code is good as it is I won't object. I simply suggest to reconsider.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will leave this as is.


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)
24 changes: 23 additions & 1 deletion telegram/files/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.

"""
Expand All @@ -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)
Expand All @@ -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,)

Expand All @@ -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)
24 changes: 23 additions & 1 deletion telegram/files/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ 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
thumb (:class:`telegram.PhotoSize`, optional): Document thumbnail as defined by sender.
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.

"""
Expand All @@ -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)
Expand All @@ -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,)

Expand All @@ -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)
25 changes: 23 additions & 2 deletions telegram/files/photosize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,)

Expand All @@ -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):
Expand All @@ -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)
26 changes: 24 additions & 2 deletions telegram/files/sticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.

"""

Expand All @@ -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)
Expand All @@ -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,)

Expand All @@ -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):
Expand All @@ -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.
Expand Down
Loading
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