Add support for joining linked chats with the .join() bound method

This commit is contained in:
Dan 2020-05-07 15:05:31 +02:00
parent 12ce0a33c1
commit 0556efa26b
2 changed files with 12 additions and 7 deletions

View File

@ -16,6 +16,8 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Union
import pyrogram
from pyrogram.api import functions, types
from ...ext import BaseClient
@ -24,14 +26,14 @@ from ...ext import BaseClient
class JoinChat(BaseClient):
def join_chat(
self,
chat_id: str
chat_id: Union[int, str]
):
"""Join a group chat or channel.
Parameters:
chat_id (``str``):
Unique identifier for the target chat in form of a *t.me/joinchat/* link or username of the target
channel/supergroup (in the format @username).
chat_id (``int`` | ``str``):
Unique identifier for the target chat in form of a *t.me/joinchat/* link, a username of the target
channel/supergroup (in the format @username) or a chat id of a linked chat (channel or supergroup).
Returns:
:obj:`Chat`: On success, a chat object is returned.
@ -44,8 +46,11 @@ class JoinChat(BaseClient):
# Join chat via invite link
app.join_chat("https://t.me/joinchat/AAAAAE0QmSW3IUmm3UFR7A")
# Join a linked chat
app.join_chat(app.get_chat("pyrogram").linked_chat.id)
"""
match = self.INVITE_LINK_RE.match(chat_id)
match = self.INVITE_LINK_RE.match(str(chat_id))
if match:
chat = self.send(

View File

@ -675,7 +675,7 @@ class Chat(Object):
chat.join()
Note:
This only works for public groups and channels that have set a username.
This only works for public groups, channels that have set a username or linked chats.
Returns:
:obj:`Chat`: On success, a chat object is returned.
@ -684,7 +684,7 @@ class Chat(Object):
RPCError: In case of a Telegram RPC error.
"""
return self._client.join_chat(self.username)
return self._client.join_chat(self.username or self.id)
def leave(self):
"""Bound method *leave* of :obj:`Chat`.