mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-03 19:50:15 +03:00
Apply suggestions from #1229
This commit is contained in:
parent
c61a5c8fc6
commit
1be54035e8
|
@ -1,38 +1,11 @@
|
||||||
import base64
|
import base64
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from quart import Quart, render_template_string, request, session
|
import hypercorn.asyncio
|
||||||
|
from quart import Quart, render_template_string, request
|
||||||
|
|
||||||
from telethon import TelegramClient, utils
|
from telethon import TelegramClient, utils
|
||||||
|
|
||||||
BASE_TEMPLATE = """
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Telethon + Quart</title>
|
|
||||||
</head>
|
|
||||||
<body>{{ content | safe }}</body>
|
|
||||||
</html>
|
|
||||||
"""
|
|
||||||
|
|
||||||
PHONE_FORM = """
|
|
||||||
<form action="/" method="post">
|
|
||||||
Phone (international format): <input name="phone" type="text" placeholder="+34600000000">
|
|
||||||
<input type="submit">
|
|
||||||
</form>
|
|
||||||
"""
|
|
||||||
|
|
||||||
CODE_FORM = """
|
|
||||||
<form action="/" method="post">
|
|
||||||
Telegram code: <input name="code" type="text" placeholder="70707">
|
|
||||||
<input type="submit">
|
|
||||||
</form>
|
|
||||||
"""
|
|
||||||
|
|
||||||
app = Quart(__name__)
|
|
||||||
app.secret_key = "CHANGE THIS TO SOMETHING SECRET"
|
|
||||||
|
|
||||||
|
|
||||||
def get_env(name, message):
|
def get_env(name, message):
|
||||||
if name in os.environ:
|
if name in os.environ:
|
||||||
|
@ -40,11 +13,45 @@ def get_env(name, message):
|
||||||
return input(message)
|
return input(message)
|
||||||
|
|
||||||
|
|
||||||
|
BASE_TEMPLATE = '''
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset='UTF-8'>
|
||||||
|
<title>Telethon + Quart</title>
|
||||||
|
</head>
|
||||||
|
<body>{{ content | safe }}</body>
|
||||||
|
</html>
|
||||||
|
'''
|
||||||
|
|
||||||
|
PHONE_FORM = '''
|
||||||
|
<form action='/' method='post'>
|
||||||
|
Phone (international format): <input name='phone' type='text' placeholder='+34600000000'>
|
||||||
|
<input type='submit'>
|
||||||
|
</form>
|
||||||
|
'''
|
||||||
|
|
||||||
|
CODE_FORM = '''
|
||||||
|
<form action='/' method='post'>
|
||||||
|
Telegram code: <input name='code' type='text' placeholder='70707'>
|
||||||
|
<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: '))
|
||||||
API_HASH = get_env('TG_API_HASH', 'Enter your API hash: ')
|
API_HASH = get_env('TG_API_HASH', 'Enter your API hash: ')
|
||||||
|
|
||||||
|
# Telethon client
|
||||||
|
client = TelegramClient(SESSION, API_ID, API_HASH)
|
||||||
|
client.parse_mode = 'html' # <- Render things nicely
|
||||||
|
phone = None
|
||||||
|
|
||||||
|
# Quart app
|
||||||
|
app = Quart(__name__)
|
||||||
|
app.secret_key = 'CHANGE THIS TO SOMETHING SECRET'
|
||||||
|
|
||||||
|
|
||||||
# Helper method to format messages nicely
|
# Helper method to format messages nicely
|
||||||
async def format_message(message):
|
async def format_message(message):
|
||||||
|
@ -64,36 +71,68 @@ async def format_message(message):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/', methods=['GET', 'POST'])
|
# Connect the client before we start serving with Quart
|
||||||
async def root():
|
@app.before_serving
|
||||||
client = TelegramClient(SESSION, API_ID, API_HASH)
|
async def startup():
|
||||||
await client.connect()
|
await client.connect()
|
||||||
|
|
||||||
if request.method == "POST":
|
|
||||||
|
# After we're done serving (near shutdown), clean up the client
|
||||||
|
@app.after_serving
|
||||||
|
async def cleanup():
|
||||||
|
await client.disconnect()
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/', methods=['GET', 'POST'])
|
||||||
|
async def root():
|
||||||
|
# We want to update the global phone variable to remember it
|
||||||
|
global phone
|
||||||
|
|
||||||
|
# Check form parameters (phone/code)
|
||||||
form = await request.form
|
form = await request.form
|
||||||
if 'phone' in form:
|
if 'phone' in form:
|
||||||
session["phone"] = form["phone"]
|
phone = form['phone']
|
||||||
await client.send_code_request(form["phone"])
|
await client.send_code_request(phone)
|
||||||
|
|
||||||
if 'code' in form:
|
if 'code' in form:
|
||||||
session["code"] = form["code"]
|
await client.sign_in(code=form['code'])
|
||||||
|
|
||||||
if "phone" not in session:
|
# If we're logged in, show them some messages from their first dialog
|
||||||
return await render_template_string(BASE_TEMPLATE, content=PHONE_FORM)
|
if await client.is_user_authorized():
|
||||||
|
|
||||||
if "code" not in session:
|
|
||||||
return await render_template_string(BASE_TEMPLATE, content=CODE_FORM)
|
|
||||||
|
|
||||||
await client.sign_in(code=session["code"])
|
|
||||||
# They are logged in, show them some messages from their first dialog
|
# They are logged in, show them some messages from their first dialog
|
||||||
dialog = (await client.get_dialogs())[0]
|
dialog = (await client.get_dialogs())[0]
|
||||||
result = '<h1>{}</h1>'.format(dialog.title)
|
result = '<h1>{}</h1>'.format(dialog.title)
|
||||||
async for m in client.iter_messages(dialog, 10):
|
async for m in client.iter_messages(dialog, 10):
|
||||||
result += await(format_message(m))
|
result += await(format_message(m))
|
||||||
|
|
||||||
await client.disconnect()
|
|
||||||
return await render_template_string(BASE_TEMPLATE, content=result)
|
return await render_template_string(BASE_TEMPLATE, content=result)
|
||||||
|
|
||||||
|
# Ask for the phone if we don't know it yet
|
||||||
|
if phone is None:
|
||||||
|
return await render_template_string(BASE_TEMPLATE, content=PHONE_FORM)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
# We have the phone, but we're not logged in, so ask for the code
|
||||||
app.run()
|
return await render_template_string(BASE_TEMPLATE, content=CODE_FORM)
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
await hypercorn.asyncio.serve(app, hypercorn.Config())
|
||||||
|
|
||||||
|
|
||||||
|
# By default, `Quart.run` uses `asyncio.run()`, which creates a new asyncio
|
||||||
|
# event loop. If we create the `TelegramClient` before, `telethon` will
|
||||||
|
# use `asyncio.get_event_loop()`, which is the implicit loop in the main
|
||||||
|
# thread. These two loops are different, and it won't work.
|
||||||
|
#
|
||||||
|
# So, we have to manually pass the same `loop` to both applications to
|
||||||
|
# make 100% sure it works and to avoid headaches.
|
||||||
|
#
|
||||||
|
# To run Quart inside `async def`, we must use `hypercorn.asyncio.serve()`
|
||||||
|
# directly.
|
||||||
|
#
|
||||||
|
# This example creates a global client outside of Quart handlers.
|
||||||
|
# If you create the client inside the handlers (common case), you
|
||||||
|
# won't have to worry about any of this, but it's still good to be
|
||||||
|
# explicit about the event loop.
|
||||||
|
if __name__ == '__main__':
|
||||||
|
client.loop.run_until_complete(main())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user