Skip to content

Commit 258be98

Browse files
authored
Added support for chat photos (#131)
* Added support for live locations * Added support for getting, setting and removing the chat photo * Added trailing commas * Added trailing comma * chat.photo.save() edit Changed the function to check whether that specific instance is a ChatPhoto instance or any other media instance * Enhancements with ChatPhoto.save(...) method * Remove useless if statement in mixins * Remove useless newline * Add docstring for Chat.set_photo and Chat.remove_photo
2 parents d271711 + 20024fc commit 258be98

File tree

5 files changed

+121
-2
lines changed

5 files changed

+121
-2
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,32 @@ 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+
def save(self, *args, small=False, **kwargs):
59+
"""Workaround for dealing with big and small chat photos"""
60+
if small:
61+
self.file_id = self.small
62+
else:
63+
self.file_id = self.big
64+
super(ChatPhoto, self).save(*args, **kwargs)
65+
del self.file_id
66+
67+
4268
class Photo(mixins.FileMixin):
4369
"""Custom representation of a photo
4470

botogram/objects/mixins.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,19 @@ def delete_message(self, message):
388388
"message_id": message,
389389
})
390390

391+
@_require_api
392+
def set_photo(self, path):
393+
"""Set a new chat photo"""
394+
args = {"chat_id": self.id}
395+
files = {"photo": open(path, "rb")}
396+
self._api.call("setChatPhoto", args, files)
397+
398+
@_require_api
399+
def remove_photo(self):
400+
"""Remove the current chat photo"""
401+
args = {"chat_id": self.id}
402+
self._api.call("deleteChatPhoto", args)
403+
391404
@_require_api
392405
def send_album(self, album=None, reply_to=None, notify=True):
393406
"""Send a Album"""

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`
@@ -811,6 +812,12 @@ about its business.
811812
812813
.. versionadded:: 0.3
813814

815+
.. py:attribute:: photo
816+
817+
The current chat photo, represented by a :py:class:`~botogram.ChatPhoto` object.
818+
819+
.. versionadded:: 0.7
820+
814821
.. py:method:: status_of(user)
815822
816823
Return the status of the provided user (either an instance of
@@ -1546,6 +1553,20 @@ about its business.
15461553

15471554
.. versionadded:: 0.6
15481555

1556+
.. py:method:: set_photo(path)
1557+
1558+
Set a new chat photo, by providing its path.
1559+
1560+
:param str path: The path to the new photo
1561+
1562+
.. versionadded:: 0.7
1563+
1564+
.. py:method:: remove_photo()
1565+
1566+
Remove the current chat photo.
1567+
1568+
.. versionadded:: 0.7
1569+
15491570
.. py:class:: botogram.ParsedText
15501571
15511572
This class contains the parsed representation of the text of a received
@@ -2608,6 +2629,32 @@ about its business.
26082629

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

2632+
.. py:class:: botogram.ChatPhoto
2633+
2634+
This class represents a Telegram API chat photo.
2635+
2636+
It consists of two file IDs, each one representing a different size of the current chat photo.
2637+
2638+
The photos can be saved using the method :py:meth:`~ChatPhoto.save`
2639+
2640+
.. py:attribute:: big
2641+
2642+
The string ID of the 640x640 version of the chat photo
2643+
2644+
.. py:attribute:: small
2645+
2646+
The string ID of the 160x160 version of the chat photo
2647+
2648+
.. py:method:: save(path [, small=False])
2649+
2650+
Save the image represented to a file located by *path*. Be aware that
2651+
Telegram does not provide the name of the original file sent by its
2652+
sender. This should be generated as part of the path.
2653+
2654+
:param str path: The file name path locating where the image should be saved.
2655+
:param bool small: Whether it should save the big or the small version of the chat photo
2656+
2657+
.. versionadded:: 0.7
26112658

26122659
.. py:class:: botogram.Audio
26132660

docs/changelog/0.7.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ New features
6464
* New method :py:meth:`~Message.edit_live_location`
6565
* New method :py:meth:`~Message.stop_live_location`
6666

67+
* Added support for editing, getting and removing the chat photo
68+
69+
* New method :py:meth:`~Chat.set_photo`
70+
* New method :py:meth:`~Chat.remove_photo`
71+
* New attribute :py:attr:`~Chat.photo`
72+
* New class :py:class:`~botogram.ChatPhoto`
73+
6774
Bug fixes
6875
---------
6976

0 commit comments

Comments
 (0)