【发布时间】:2020-10-13 12:25:41
【问题描述】:
我正在尝试创建一个 python 脚本来检查用户是否在线。 我成功地使用 Telethon 在 Telegram 和 python 之间制作了一个赛季,但我真的很难获得正确的语法来获得我的一个联系人状态。 任何帮助都会被应用!
from telethon.tl.types import UserStatusOnline, UserStatusOffline, ContactStatus
from telethon.sync import TelegramClient
from datetime import datetime
### Client Side ###
phone = "+"
api_id =
api_hash = ""
client = TelegramClient(phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code: '))
else:
print("Logging Complete")
account = client.get_entity('chats_name')
if isinstance(account.status, UserStatusOffline):
if contact.online != False:
contact.online = False
event.respond(f'{utc2localtime(account.status.was_online).strftime(DATETIME_FORMAT)}: {contact.name} went offline.')
elif contact.last_offline != account.status.was_online:
if contact.last_offline is not None:
event.respond(f'{utc2localtime(account.status.was_online).strftime(DATETIME_FORMAT)}: {contact.name} went offline after being online for short time.')
else:
event.respond(f'{utc2localtime(account.status.was_online).strftime(DATETIME_FORMAT)}: {contact.name} went offline.')
contact.last_offline = account.status.was_online
elif isinstance(account.status, UserStatusOnline):
if contact.online != True:
contact.online = True
event.respond(f'{datetime.now().strftime(DATETIME_FORMAT)}: {contact.name} went online.')
else:
if contact.online != False:
contact.online = False
event.respond(f'{datetime.now().strftime(DATETIME_FORMAT)}: {contact.name} went offline.')
contact.last_offline = None
【问题讨论】:
标签: python telegram status telethon