mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-25 10:53:44 +03:00
Add password support for quart example (#1732)
This commit is contained in:
parent
3d6a2bb945
commit
ad0307fda6
|
@ -5,6 +5,7 @@ import hypercorn.asyncio
|
||||||
from quart import Quart, render_template_string, request
|
from quart import Quart, render_template_string, request
|
||||||
|
|
||||||
from telethon import TelegramClient, utils
|
from telethon import TelegramClient, utils
|
||||||
|
from telethon.errors import SessionPasswordNeededError
|
||||||
|
|
||||||
|
|
||||||
def get_env(name, message):
|
def get_env(name, message):
|
||||||
|
@ -38,6 +39,13 @@ CODE_FORM = '''
|
||||||
</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 name, API ID and hash to use; loaded from environmental variables
|
||||||
SESSION = os.environ.get('TG_SESSION', 'quart')
|
SESSION = os.environ.get('TG_SESSION', 'quart')
|
||||||
API_ID = int(get_env('TG_API_ID', 'Enter your API ID: '))
|
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)
|
await client.send_code_request(phone)
|
||||||
|
|
||||||
if 'code' in form:
|
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 we're logged in, show them some messages from their first dialog
|
||||||
if await client.is_user_authorized():
|
if await client.is_user_authorized():
|
||||||
|
|
Loading…
Reference in New Issue
Block a user