Add password support for quart example (#1732)

This commit is contained in:
painor 2021-03-15 22:25:06 +01:00 committed by GitHub
parent 3d6a2bb945
commit ad0307fda6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 = '''
</form>
'''
PASSWORD_FORM = '''
<form action='/' method='post'>
Telegram password: <input name='password' type='text' placeholder='your password'>
<input type='submit'>
</form>
'''
# 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:
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():