Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions botogram/objects/chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ class Chat(BaseObject, mixins.ChatMixin):
"sticker_set_name": str,
"can_set_sticker_set": bool
}
replace_keys = {
"invite_link": "_invite_link",
}
_check_equality_ = "id"

def _to_user(self):
Expand Down Expand Up @@ -294,6 +297,37 @@ def kick(self, user, time=None):
def permissions(self, user):
return Permissions(user, self)

@mixins._require_api
def revoke_invite_link(self):
"""Revoke and generate a new invike link for this chat"""
if self.type not in ("supergroup", "channel"):
raise RuntimeError("You can revoke the invite link only in a supergroup or a channel")

link = self._api.call("exportChatInviteLink", {
"chat_id": self.id,
}).get('result', None)
self._cache_invite_link = link
return link

@property
@mixins._require_api
def invite_link(self):
"""Get the invite link of this chat"""
if self.type not in ("supergroup", "channel"):
raise RuntimeError("You can get the invite link only in a supergroup or a channel")

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

chat = self._api.call("getChat", {
"chat_id": self.id,
}, expect=Chat)
if not chat._invite_link:
return self.revoke_invite_link()

self._cache_invite_link = chat._invite_link
return self._cache_invite_link


class Permissions:
def __init__(self, user, chat):
Expand Down
22 changes: 21 additions & 1 deletion docs/api/telegram.rst
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,9 @@ about its business.

.. py:attribute:: invite_link

This group chat/channel invite link.
The chat invite link, it works only in a supergroup or a channel.
You can revoke the current link and create a new one by using the
:py:method:`~botogram.Chat.revoke_invite_link` method.

*This attribute can be None if it's not provided by Telegram.*
.. versionadded:: 0.6
Expand Down Expand Up @@ -831,6 +833,24 @@ about its business.
:rtype: :py:class:`~botogram.Permissions`
.. versionadded:: 0.6

.. py:method:: revoke_invite_link()
Revokes the previous invite link of the chat and returns a new one.
It works only in a supergroup or a channel.

.. code-block:: python

@bot.command("revoke")
def revoke_invite(chat):
chat.send("Alright, revoking the invite link...")
new_link = chat.revoke_invite_link()
chat.send("New link: " + new_link)


:returns: A new invite link
:rtype: str
.. versionadded:: 0.6


.. py:method:: send(message, [preview=True, reply_to=None, syntax=None, attach=None, extra=None, notify=True])

Send the textual *message* to the chat. You may optionally stop clients
Expand Down