Telethon icon indicating copy to clipboard operation
Telethon copied to clipboard

Unable to run 2 or more sessions

Open walkerherb opened this issue 2 years ago • 5 comments

Trying to make parsing from 4 different chats using 2 different accounts - 2 chats per each account. In different programs it works but in same nothing appears:

Tried multiple variants, including event loops but wasn't able to make it working. Could anyone show the code of event loops how it would work or other way

import asyncio
from telethon import TelegramClient, sync, events
import asyncio
import string
import sys

client1 = TelegramClient('session1', api_id1, api_hash1)
client2 = TelegramClient('session2', api_id1, api_hash1)

#Chats block
chats1 =("chat1",
        "chat2")

chats2 =("chat3",
        "chat4")

@client1.on(events.NewMessage(chats=tuple(chats1)))
async def normal_handler1(event):

    user_message = event.message.to_dict()['message']
    print(user_message)

@client2.on(events.NewMessage(chats=tuple(chats2)))
async def normal_handler2(event):

    user_message = event.message.to_dict()['message']
    print(user_message)

client1.start()
client2.start()

async def main():
    return await asyncio.gather(
        client1.run_until_disconnected(),
        client2.run_until_disconnected()
    )

asyncio.run(main())

walkerherb avatar Jan 30 '22 16:01 walkerherb

What's the issue? Is there an error? Are you using the development version?

Lonami avatar Jan 30 '22 16:01 Lonami

What's the issue? Is there an error? Are you using the development version?

The problem is that there is no error it just doesn't work and that's it, means chats are not parsed at all. There was alike issue on stackoverflow -> https://stackoverflow.com/questions/70320400/run-multiple-telethon-clients

But I wasn't able to properly rewrite code for event loops as on that issue were classes and I needed it for code without classes like in my example. Maybe you could help me to make it work ? Would be appreciated

Btw: I'm using version Telethon 1.24.0 according to pip list

walkerherb avatar Jan 31 '22 03:01 walkerherb

@Lonami still wasn't able to find solution, could you help with code ? I really don't know how to rewrite it with event loops to make it work

walkerherb avatar Feb 01 '22 10:02 walkerherb

@Lonami do you know how to solve it ?

walkerherb avatar Feb 05 '22 19:02 walkerherb

I just run clients in different threads.

w1ld32 avatar Feb 18 '22 06:02 w1ld32

@w1ld32 do you have an example you can share on how you are able to run multiple clients using different threads plz.

henishoye avatar Oct 14 '22 03:10 henishoye

I'm going to close since because I have been unable to reproduce the issue (I've been able to run more than one client at the same time just fine).

Lonami avatar Oct 14 '22 15:10 Lonami

ok may be I just need help. Is there any way to get a code example. Here is what I have tried to do but didn't really worked. No error message or anything, it is just not capturing new messages from the bot.

import asyncio
from telethon import TelegramClient, sync, events
import asyncio
import string
import sys

async def start(bot_token)
    client = TelegramClient(f'bot:{bot_token}', api_id1, api_hash1)
    BOT = client.start(bot_token='bot_token')

    async def handler(event):
        print(event)

    BOT.add_event_handler(handler, telethon.events.Raw)
    BOT.catch_up()
    BOT.run_until_disconnected()

async def main():
    return await asyncio.gather(
        start(bot_token1),start(bot_token2),start(bot_token3)
    )

asyncio.run(main())

henishoye avatar Oct 14 '22 15:10 henishoye

I have tried this code:

async with \
        TelegramClient('user', os.getenv('TG_ID'), os.getenv('TG_HASH')) as client, \
        TelegramClient('bot',  os.getenv('TG_ID'), os.getenv('TG_HASH')) as bot:

    @client.on(events.Raw)
    async def handler(event):
        print('User', event)

    @bot.on(events.Raw)
    async def handler(event):
        print('Bot', event)

    await client.send_message('bot', 'Hi')
    await bot.send_message('user, 'Hello')
    await client.run_until_disconnected()

I have had no issues receiving both updates:

Bot UpdateNewMessage(message=Message(...message='Hi'...))
User UpdateShortMessage(...message='Hello'...)

It might be something depending on your configuration, or bot settings, but Telethon is very likely working fine.

Lonami avatar Oct 14 '22 16:10 Lonami

I see. yes that will work just fine. But if you try to have two clients listening at the same time and have something like

await client1.run_until_disconnected()
await client2.run_until_disconnected()

it will stop working (at least for me)

henishoye avatar Oct 15 '22 01:10 henishoye

My code has two clients listening at the same time. This code:

await client1.run_until_disconnected()
await client2.run_until_disconnected()

Waits for the first client to disconnect. Once that happens, it waits for the second client to disconnect. While waiting, both clients which are still connected can receive updates just fine (this is why my code only uses one run_until_disconnected(), because it's essentially the same thing; the more correct solution would be to wait on both and exit as soon as one disconnects).

So, even if I put those two waits, my code would probably still work for me.

Lonami avatar Oct 15 '22 08:10 Lonami