Example #1
0
class TelegramLogin:
    def __init__(self, phone, session_dir):
        self.phone = phone
        self.session_dir = session_dir
        os.mkdir(self.session_dir)
        self.app = Client("app", api_id, api_hash, workdir=self.session_dir)
        self.app.connect()

    def request_code(self):
        try:
            sent_code = self.app.send_code(self.phone)
            print(sent_code)

            if sent_code.type != "sms":
                self.bye()
                return False

        except exceptions.bad_request_400.PhoneNumberBanned:
            self.bye()
            return False

        self.sent_code = sent_code

        return True

    def create_session(self, code) -> User:
        signed_up = None
        signed_in = self.app.sign_in(self.phone,
                                     self.sent_code.phone_code_hash, code)

        if isinstance(signed_in, User):
            self.bye()
            return False

        first_name, last_name = gen_person()
        while not signed_up:
            try:
                signed_up = self.app.sign_up(self.phone,
                                             self.sent_code.phone_code_hash,
                                             first_name=first_name,
                                             last_name=last_name)

                if isinstance(signed_in, TermsOfService):
                    self.app.accept_terms_of_service(signed_in.id)

                self.app.disconnect()
                return signed_up

            except FloodWait as e:
                print("Sleeping ", e.x)
                time.sleep(e.x)

    def bye(self):
        self.app.disconnect()
        shutil.rmtree(self.session_dir)
def sessioncu():
    api_id = input('API ID: ')
    api_hash = input('API HASH: ')
    telefon = input('Telefon Numarası (+90 ile): ')
    print('\n')

    try:
        client = Client(f'{SESSION}{telefon}', api_id, api_hash)
        client.connect()
        kod = client.send_code(telefon)
        try:
            giris_yap = client.sign_in(telefon, kod.phone_code_hash,
                                       input('Doğrulama Kodu: '))
            client.accept_terms_of_service(str(giris_yap.id))
        except AssertionError:
            sleep(5)
        except SessionPasswordNeeded:
            client.check_password(
                input(
                    f'İki Adımlı Doğrulama Şifresi ({client.get_password_hint()}): '
                ))
        client.disconnect()
    except Exception as hata:
        os.remove(f'{SESSION}{telefon}.session')
        print(f'Hata Var !\n\t`{type(hata).__name__}`\n\t{hata}')
        return

    with client as app:
        app.send_message(
            'me',
            f'__Merhaba, Ben **KekikTelefon** Tarafından Gönderildim!__\n\n__Senin Bilgilerin;__\n\n**ID :** `{api_id}`\n**Hash :** `{api_hash}`\n**Telefon :** `{telefon}`\n\n**Kendi gizliliğin için bunları kimseyle paylaşma..**'
        )

    dict2json({
        'id': api_id,
        'hash': api_hash,
        'tel': telefon
    },
              dosya_adi=f'{SESSION}bilgiler.json')

    print(f'\n\n\t\t{telefon} Session Kayıt Edildi..!')