Uomolepre
Uomolepre

Reputation: 121

How to have two client Telethon running concurrently

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

  1. First Chat: receive a message
  2. First Chat: stop and wait for the second chat
  3. Second Chat: start
  4. Second Chat: receive only a single message
  5. Second Chat: stop
  6. 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()

Upvotes: 2

Views: 4031

Answers (3)

Alexandr Milko
Alexandr Milko

Reputation: 34

Try multiprocessing python library?

def run_in_parallel(fn, args):
    proc = []
    for client in clients_work.keys():
        p = Process(target=fn, args=args)
        p.start()
        proc.append(p)
    for p in proc:
        p.join()

Upvotes: 0

Dracula
Dracula

Reputation: 9

Hey i was trying to run one bot and one client so this worked out for me , Hope it works for you

import asyncio
client = TelegramClient('1', api_id, api_hash)
client2 = TelegramClient('2', api_id, api_hash)


loop = asyncio.get_event_loop()
client1.start()
client2.start()
loop.run_forever()

Upvotes: 0

Lonami
Lonami

Reputation: 7076

client.run_until_disconnected will run the asyncio 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:

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)
client.start()

@client2.on(events.NewMessage(chats=xxxxxxx))
async def second(event):
    second_message = event.raw_text
    print(second_message)
client2.start()

client.loop.run_forever()

Please read the asyncio documentation to learn about different ways in which you can manage async code, including concepts such as asyncio.gather, which would also work here.

Upvotes: 2

Related Questions