Search code examples
pythontelethon

Chaining Telethon start methods


I have been using telethon for a long time with two clients, one for a bot (with bot token) and another for my user (using phone).

I always thought two separate clients were necessary (are them?) but I recently saw this in the documentation: enter image description here https://docs.telethon.dev/en/stable/modules/client.html#telethon.client.auth.AuthMethods.start

But when I go to test it I got:

UserWarning:

the session already had an authorized user so it did not login to the user account using the provided phone (it may not be using the user you expect)

So I don't understand if the example indicates that I can have a single client to control a bot and a userbot, if one start(...) overrides the other or if the documentation example is wrong directly.

On the other hand, if I use that example code (including the last with part) I get:

RuntimeError: You must use "async with" if the event loop is running (i.e. you are inside an "async def")

And lastly, my ide was warning me when passing a phone as a string because it expected typing.Callable[[], str].


Solution

  • The documentation says "initialization can be chained". Initialization is this line:

    client = TelegramClient(...)
    

    and you can chain .start() there:

    client = await TelegramClient(...).start(...)
    

    but it doesn't mean you can chain multiple calls to start(). Indeed, if you want to control more than one account, you will need separate clients.