python - how can I fix the error? TypeError: Lock.__init__() got an unexpected keyword argument 'loop' - Stack O

This simple program, written in Python, allows you to find out when other users have logged on to the n

This simple program, written in Python, allows you to find out when other users have logged on to the network. But an error occurs at startup. The project is implemented using the telethon library, based on Python 3.10, and the Telegram API.

from telethon import TelegramClient
from telethon.tl.types import UserStatusOnline
from datetime import datetime
from time import sleep

API_ID = 12345678 
API_HASH = '123456789'

USERNAMES = [
    'your_username'
]

PAIRS = [
]

TRACK_FREQUENCY = 5

client = TelegramClient('bot', API_ID, API_HASH)


async def main():
    while True:
        online = []
        offline = []

        for i in USERNAMES:
            entity = await client.get_entity(i)
            name = entity.first_name + ' ' + entity.last_name
            if isinstance(entity.status, UserStatusOnline):
                # print if user tracked is online
                print('[' + datetime.now().isoformat() + ']: ' + name + ' в онлайне!')
                online.append(entity.username)
            else:
                offline.append(entity.username)

        for u in PAIRS:
            # check if both users in the given pair are online right now
            if u[0] in online and u[1] in online:
                print('[' + datetime.now().isoformat() + ']: ' + u[0] + ' и ' + u[1] + ' оба сейчас находятся в онлайне!')
            elif u[0] in offline and u[1] in offline:
                print('[' + datetime.now().isoformat() + ']: ' + u[0] + ' и ' + u[1] + ' оба сейчас находятся в офлайне!')

        sleep(TRACK_FREQUENCY)

if __name__ == "__main__":
    print("Трекер начал работу...")
    client.start()
    print("Трекер успешно подключился к Telegram'у!")
    with client:
        client.loop.run_until_complete(main())

The ID and Hash are hidden on purpose. As soon as I run the code, an error appears:

Traceback (most recent call last):
  File "/workspaces/TG_Online_Tracker1/main.py", line 29, in <module>
    client = TelegramClient('bot', API_ID, API_HASH)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/codespace/.local/lib/python3.12/site-packages/telethon/client/telegrambaseclient.py", line 302, in __init__
    self._sender = MTProtoSender(
                   ^^^^^^^^^^^^^^
  File "/home/codespace/.local/lib/python3.12/site-packages/telethon/network/mtprotosender.py", line 58, in __init__
    self._connect_lock = asyncio.Lock(loop=loop)
                         ^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Lock.__init__() got an unexpected keyword argument 'loop'

I've already updated all my pip freezes, but the error hasn't changed.

This simple program, written in Python, allows you to find out when other users have logged on to the network. But an error occurs at startup. The project is implemented using the telethon library, based on Python 3.10, and the Telegram API.

from telethon import TelegramClient
from telethon.tl.types import UserStatusOnline
from datetime import datetime
from time import sleep

API_ID = 12345678 
API_HASH = '123456789'

USERNAMES = [
    'your_username'
]

PAIRS = [
]

TRACK_FREQUENCY = 5

client = TelegramClient('bot', API_ID, API_HASH)


async def main():
    while True:
        online = []
        offline = []

        for i in USERNAMES:
            entity = await client.get_entity(i)
            name = entity.first_name + ' ' + entity.last_name
            if isinstance(entity.status, UserStatusOnline):
                # print if user tracked is online
                print('[' + datetime.now().isoformat() + ']: ' + name + ' в онлайне!')
                online.append(entity.username)
            else:
                offline.append(entity.username)

        for u in PAIRS:
            # check if both users in the given pair are online right now
            if u[0] in online and u[1] in online:
                print('[' + datetime.now().isoformat() + ']: ' + u[0] + ' и ' + u[1] + ' оба сейчас находятся в онлайне!')
            elif u[0] in offline and u[1] in offline:
                print('[' + datetime.now().isoformat() + ']: ' + u[0] + ' и ' + u[1] + ' оба сейчас находятся в офлайне!')

        sleep(TRACK_FREQUENCY)

if __name__ == "__main__":
    print("Трекер начал работу...")
    client.start()
    print("Трекер успешно подключился к Telegram'у!")
    with client:
        client.loop.run_until_complete(main())

The ID and Hash are hidden on purpose. As soon as I run the code, an error appears:

Traceback (most recent call last):
  File "/workspaces/TG_Online_Tracker1/main.py", line 29, in <module>
    client = TelegramClient('bot', API_ID, API_HASH)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/codespace/.local/lib/python3.12/site-packages/telethon/client/telegrambaseclient.py", line 302, in __init__
    self._sender = MTProtoSender(
                   ^^^^^^^^^^^^^^
  File "/home/codespace/.local/lib/python3.12/site-packages/telethon/network/mtprotosender.py", line 58, in __init__
    self._connect_lock = asyncio.Lock(loop=loop)
                         ^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Lock.__init__() got an unexpected keyword argument 'loop'

I've already updated all my pip freezes, but the error hasn't changed.

Share asked Mar 4 at 11:42 kitdoomkitdoom 1 2
  • What version of telethon are you using? It looks like you are using python 3.12. Is telethon compatible? – quamrana Commented Mar 4 at 11:51
  • I've already updated all my pip freezes Please show the exact command you used for this. – John Gordon Commented Mar 4 at 14:18
Add a comment  | 

2 Answers 2

Reset to default 0

You can try asyncio.run() for the event loop:

if __name__ == "__main__":
    print("Трекер начал работу...")
    client.start()
    print("Трекер успешно подключился к Telegram'у!")
    asyncio.run(main())

The loop argument for asyncio.Lock() has been removed in Python 3.10. You should update your telethon lib, which seems to be quite old and does not reflect that change.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745045966a4608099.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信