From c7f7825c922406e4f5f05b42e321c5869e5c27f1 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 25 Dec 2017 12:30:48 +0100 Subject: [PATCH] Add join_chat method --- pyrogram/client/client.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index aef1fbdc..526062e7 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -22,6 +22,7 @@ import logging import math import mimetypes import os +import re import time from collections import namedtuple from configparser import ConfigParser @@ -53,6 +54,7 @@ Config = namedtuple("Config", ["api_id", "api_hash"]) class Client: + INVITE_LINK_RE = re.compile(r"^(?:https?:\/\/)?t\.me\/joinchat\/(.+)$") DIALOGS_AT_ONCE = 100 def __init__(self, session_name: str, test_mode: bool = False): @@ -933,3 +935,30 @@ class Client: limit=limit ) ) + + def join_chat(self, chat_id: str): + match = self.INVITE_LINK_RE.match(chat_id) + + if match: + return self.send( + functions.messages.ImportChatInvite( + hash=match.group(1) + ) + ) + else: + resolved_peer = self.send( + functions.contacts.ResolveUsername( + username=chat_id.lower().strip("@") + ) + ) + + channel = InputPeerChannel( + channel_id=resolved_peer.chats[0].id, + access_hash=resolved_peer.chats[0].access_hash + ) + + return self.send( + functions.channels.JoinChannel( + channel=channel + ) + )