From ad0307fda6277096406ee47b205ebf41017d8c50 Mon Sep 17 00:00:00 2001 From: painor Date: Mon, 15 Mar 2021 22:25:06 +0100 Subject: [PATCH] Add password support for quart example (#1732) --- telethon_examples/quart_login.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/telethon_examples/quart_login.py b/telethon_examples/quart_login.py index bb960c03..98fb35de 100644 --- a/telethon_examples/quart_login.py +++ b/telethon_examples/quart_login.py @@ -5,6 +5,7 @@ import hypercorn.asyncio from quart import Quart, render_template_string, request from telethon import TelegramClient, utils +from telethon.errors import SessionPasswordNeededError def get_env(name, message): @@ -38,6 +39,13 @@ CODE_FORM = ''' ''' +PASSWORD_FORM = ''' +
+ Telegram password: + +
+''' + # Session name, API ID and hash to use; loaded from environmental variables SESSION = os.environ.get('TG_SESSION', 'quart') API_ID = int(get_env('TG_API_ID', 'Enter your API ID: ')) @@ -95,7 +103,13 @@ async def root(): await client.send_code_request(phone) if 'code' in form: - await client.sign_in(code=form['code']) + try: + await client.sign_in(code=form['code']) + except SessionPasswordNeededError: + return await render_template_string(BASE_TEMPLATE, content=PASSWORD_FORM) + + if 'password' in form: + await client.sign_in(password=form['password']) # If we're logged in, show them some messages from their first dialog if await client.is_user_authorized():