Exemple #1
0
async def _get_client(loop=None):
    """Creates a Telegram client based on current Telegram secrets.

    Returns:
        TelegramClient object.
    """
    api_id, api_hash, phone_number, session_name = storage.get_telegram_secrets()
    if loop:
        client = TelegramClient(session_name, api_id, api_hash, loop=loop)
    else:
        client = TelegramClient(session_name, api_id, api_hash)
    await client.connect()

    if not await client.is_user_authorized():
        await client.send_code_request(phone_number)
        await client.sign_in(phone_number, input("Please enter the code you received: "))
    return client
Exemple #2
0
    def raise_telegram_auth_frame(self):
        """Makes an initial sign-in into Telegram client."""
        table_frame = tk.Frame()
        table_frame.pack(expand=True, fill="both")

        assert self.session_params["from_telegram"]

        api_id, api_hash, phone_number, _ = storage.get_telegram_secrets()

        # A text in labels should be 15 characters long in order to not shift entries.
        # Should make them more adaptive some day.
        api_id_label = tk.Label(table_frame,
                                text="API id :       ",
                                height=2,
                                font=self.default_font)
        api_id_label.grid(row=1, column=1, sticky=tk.W)

        api_id_dir = tk.Entry(table_frame, width=46, font=self.default_font)
        api_id_dir.insert(tk.END, api_id)
        api_id_dir.grid(row=1, column=2, sticky=tk.W)

        api_hash_label = tk.Label(table_frame,
                                  text="API hash :     ",
                                  height=2,
                                  font=self.default_font)
        api_hash_label.grid(row=2, column=1, sticky=tk.W)

        api_hash_dir = tk.Entry(table_frame, width=46, font=self.default_font)
        api_hash_dir.insert(tk.END, api_hash)
        api_hash_dir.grid(row=2, column=2, sticky=tk.W)

        phone_number_label = tk.Label(table_frame,
                                      text="Phone number : ",
                                      height=2,
                                      font=self.default_font)
        phone_number_label.grid(row=3, column=1, sticky=tk.W)

        phone_number_dir = tk.Entry(table_frame,
                                    width=46,
                                    font=self.default_font)
        phone_number_dir.insert(tk.END, phone_number)
        phone_number_dir.grid(row=3, column=2, sticky=tk.W)

        code_label = tk.Label(table_frame,
                              text="Code :         ",
                              height=2,
                              font=self.default_font)
        code_label.grid(row=4, column=1, sticky=tk.W)

        code_dir = tk.Entry(table_frame, width=46, font=self.default_font)
        code_dir.grid(row=4, column=2, sticky=tk.W)

        password_label = tk.Label(table_frame,
                                  text="Password :     "******"Please be sure You have set the right API ID and key\n"
             "They can be obtained from:\n"
             "https://core.telegram.org/api/obtaining_api_id"))
        message_label = tk.Label(table_frame,
                                 textvariable=message_label_text,
                                 height=3,
                                 font=self.default_font,
                                 fg="red",
                                 justify="left")
        message_label.grid(row=6, column=1, sticky=tk.W, columnspan=2)

        async def try_sign_in_and_continue():
            res = await tlg.get_sign_in_results(
                api_id_dir.get(),
                api_hash_dir.get(),
                code_dir.get(),
                phone_number_dir.get(),
                password_dir.get(),
                self.session_params["your_name"],
                loop=self.aio_loop)
            try:
                api_id_label.config(fg="black")
            except tk.TclError:  # too fast "continue" button clicks?
                return
            api_hash_label.config(fg="black")
            phone_number_label.config(fg="black")
            code_label.config(fg="black")
            password_label.config(fg="black")
            if res == "wrong api":
                api_id_label.config(fg="red")
                api_hash_label.config(fg="red")
                return message_label_text.set(
                    "Please be sure You have set the right API ID and hash\n"
                    "They can be obtained from:\n"
                    "https://core.telegram.org/api/obtaining_api_id")
            elif res == "need phone":
                phone_number_label.config(fg="red")
                return message_label_text.set(
                    "Please carefully set Your phone number in order to   \n"
                    "get a confirmation code.\n ")

            elif res == "need code":
                code_label.config(fg="red")
                return message_label_text.set(
                    "Please check Your private messages (or SMS) and      \n"
                    "copypaste the right code.\n ")
            elif res == "need password":
                password_label.config(fg="red")
                return message_label_text.set(
                    "Please enter correct password.\n")
            elif res.startswith("need wait for "):
                return message_label_text.set(
                    f'Please wait. A wait of {res[14:]} seconds is required.\n'
                )
            elif res == "no internet":
                return message_label_text.set(
                    "Please be sure You have stable Internet connection.\n\n")

            assert res == "success"
            storage.store_telegram_secrets(
                api_id_dir.get(),
                api_hash_dir.get(),
                phone_number_dir.get(),
                session_name=self.session_params["your_name"])
            bottom_frame.destroy()
            table_frame.destroy()
            self.aio_loop.create_task(self.raise_dialogs_select_frame())

        bottom_frame = tk.Frame()
        bottom_frame.pack(side=tk.BOTTOM)
        continue_button = tk.Button(bottom_frame,
                                    text="Continue",
                                    command=lambda: self.aio_loop.create_task(
                                        try_sign_in_and_continue()),
                                    padx=35,
                                    background=self.button_background,
                                    font=self.default_font)
        continue_button.pack(side=tk.BOTTOM)
        self.parent.bind(
            '<Return>',
            lambda _: self.aio_loop.create_task(try_sign_in_and_continue()))