if hasattr(client, "listener_added") and client.listener_added:
return
@client.on(events.Raw())
async def update_handler(event):
sout(f"
client.listener_added = True
< /code>
async def load_existing_sessions():
"""Loads all Telegram users from saved session files at startup."""
session_files = \[f for f in os.listdir(SESSIONS_DIR) if f.endswith(".session")\]
for session_file in session_files:
session_path = os.path.join(SESSIONS_DIR, session_file)
client = TelegramClient(session_path, API_ID, API_HASH)
await client.connect()
if await client.is_user_authorized():
clients[me.phone] = client
asyncio.create_task(handle_updates(client)) # Start listening for messages
< /code>
async def start_listening():
for client in clients.values():
asyncio.create_task(handle_updates(client))
try:
await asyncio.Event().wait()
except:
client.disconnect()
< /code>
async def main():
await load_existing_sessions()
asyncio.create_task(manage_clients())
await start_listening()
< /code>
if __name__ == "__main__":
asyncio.run(main())
< /code>
- - I stopped the program
- - I sent a message (text="hello1") to a bot user from another account
- - I launched the program
- - and i have no updates, but i need to receive update with text="hello1"
- - I sent it again, but now with the text="hello2", while the program was running
- - and now I got the update (hello2) because the program was running