【发布时间】:2020-10-15 09:00:02
【问题描述】:
所以我已经在这个脚本上工作了几天了,该脚本似乎可以正确运行而没有出现任何错误,但问题是我的脚本是获取特定用户的状态更改,而不是脚本打印每当我的联系人中的任何人改变他们的状态时,我都会对我自己产生影响。 请问,有人认为他可以帮助我吗?我已经坚持太久了,我真的很绝望让这个脚本有效..
顺便说一句,我添加 print(client.user_id) 只是为了看看它是否有效,并且我收到了我的联系人中任何人的用户 ID,他们采取了某种行动。
from telethon.tl.types import UserStatusOffline
from telethon.sync import TelegramClient
from telethon import events
from datetime import datetime
import time
### Client Side ###
target = ""
phone = "+"
api_id =
api_hash = ""
client = TelegramClient(phone, api_id, api_hash)
client.start()
if client.is_user_authorized():
print(f"Session started at : {datetime.now()}")
time.sleep(2)
print("Logging in to Telegram complete.")
time.sleep(2)
else:
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code: '))
print(f"Started listening to {target}'s status...")
time.sleep(2)
target_id = client.get_peer_id(target)
print(f"{target}'s User ID is : {target_id}")
time.sleep(2)
############################################
# First status check to see rather #
# the user is correctly online or not #
# once it prints hes correct statement #
# global value will be changed so it wont #
# be printed again and again. #
############################################
first_msg = False
async def first_con():
if first_msg == False:
account = await client.get_entity(target)
if isinstance(account.status, UserStatusOffline):
print(f"{target} is correctly Offline.")
first_msg = True
else:
print(f"{target} is correctly Online.")
first_msg = True
else:
print("Something went wrong checking correct status.")
##################EVENTS####################
# Only events thats occurred after script #
# first run will pop up with prints ! #
# Every event that doesn't come from the #
# target gets as "event from username" #
############################################
@client.on(events.UserUpdate())
async def handler(event):
await first_con()
time.sleep(2)
if event.user_id == target_id:
if event.online:
print(f"{target} went Online at : {datetime.now()}")
elif event.recently:
print(f"{target} was recently online at : {datetime.now()}")
elif event.typing:
print(f"{target} typed a message at : {datetime.now()}")
else:
print("Sorry there was an error.")
else:
#print("Event from non-intersting user.") debugging propuses only
client.run_until_disconnected()
【问题讨论】:
标签: python-3.x events telegram telethon