Telethon: EventBuilder.self_id acts as a global and breaks when multiple clients are used

Checklist

  • The error is in the library’s code, and not in my own.
  • I have searched for this issue before posting it and there isn’t a duplicate.
  • I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/master.zip and triggered the bug in the latest version.

Code that causes the issue

import os
import glob
import asyncio

from telethon import TelegramClient, events

API_ID = os.getenv('API_ID')
API_HASH = os.getenv('API_HASH')


async def foo(event):
    await event.reply('bar')


async def make_client(session, loop):
    client = TelegramClient(session, API_ID, API_HASH, loop=loop)
    client.add_event_handler(foo, events.NewMessage(outgoing=True, pattern='foo'))
    if not client.is_connected():
        await client.connect()
    me = await client.get_me()
    print(me)
    return client


def main():
    loop = asyncio.get_event_loop()
    clients = []
    
    # load all available sessions
    for file in glob.glob('sessions/*.session'):
        c = loop.run_until_complete(make_client(file, loop))
        clients.append(c)

    loop.run_forever()


if __name__ == '__main__':
    main()

Missbehaviour In a chat with one another, both users respond to outgoing messages of one of them.

For example: let’s say that we have 2 users (A and B), both have userbots, userbots’ clients have shared event loop. If users chat with one another then we will see that user A will also respond to the outgoing command of user B. (> denotes reply)

Expected dialog:

A: hello
B: foo
B: > bar

Actual dialog:

A: hello
B: foo
B: > bar
A: > bar

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Okay thanks for confirming and sorry for refusing this was a bug at the beginning (although the original issue never existed, this edited one did).