4

I am trying to build a python script which requires login with multiple telegram accounts. I don't want to run a separate script for each account.I am using TELETHON. I know that there is something like create_new_connection in telethon, but I don't know how it can help me. Is there a way by which I can use just one python script and login using several accounts..? (If possible please include the code snippet to use in your answer)

Mayank
  • 71
  • 3
  • 6
  • > *I know that there is something like `create_new_connection` in telethon*. There isn't (it used to be a thing but was removed). In any case it wouldn't do what you want. – Lonami Jun 24 '20 at 16:21

2 Answers2

12

When you create a client, the first parameter you pass is the session file name. Different sessions can have different clients logged in, so it's enough to create multiple clients each with a different session file name for them to use different accounts:

user1 = TelegramClient('user1', api_id, api_hash)
user2 = TelegramClient('user2', api_id, api_hash)

Of course, you could also use a list to trivially support an arbitrary number of clients. What you do with these clients is up to you, although you will generally just take advantage of asyncio as explained in the Python documentation - Running Tasks Concurrently:

import asyncio

async def work(client):
    async with client:
        me = await client.get_me()
        print('Working with', me.first_name)

async def main():
    await asyncio.gather(
        work(TelegramClient('user1', api_id, api_hash)),
        work(TelegramClient('user2', api_id, api_hash)),
    )

asyncio.run(main())

You could, of course, run separate code for each, use conditionals, etc. It depends a lot on your needs, and what these are will influence your design too.

P i
  • 29,020
  • 36
  • 159
  • 267
Lonami
  • 5,945
  • 2
  • 20
  • 38
  • Yeah..! Thanks a lot buddy. I tried the above code, it's working perfectly fine.But I am having one small problem with events. How do I handle events using asyncio.gather..? – Mayank Jun 24 '20 at 18:43
  • Just like you can use client methods on each client, you can use them in decorators or to call add event handler. The documentation lists what methods are available. – Lonami Jun 24 '20 at 22:05
  • 1
    @Lonami could you please give a hint, what is the preferrable way to run multiple "polling" with Telethon? Is it fine to running multiple `create_task` with `clientX.start() clientX.run_until_disconnected()`? – Groosha Jul 18 '20 at 09:21
  • @Lonami Do you also know how to add or remove clients at runtime? – Alp Dec 13 '20 at 16:48
  • @Alp you will need to keep a list of spawned tasks so that they can all run at the same time. – Lonami Dec 13 '20 at 17:03
  • Thanks @Lonami. Care to show an example of how it could look like? – Alp Dec 27 '20 at 09:05
  • `create_task` is how you spawn new tasks, storing them somewhere (like `append`ing to a list) is just a good idea so you can cancel or refer to them later (they will run even if you don't store them, but then you lose some control). – Lonami Dec 28 '20 at 19:21
0

Create multiple clients

user1 = TelegramClient(...)
user2 = TelegramClient(...)
Qwerty-Space
  • 134
  • 1
  • 9
  • 1
    While this is the right way to go, the answer is a bit vague: it doesn't explain why this works, the fact that different session files are needed and why, and also does not explain how to run them both at the same time. – Lonami Jun 24 '20 at 16:22