работаем с этим кодом:< /p>
Код: Выделить всё
import asyncio
from telethon import TelegramClient, events
api_id = id
api_hash = 'hash'
client = TelegramClient('session_machine_1.session', api_id, api_hash)
@client.on(events.NewMessage(chats='chat'))
async def handler(event):
print("New message received")
print(event.raw_text)
async def console_input():
loop = asyncio.get_running_loop()
while True:
command = await loop.run_in_executor(None, input, "> ")
if command.lower() == "exit":
print("Wrapping up the session...")
await client.disconnect()
print("The session is over.")
break
async def main():
await client.connect()
if not await client.is_user_authorized():
await client.start()
print("The bot is running. Type ‘exit’ in the console to terminate.")
await asyncio.gather(client.run_until_disconnected(), console_input())
asyncio.run(main())
Подробнее здесь: https://stackoverflow.com/questions/790 ... -real-time