Skip to content

Commit 173a7e8

Browse files
author
Pietro Albini
committed
Add the Chat.status_of(user) method
This new method returns the current status of an user in a group chat. List of possible status is available in the documentation. Support for this was introduced in the Bot API 2.1 update. Issue: GH-64
1 parent 4a50a69 commit 173a7e8

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

botogram/objects/chats.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,29 @@ def members_count(self):
179179
expect=int)
180180
return self._cache_members_count
181181

182+
def status_of(self, user):
183+
"""Check the status of a member of the group"""
184+
if self.type in ("private", "channel"):
185+
raise TypeError("Not available in private chats or channels")
186+
187+
# Convert Users to IDs
188+
if isinstance(user, User):
189+
user = user.id
190+
191+
# Initialize the cache
192+
if not hasattr(self, "_cache_status_of"):
193+
self._cache_status_of = {}
194+
195+
# Populate the cache for new users
196+
if user not in self._cache_status_of:
197+
member = self._api.call("getChatMember", {
198+
"chat_id": self.id,
199+
"user_id": user,
200+
}, expect=ChatMember)
201+
self._cache_status_of[user] = member.status
202+
203+
return self._cache_status_of[user]
204+
182205
def leave(self):
183206
"""Leave this chat"""
184207
if self.type not in ("group", "supergroup"):

docs/api/telegram.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,42 @@ about its business.
452452
453453
.. versionadded:: 0.3
454454

455+
.. py:method:: status_of(user)
456+
457+
Return the status of the provided user (either an instance of
458+
:py:class:`~botogram.User` or an ID) in the group chat. A ``TypeError``
459+
is raised if the current chat is a private conversation or a channel.
460+
461+
Currently available statuses:
462+
463+
* **creator**: this user created the group in the first place
464+
* **administrator**: the user is an admin appointed by the group creator
465+
* **member**: the user is a normal member of the group
466+
* **left**: the user left the group in the past or never joined it
467+
* **kicked**: the user was kicked by an administrator out of the group
468+
469+
Please remember the content of this attribute is fetched from Telegram
470+
the first time you access it (so it might be slow), but it's cached right
471+
after, so the following accesses will involve no network communication.
472+
473+
.. code-block:: python
474+
:emphasize-lines: 6,7
475+
476+
@bot.command("status_of")
477+
def status_of_command(chat, message, args):
478+
if len(args) != 1:
479+
message.reply("You must provide just the ID of the user!")
480+
481+
status = chat.status_of(int(args[0]))
482+
chat.send("*%s*" % status)
483+
484+
:param user: the user you want to check the status of (either
485+
:py:class:`~botogram.User` or the user ID as an ``int``)
486+
:returns: the status of the user
487+
:rtype: str
488+
489+
.. versionaddedd:: 0.3
490+
455491
.. py:method:: leave()
456492
457493
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
@@ -50,6 +50,7 @@ New features
5050
* Added new attribute :py:attr:`botogram.Chat.admins`
5151
* Added new attribute :py:attr:`botogram.Chat.creator`
5252
* Added new attribute :py:attr:`botogram.Chat.members_count`
53+
* Added new method :py:meth:`botogram.Chat.status_of`
5354
* Added new method :py:meth:`botogram.Chat.leave`
5455
* Every method which sends something to a chat now returns the sent
5556
:py:class:`~botogram.Message`

0 commit comments

Comments
 (0)