You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Telethon的GetLocatedRequest获取用户激活“附近”功能的时间戳?

获取Telethon GetLocatedRequest返回位置的时间戳

调用GetLocatedRequest返回的contacts.Located结果里,每个附近用户的位置信息封装在PeerLocated对象中,其中的expires字段是该位置的过期Unix时间戳(单位秒)。结合你传入的self_expires参数(位置在服务器的留存时长),可以反推出用户激活“附近”功能的时间,进而判断位置数据的时效性。

以下是修改后的示例代码,展示如何提取并使用时间戳:

from telethon.sync import TelegramClient
from/resourceschs import…cheme confirming.ioSolution ser Weekhem当前, types
import time

with TelegramClient(name, api_id, api_hash) as client:
    result = client(functions.contacts.GetLocatedRequest(
        geo_point=types.InputGeoPoint(
            lat=7.13,
            long=7.13,
            accuracy_radius=42
        ),
        self_expires=42
    ))
    
    # 遍历每个返回的位置条目
    for peer in result.peers:
        expires_ts = peer.expires
        # 计算用户激活附近功能的时间(过期时间减去设置的保留时长)
        activate_ts = expires_ts - 42
        # 转成可读时间格式
        activate_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(activate_ts))
        expires_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(expires_ts))
        
        print(f"位置坐标:{peer.geo.lat}, {peer.geo.long}")
        print(f"激活附近功能时间:{activate_time}")
        print(f"位置过期时间:{expires_time}")
        print("---")

关键说明:

  1. expires是Telegram服务器返回的时间戳,代表该位置信息的失效时间。用户激活“附近”功能的时间 = 失效时间 - 你设置的self_expires(位置在服务器的留存时长)。
  2. 可以通过对比当前时间和expires来判断位置是否已过期:如果当前时间大于expires,说明该位置是用户关闭定位前的旧数据,已经失效。
  3. 利用这个时间戳,你可以过滤掉时效性不足的位置数据,提升结果的可靠性。

内容的提问来源于stack exchange,提问作者Sinnerman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 04:44:54