Skip to content

Commit 5f40044

Browse files
author
Francesco Zimbolo
committed
Added support for getting, setting and removing the chat photo
1 parent 6e043d1 commit 5f40044

File tree

5 files changed

+122
-4
lines changed

5 files changed

+122
-4
lines changed

botogram/objects/chats.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from . import mixins
2424
from datetime import datetime as dt
2525
from time import mktime
26-
from .media import Photo
26+
from .media import Photo, ChatPhoto
2727

2828

2929
class User(BaseObject, mixins.ChatMixin):
@@ -114,10 +114,12 @@ class Chat(BaseObject, mixins.ChatMixin):
114114
# This is added at the bottom of messages.py due to circular imports
115115
# "pinned_message" = Message
116116
"sticker_set_name": str,
117-
"can_set_sticker_set": bool
117+
"can_set_sticker_set": bool,
118+
"photo": ChatPhoto
118119
}
119120
replace_keys = {
120121
"invite_link": "_invite_link",
122+
"photo": "_photo"
121123
}
122124
_check_equality_ = "id"
123125

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

363+
@property
364+
@mixins._require_api
365+
def photo(self):
366+
"""Get the current chat photo small and big ids"""
367+
if self.type not in ("supergroup", "channel"):
368+
raise RuntimeError(
369+
"You can only get the photo in a supergroup or a channel")
370+
371+
if hasattr(self, "_cache_photo"):
372+
return self._cache_photo
373+
374+
if self._photo is not None:
375+
self._cache_photo = self._photo
376+
return self._cache_photo
377+
378+
chat = self._api.call("getChat", {
379+
"chat_id": self.id
380+
}, expect=Chat)
381+
if not chat._photo:
382+
raise RuntimeError("This chat doesn't have a photo")
383+
384+
self._cache_photo = chat._photo
385+
return self._cache_photo
386+
361387
def unpin_message(self):
362388
return self._api.call("unpinChatMessage", {
363389
"chat_id": self.id,

botogram/objects/media.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ class PhotoSize(BaseObject, mixins.FileMixin):
3939
_check_equality_ = "file_id"
4040

4141

42+
class ChatPhoto(BaseObject, mixins.FileMixin):
43+
"""Telegram API representation of a chat photo
44+
45+
https://core.telegram.org/bots/api#chatphoto
46+
"""
47+
48+
required = {
49+
"small_file_id": str,
50+
"big_file_id": str,
51+
}
52+
replace_keys = {
53+
"small_file_id": "small",
54+
"big_file_id": "big"
55+
}
56+
_check_equality_ = "small_file_id"
57+
58+
4259
class Photo(mixins.FileMixin):
4360
"""Custom representation of a photo
4461

botogram/objects/mixins.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,21 @@ def delete_message(self, message):
361361
"message_id": message,
362362
})
363363

364+
@_require_api
365+
def set_photo(self, path):
366+
args = {"chat_id": self.id}
367+
if path is not None:
368+
files = {"photo": open(path, "rb")}
369+
else:
370+
raise TypeError("path of the new photo is missing")
371+
372+
self._api.call("setChatPhoto", args, files)
373+
374+
@_require_api
375+
def remove_photo(self):
376+
args = {"chat_id": self.id}
377+
self._api.call("deleteChatPhoto", args)
378+
364379
@_require_api
365380
def send_album(self, album=None, reply_to=None, notify=True):
366381
"""Send a Album"""
@@ -566,9 +581,16 @@ class FileMixin:
566581
"""Add some methods for files"""
567582

568583
@_require_api
569-
def save(self, path):
584+
def save(self, path, big=True):
570585
"""Save the file to a particular path"""
571-
response = self._api.call("getFile", {"file_id": self.file_id})
586+
if not hasattr("self", "file_id"):
587+
if big:
588+
_file_id = self.big
589+
else:
590+
_file_id = self.small
591+
else:
592+
_file_id = self.file_id
593+
response = self._api.call("getFile", {"file_id": _file_id})
572594

573595
# Save the file to the wanted path
574596
downloaded = self._api.file_content(response["result"]["file_path"])

docs/api/telegram.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ about its business.
2121
* :py:class:`~botogram.Message`
2222
* :py:class:`~botogram.Photo`
2323
* :py:class:`~botogram.PhotoSize`
24+
* :py:class:`~botogram.ChatPhoto`
2425
* :py:class:`~botogram.Audio`
2526
* :py:class:`~botogram.Document`
2627
* :py:class:`~botogram.Sticker`
@@ -772,6 +773,12 @@ about its business.
772773
773774
.. versionadded:: 0.3
774775

776+
.. py:attribute:: photo
777+
778+
The current chat photo, represented by a :py:class:`~botogram.ChatPhoto` object.
779+
780+
.. versionadded:: 0.7
781+
775782
.. py:method:: status_of(user)
776783
777784
Return the status of the provided user (either an instance of
@@ -1445,6 +1452,20 @@ about its business.
14451452

14461453
.. versionadded:: 0.6
14471454

1455+
.. py:method:: set_photo(path)
1456+
1457+
Set a new chat photo, by providing its path.
1458+
1459+
:param str path: The path to the new photo
1460+
1461+
.. versionadded:: 0.7
1462+
1463+
.. py:method:: remove_photo()
1464+
1465+
Remove the current chat photo.
1466+
1467+
.. versionadded:: 0.7
1468+
14481469
.. py:class:: botogram.ParsedText
14491470
14501471
This class contains the parsed representation of the text of a received
@@ -2419,6 +2440,32 @@ about its business.
24192440

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

2443+
.. py:class:: botogram.ChatPhoto
2444+
2445+
This class represents a Telegram API chat photo.
2446+
2447+
It consists of two file IDs, each one representing a different size of the current chat photo.
2448+
2449+
The photos can be saved using the method :py:meth:`~ChatPhoto.save`
2450+
2451+
.. py:attribute:: big
2452+
2453+
The string ID of the 640x640 version of the chat photo
2454+
2455+
.. py:attribute:: small
2456+
2457+
The string ID of the 160x160 version of the chat photo
2458+
2459+
.. py:method:: save(path [, big=True])
2460+
2461+
Save the image represented to a file located by *path*. Be aware that
2462+
Telegram does not provide the name of the original file sent by its
2463+
sender. This should be generated as part of the path.
2464+
2465+
:param str path: The file name path locating where the image should be saved.
2466+
:param bool big: Whether it should save the big or the small version of the chat photo
2467+
2468+
.. versionadded:: 0.7
24222469

24232470
.. py:class:: botogram.Audio
24242471

docs/changelog/0.7.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ New features
3232
* New method :py:meth:`~Message.edit_live_location`
3333
* New method :py:meth:`~Message.stop_live_location`
3434

35+
* Added support for editing, getting and removing the chat photo
36+
37+
* New method :py:meth:`~Chat.set_photo`
38+
* New method :py:meth:`~Chat.remove_photo`
39+
* New attribute :py:attr:`~Chat.photo`
40+
* New class :py:class:`~botogram.ChatPhoto`
3541

3642
Bug fixes
3743
---------

0 commit comments

Comments
 (0)