Skip to content
30 changes: 28 additions & 2 deletions botogram/objects/chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from . import mixins
from datetime import datetime as dt
from time import mktime
from .media import Photo
from .media import Photo, ChatPhoto


class User(BaseObject, mixins.ChatMixin):
Expand Down Expand Up @@ -114,10 +114,12 @@ class Chat(BaseObject, mixins.ChatMixin):
# This is added at the bottom of messages.py due to circular imports
# "pinned_message" = Message
"sticker_set_name": str,
"can_set_sticker_set": bool
"can_set_sticker_set": bool,
"photo": ChatPhoto,
}
replace_keys = {
"invite_link": "_invite_link",
"photo": "_photo",
}
_check_equality_ = "id"

Expand Down Expand Up @@ -358,6 +360,30 @@ def pin_message(self, message, notify=True):
"disable_notification": not notify
}, expect=bool)

@property
@mixins._require_api
def photo(self):
"""Get the current chat photo small and big ids"""
if self.type not in ("supergroup", "channel"):
raise RuntimeError(
"You can only get the photo in a supergroup or a channel")

if hasattr(self, "_cache_photo"):
return self._cache_photo

if self._photo is not None:
self._cache_photo = self._photo
return self._cache_photo

chat = self._api.call("getChat", {
"chat_id": self.id
}, expect=Chat)
if not chat._photo:
raise RuntimeError("This chat doesn't have a photo")

self._cache_photo = chat._photo
return self._cache_photo

def unpin_message(self):
return self._api.call("unpinChatMessage", {
"chat_id": self.id,
Expand Down
26 changes: 26 additions & 0 deletions botogram/objects/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ class PhotoSize(BaseObject, mixins.FileMixin):
_check_equality_ = "file_id"


class ChatPhoto(BaseObject, mixins.FileMixin):
"""Telegram API representation of a chat photo

https://core.telegram.org/bots/api#chatphoto
"""

required = {
"small_file_id": str,
"big_file_id": str,
}
replace_keys = {
"small_file_id": "small",
"big_file_id": "big",
}
_check_equality_ = "small_file_id"

def save(self, *args, small=False, **kwargs):
"""Workaround for dealing with big and small chat photos"""
if small:
self.file_id = self.small
else:
self.file_id = self.big
super(ChatPhoto, self).save(*args, **kwargs)
del self.file_id


class Photo(mixins.FileMixin):
"""Custom representation of a photo

Expand Down
13 changes: 13 additions & 0 deletions botogram/objects/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,19 @@ def delete_message(self, message):
"message_id": message,
})

@_require_api
def set_photo(self, path):
"""Set a new chat photo"""
args = {"chat_id": self.id}
files = {"photo": open(path, "rb")}
self._api.call("setChatPhoto", args, files)

@_require_api
def remove_photo(self):
"""Remove the current chat photo"""
args = {"chat_id": self.id}
self._api.call("deleteChatPhoto", args)

@_require_api
def send_album(self, album=None, reply_to=None, notify=True):
"""Send a Album"""
Expand Down
47 changes: 47 additions & 0 deletions docs/api/telegram.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ about its business.
* :py:class:`~botogram.Message`
* :py:class:`~botogram.Photo`
* :py:class:`~botogram.PhotoSize`
* :py:class:`~botogram.ChatPhoto`
* :py:class:`~botogram.Audio`
* :py:class:`~botogram.Document`
* :py:class:`~botogram.Sticker`
Expand Down Expand Up @@ -811,6 +812,12 @@ about its business.

.. versionadded:: 0.3

.. py:attribute:: photo

The current chat photo, represented by a :py:class:`~botogram.ChatPhoto` object.

.. versionadded:: 0.7

.. py:method:: status_of(user)

Return the status of the provided user (either an instance of
Expand Down Expand Up @@ -1546,6 +1553,20 @@ about its business.

.. versionadded:: 0.6

.. py:method:: set_photo(path)

Set a new chat photo, by providing its path.

:param str path: The path to the new photo

.. versionadded:: 0.7

.. py:method:: remove_photo()

Remove the current chat photo.

.. versionadded:: 0.7

.. py:class:: botogram.ParsedText

This class contains the parsed representation of the text of a received
Expand Down Expand Up @@ -2608,6 +2629,32 @@ about its business.

:param str path: The file name path locating where the image should be saved.

.. py:class:: botogram.ChatPhoto

This class represents a Telegram API chat photo.

It consists of two file IDs, each one representing a different size of the current chat photo.

The photos can be saved using the method :py:meth:`~ChatPhoto.save`

.. py:attribute:: big

The string ID of the 640x640 version of the chat photo

.. py:attribute:: small

The string ID of the 160x160 version of the chat photo

.. py:method:: save(path [, small=False])

Save the image represented to a file located by *path*. Be aware that
Telegram does not provide the name of the original file sent by its
sender. This should be generated as part of the path.

:param str path: The file name path locating where the image should be saved.
:param bool small: Whether it should save the big or the small version of the chat photo

.. versionadded:: 0.7

.. py:class:: botogram.Audio

Expand Down
7 changes: 7 additions & 0 deletions docs/changelog/0.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ New features
* New method :py:meth:`~Message.edit_live_location`
* New method :py:meth:`~Message.stop_live_location`

* Added support for editing, getting and removing the chat photo

* New method :py:meth:`~Chat.set_photo`
* New method :py:meth:`~Chat.remove_photo`
* New attribute :py:attr:`~Chat.photo`
* New class :py:class:`~botogram.ChatPhoto`

Bug fixes
---------

Expand Down