mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-05 12:40:22 +03:00
Update telethon_and_quart
better and funnier example + create_task instead of run_until_complete
This commit is contained in:
parent
bf4d2158a8
commit
29871ac33f
|
@ -1,15 +1,21 @@
|
||||||
from quart import Quart
|
from quart import Quart
|
||||||
from telethon import TelegramClient, events
|
from telethon import TelegramClient, events
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import json
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
app = Quart(__name__)
|
app = Quart(__name__)
|
||||||
|
#
|
||||||
|
# TELEGREM_TOKEN = "complete me"
|
||||||
|
# API_ID = 0 # complete me
|
||||||
|
# API_HASH = 'complete me'
|
||||||
|
|
||||||
|
TELEGREM_TOKEN = "814032872:AAFqS3-RyK4ECdzPBRhW1_2nsrmt5Al4UtA"
|
||||||
|
API_ID = 669543
|
||||||
|
API_HASH = 'ff524b09e88b6c657c6f524697458ff1'
|
||||||
|
messages_list = []
|
||||||
|
|
||||||
TELEGREM_TOKEN = "complete me"
|
|
||||||
API_ID = 0 # complete me
|
|
||||||
API_HASH = 'complete me'
|
|
||||||
|
|
||||||
# exapmple class for telegrem
|
|
||||||
class Telegrem:
|
class Telegrem:
|
||||||
def __init__(self, bot_token, api_id, api_hash):
|
def __init__(self, bot_token, api_id, api_hash):
|
||||||
self.bot_token = bot_token
|
self.bot_token = bot_token
|
||||||
|
@ -18,22 +24,22 @@ class Telegrem:
|
||||||
self.client = TelegramClient('bot', api_id, api_hash)
|
self.client = TelegramClient('bot', api_id, api_hash)
|
||||||
|
|
||||||
async def receive_message(self, event):
|
async def receive_message(self, event):
|
||||||
print(f"message is here {event.raw_text}")
|
print(f'message is here {event.raw_text}')
|
||||||
|
messages_list.append(event.raw_text)
|
||||||
|
|
||||||
async def start_polling(self):
|
async def start_polling(self):
|
||||||
await self.client.start(bot_token=self.bot_token)
|
await self.client.start(bot_token=self.bot_token)
|
||||||
self.client.add_event_handler(self.receive_message, events.NewMessage)
|
self.client.add_event_handler(self.receive_message, events.NewMessage)
|
||||||
|
|
||||||
# create a task that runs in the background and listen to telegrem
|
|
||||||
async def background():
|
async def background():
|
||||||
loop.create_task(Telegrem(TELEGREM_TOKEN, API_ID, API_HASH).start_polling())
|
telegrem = Telegrem(TELEGREM_TOKEN, API_ID, API_HASH)
|
||||||
|
loop.create_task(telegrem.start_polling())
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
async def hello():
|
async def hello():
|
||||||
return {'hello': 'world'}
|
return json.dumps(messages_list)
|
||||||
|
|
||||||
loop.run_until_complete(background())
|
loop.create_task(background())
|
||||||
|
|
||||||
# simultaneously run a Quart app
|
|
||||||
app.run(loop=loop)
|
app.run(loop=loop)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user