skip to Main Content

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)

2

Answers


  1. Create multiple clients

    user1 = TelegramClient(...)
    user2 = TelegramClient(...)
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search