I have two different Telegram chats. When the first one receives a message, it should wait for a message from another chat. After the second chat receives a message, it should stop running and retake the first chat.
EXAMPLE: It should repeat each time First chat receive a message
- First Chat: receive a message
- First Chat: stop and wait for the second chat
- Second Chat: start
- Second Chat: receive only a single message
- Second Chat: stop
- First Chat: retrieve the message from the second chat and continue.
Here is my code. I’m using python 3.9, Telethon and asyncio
client = TelegramClient('1', api_id, api_hash)
client2 = TelegramClient('2', api_id, api_hash)
@client.on(events.NewMessage(chats=yyyyyyy))
async def my_event_handler(event):
first_message = event.raw_text
print(first_message)
second_message = await second()
print(second_message)
client.start()
client.run_until_disconnected()
@client2.on(events.NewMessage(chats=xxxxxxx))
async def second(event):
print("I'm in")
second_m= event.raw_text
return second_m
await client2.disconnect()
client2.start()
client2.run_until_disconnected()
It never prints in console "I’m in". I thought to create a global shared variable, but in that way, Client1 never knows when it is updated.
UPDATE
I’ve tried to use only task and, after that, I’ve tried with asyncio.gather, but I never came back to the first loop client. In console always print the second message only 1 time (because it sticks in client2 event). I’m not going back to client1 and continue is loop.
client = TelegramClient('1', api_id, api_hash)
client2 = TelegramClient('2', api_id, api_hash)
@client.on(events.NewMessage(chats=yyyyyyy))
async def my_event_handler(event):
first_message = event.raw_text
print(first_message)
second_message = await asyncio.gather(task)
print(second_message)
client.start()
client.run_until_disconnected()
@client2.on(events.NewMessage(chats=xxxxxxx))
async def second(event):
print("I'm in")
second_m= event.raw_text
return second_m
await client2.disconnect()
client2.start()
client2.run_until_disconnected()
task = client2.loop.create_task(second(events.client2))
UPDATE 2
I’ve not found any solution to this problem. Actually, I’ve resolved it with only async.sleep() function and global variable.
In this way, I’ve changed the value of msg.
What I would like to do, is avoid asyncio.sleep() and find a way to "wake up" CLIENT1, after CLIENT2 is disconnected, and continue with its instruction (in this case: print msg) until a new Event comes.
msg = ""
@client.on(events.NewMessage(chats=xxxxxxxxx))
async def my_event_handler(event):
first_message = event.raw_text
print(first_message)
global client2
await client2.start()
await asyncio.sleep(5) #NOW IS SLEEPING FOR 5 SECONDS
print("Time ended")
print(msg)
@client2.on(events.NewMessage(chats=xxxxxx))
async def second(event):
print("I'm in")
global msg
msg = event.raw_text
await client2.disconnect()
client.start()
3
Answers
client.run_until_disconnected
will run theasyncio
event loop until the client is disconnected. You do not need to use this method, as long as the loop is running. Here’s the simplest way to do what you need:Please read the
asyncio
documentation to learn about different ways in which you can manageasync
code, including concepts such asasyncio.gather
, which would also work here.Hey i was trying to run one bot and one client so this worked out for me ,
Hope it works for you
Try
multiprocessing python library?