E
E
el-bekasto2021-03-06 10:21:34
Python
el-bekasto, 2021-03-06 10:21:34

How can I run multiple Telethon clients?

I found a number of answers, good ones, but these methods are only valid if there are a certain number of accounts. And I need the program to receive data from json accounts, the number of which can be as many as you like, one, two, at least twenty. I tried to implement it by creating a class and declaring an instance of this class for each account. But still, I failed: because in order to declare a decorator, it needs an explicit instance of TelegramClient, and it will not work to declare, because a session file will be created and data must be passed when declaring a client.

class Account:
    client = None
    def __init__(self, acc):
        client = TelegramClient(acc['name'], acc['api_id'], acc['api_hash'])
    
    @client.on(events.NewMessage)
    async def my_event_handler(event):
        pass

And in this case, it just swears that the None object does not have an on attribute. What other solution is there?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
say8hi, 2022-01-25
@say8hi

You need to use self:

class Account:
    def __init__(self, acc):
        self.client = TelegramClient(acc['name'], acc['api_id'], acc['api_hash'])

    def run(self):
        @self.client.on(events.NewMessage)
        async def my_event_handler(event):
            pass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question