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 |2 Answers
Reset to default 0You 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
telethon
are you using? It looks like you are using python 3.12. Istelethon
compatible? – quamrana Commented Mar 4 at 11:51