简体   繁体   English

如何同时使用 3 个或更多电报客户端?

[英]How to use 3 or more telegram clients at the same time?

i want to use 3 or more telegram clients at the same time, with 1 or/and 2 clients i don't have problems, but with 3 clients i get errors.我想同时使用 3 个或更多电报客户端,使用 1 个或/和 2 个客户端我没有问题,但是使用 3 个客户端我会出错。

    client2 = TelegramClient('session1', api_id2, api_hash2)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\telethon\client\telegrambaseclient.py", line 294, in __init__
    session.set_dc(
  File "C:\Python311\Lib\site-packages\telethon\sessions\sqlite.py", line 168, in set_dc
    self._update_session_table()
  File "C:\Python311\Lib\site-packages\telethon\sessions\sqlite.py", line 194, in _update_session_table
    c.execute('delete from sessions')
sqlite3.OperationalError: database is locked

What i want to do:我想做的事:

I want to use multiple accounts, one will only stay in groups, and when someone join in the group, the first account will get event "event.user_joined" and get the member id and then, using others account send them a private message (I already realized this part, but only with 2 accounts) but i want, for every 50 messages sent, to switch to next account.我想使用多个帐户,一个只会留在组中,当有人加入组时,第一个帐户将获取事件“event.user_joined”并获取成员 ID,然后使用其他帐户向他们发送私人消息(我已经意识到这部分,但只有 2 个帐户)但我希望每发送 50 条消息,就切换到下一个帐户。 In this case, first 50 messages to be sent by "client1", next 50 messages to be sent by "client2" until last client i have (I want atleast 6) and then start again.在这种情况下,前 50 条消息由“client1”发送,接下来的 50 条消息由“client2”发送,直到我拥有的最后一个客户端(我想要至少 6 个)然后重新开始。

This is the code im using now这是我现在使用的代码



    @client.on(events.ChatAction)
    async def handler(event):
        index = 0
        if (event.user_added or event.user_joined):
            user = await event.get_user()
            receiver = InputPeerUser(user.id,user.access_hash)
            index = 0
            try:  
                if index < 50:
                    await client1.send_message(receiver, message)
                    print('Message sent successfully!') 
                elif index < 100:
                    await client2.send_message(receiver, message)
                    print('Message sent successfully!') 
                elif index < 150:
                    await client3.send_message(receiver, message)
                    print('Message sent successfully!') 
                # elif index < 200:
                    # await client4.send_message(receiver, message)
                    # print('Message sent successfully!') 
                # elif index < 250:
                    # await client5.send_message(receiver, message)
                    # print('Message sent successfully!') 
                elif index < 200:
                    index == 0
                
            except:
                pass

i used this part of code for logging in more than 2 clients.我使用这部分代码登录了 2 个以上的客户端。

client = TelegramClient('session', api_id, api_hash)
client.start()
client1 = TelegramClient('session1', api_id1, api_hash1)
client1.start()
client2 = TelegramClient('session1', api_id2, api_hash2)
client2.start()
client3 = TelegramClient('session1', api_id3, api_hash3)
client3.start()
client4 = TelegramClient('session1', api_id4, api_hash4)
client4.start()
client5 = TelegramClient('session1', api_id5, api_hash5)
client5.start()

And this is the error i get, when im trying to connect the 3rd client这是我在尝试连接第三个客户端时遇到的错误

PS C:\Users\37378\Desktop\Telegram new member dm> python .\main.py
Please enter your phone (or bot token): 6282274692947
Please enter the code you received: 30365
Signed in successfully as Dufufj Ff
Please enter your phone (or bot token): 6281996803497
Please enter the code you received: 63977
Signed in successfully as Hduduf
Traceback (most recent call last):
  File "C:\Users\37378\Desktop\Telegram new member dm\main.py", line 31, in <module>
    client2.start()
  File "C:\Users\37378\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\client\auth.py", line 134, in start
    else self.loop.run_until_complete(coro)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 649, in run_until_complete
    return future.result()
  File "C:\Users\37378\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\client\auth.py", line 141, in _start
    await self.connect()
  File "C:\Users\37378\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\client\telegrambaseclient.py", line 537, in connect
    self.session.auth_key = self._sender.auth_key
  File "C:\Users\37378\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\sessions\sqlite.py", line 180, in auth_key
    self._update_session_table()
  File "C:\Users\37378\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\sessions\sqlite.py", line 194, in _update_session_table
    c.execute('delete from sessions')
sqlite3.OperationalError: database is locked
Task was destroyed but it is pending!
task: <Task pending name='Task-47' coro=<Connection._send_loop() running at C:\Users\37378\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\network\connection\connection.py:311> wait_for=<Future pending cb=[Task.task_wakeup()]>>
Task was destroyed but it is pending!
task: <Task pending name='Task-48' coro=<Connection._recv_loop() running at C:\Users\37378\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\network\connection\connection.py:329> wait_for=<Future pending cb=[Task.task_wakeup()]>>
Task was destroyed but it is pending!
task: <Task pending name='Task-49' coro=<MTProtoSender._send_loop() running at C:\Users\37378\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\network\mtprotosender.py:462> wait_for=<Future pending cb=[Task.task_wakeup()]>>
Task was destroyed but it is pending!
task: <Task pending name='Task-50' coro=<MTProtoSender._recv_loop() running at C:\Users\37378\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\network\mtprotosender.py:505> wait_for=<Future pending cb=[Task.task_wakeup()]>>

Change each client to use different session names, and properly start them (right now, you're only starting client repeatedly)将每个客户端更改为使用不同的 session 名称,并正确启动它们(现在,您只是重复启动client

client = TelegramClient('session', api_id, api_hash)
client.start()
client1 = TelegramClient('session1', api_id1, api_hash1)
client1.start()
client2 = TelegramClient('session2', api_id2, api_hash2)
client2.start()
client3 = TelegramClient('session3', api_id3, api_hash3)
client3.start()
client4 = TelegramClient('session4', api_id4, api_hash4)
client4.start()
client5 = TelegramClient('session5', api_id5, api_hash5)
client5.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM