2018-05-07 12:30:55 +00:00
|
|
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
2019-01-01 11:36:16 +00:00
|
|
|
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
|
2018-05-07 12:30:55 +00:00
|
|
|
#
|
|
|
|
# This file is part of Pyrogram.
|
|
|
|
#
|
|
|
|
# Pyrogram is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Pyrogram is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2019-03-01 17:28:46 +00:00
|
|
|
import pyrogram
|
2018-05-07 12:30:55 +00:00
|
|
|
from pyrogram.api import functions, types
|
|
|
|
from ...ext import BaseClient
|
|
|
|
|
|
|
|
|
|
|
|
class JoinChat(BaseClient):
|
2019-03-16 18:49:01 +00:00
|
|
|
async def join_chat(
|
2019-03-16 18:23:23 +00:00
|
|
|
self,
|
|
|
|
chat_id: str
|
|
|
|
):
|
2019-05-12 17:49:06 +00:00
|
|
|
"""Join a group chat or channel.
|
2018-05-07 12:30:55 +00:00
|
|
|
|
2019-05-09 02:28:46 +00:00
|
|
|
Parameters:
|
2018-05-07 12:30:55 +00:00
|
|
|
chat_id (``str``):
|
2018-12-28 13:34:47 +00:00
|
|
|
Unique identifier for the target chat in form of a *t.me/joinchat/* link or username of the target
|
2018-05-07 12:30:55 +00:00
|
|
|
channel/supergroup (in the format @username).
|
|
|
|
|
2019-03-01 17:28:46 +00:00
|
|
|
Returns:
|
2019-05-09 02:28:46 +00:00
|
|
|
:obj:`Chat`: On success, a chat object is returned.
|
2019-03-01 17:28:46 +00:00
|
|
|
|
2019-07-25 09:22:14 +00:00
|
|
|
Example:
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Join chat via username
|
|
|
|
app.join_chat("pyrogram")
|
|
|
|
|
|
|
|
# Join chat via invite link
|
|
|
|
app.join_chat("https://t.me/joinchat/AAAAAE0QmSW3IUmm3UFR7A")
|
2018-05-07 12:30:55 +00:00
|
|
|
"""
|
|
|
|
match = self.INVITE_LINK_RE.match(chat_id)
|
|
|
|
|
|
|
|
if match:
|
2019-03-02 15:36:44 +00:00
|
|
|
chat = await self.send(
|
2018-05-07 12:30:55 +00:00
|
|
|
functions.messages.ImportChatInvite(
|
|
|
|
hash=match.group(1)
|
|
|
|
)
|
|
|
|
)
|
2019-03-01 17:28:46 +00:00
|
|
|
if isinstance(chat.chats[0], types.Chat):
|
|
|
|
return pyrogram.Chat._parse_chat_chat(self, chat.chats[0])
|
|
|
|
elif isinstance(chat.chats[0], types.Channel):
|
|
|
|
return pyrogram.Chat._parse_channel_chat(self, chat.chats[0])
|
2018-05-07 12:30:55 +00:00
|
|
|
else:
|
2019-03-02 15:36:44 +00:00
|
|
|
chat = await self.send(
|
2018-05-07 12:30:55 +00:00
|
|
|
functions.channels.JoinChannel(
|
2019-09-09 14:00:38 +00:00
|
|
|
channel=await self.resolve_peer(chat_id)
|
2018-05-07 12:30:55 +00:00
|
|
|
)
|
|
|
|
)
|
2019-03-01 17:28:46 +00:00
|
|
|
|
|
|
|
return pyrogram.Chat._parse_channel_chat(self, chat.chats[0])
|