Skip to content

Commit 7aef207

Browse files
author
Pietro Albini
committed
Add the Chat.creator dynamic attribute
This new attribute returns the original creator of the group chat, fetching it from a new API introduced in the Bot API 2.1 update. Issue: GH-64
1 parent 2c7fb98 commit 7aef207

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

botogram/objects/chats.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,27 @@ def admins(self):
144144
# returned as a list (the tuple thing is an implementation detail)
145145
return list(self._cache_admins)
146146

147+
@property
148+
def creator(self):
149+
"""Get the creator of this chat"""
150+
# If this is a private chat, return the current user since Telegram
151+
# doesn't support `getChatAdministrators` for private chats
152+
if self.type == "private":
153+
return self._to_user()
154+
elif self.type == "channel":
155+
raise TypeError("Not available on channels")
156+
157+
# Be sure to cache the chat creator
158+
if not hasattr(self, "_cache_creator"):
159+
# Get a list of the admins and fetch only the creator
160+
members = self._api.call("getChatAdministrators",
161+
{"chat_id": self.id},
162+
expect=multiple(ChatMember))
163+
self._cache_creator = [member.user for member in members
164+
if member.status == "creator"][0]
165+
166+
return self._cache_creator
167+
147168
def leave(self):
148169
"""Leave this chat"""
149170
if self.type not in ("group", "supergroup"):

docs/api/telegram.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,33 @@ about its business.
407407
408408
.. versionadded:: 0.3
409409

410+
.. py:attribute:: creator
411+
412+
Return the creator of the group (or supergroup), represented as an
413+
:py:class:`~botogram.User`. If the chat is a private chat, the chat
414+
partner is returned. Instead, a ``TypeError`` is raised if the chat is a
415+
channel.
416+
417+
Please remember the content of this attribute is fetched from Telegram
418+
the first time you access it (so it might be slow), but it's cached right
419+
after, so the following accesses will involve no network communication.
420+
421+
.. code-block:: python
422+
:emphasize-lines: 4,5,6
423+
424+
@bot.command("antiflood_limit")
425+
def antiflood_limit_command(shared, chat, message, args):
426+
"""Set the antiflood limit"""
427+
# Only the chat creator should be able to do this
428+
if message.sender != chat.creator:
429+
message.reply("You're not the creator of the chat")
430+
431+
if len(args) != 1:
432+
message.reply("You need to provide just the new limit!")
433+
shared["antiflood_limit"] = int(args[0])
434+
435+
.. versionadded:: 0.3
436+
410437
.. py:method:: leave()
411438
412439
Kick the bot from this chat. This method is available only on groups and

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ New features
4848
* Added new attribute :py:attr:`botogram.Message.pinned_message`
4949
* Added new attribute :py:attr:`botogram.Sticker.emoji`
5050
* Added new attribute :py:attr:`botogram.Chat.admins`
51+
* Added new attribute :py:attr:`botogram.Chat.creator`
5152
* Added new method :py:meth:`botogram.Chat.leave`
5253
* Every method which sends something to a chat now returns the sent
5354
:py:class:`~botogram.Message`

0 commit comments

Comments
 (0)