Telethon GetLocatedRequest:如何访问用户id和距离信息?

2024-10-03 13:31:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我整个星期都在看电视节目的文档,除了浏览互联网,我找不到答案

我已经完成了GetLocatedRequest并成功地收到了数据,但是我不知道如何访问要在脚本的其余部分中使用的用户id和距离信息。是否有要调用的特定对象来获取此数据?到目前为止,我唯一的工作是将输出复制粘贴到.txt文件并手动解析,这非常糟糕

我的代码如下。point0是我定位的起点,我想在user变量中获取用户id和距离。到目前为止,我已经尝试过user.peer、user.PeerLocated、user.user\u id之类的方法,但都不起作用。以前有人碰到过这个吗?下面是GetLocatedRequest的输出:

更新(Updates=[UpdatePeerLocated(peer=[PeerLocated(peer=peerluser(user_id=xxxxxx),expires=datetime.datetime(2038,1,19,3,14,7,tzinfo=datetime.timezone.utc),distance=100),PeerLocated(peer=peerluser(user_id=xxxxxxx),expires=datetime.datetime(2038,1,19,3,14,7,tzinfo=datetime.timezone.utc),distance=100)

from pprint import pprint
import re
import csv
import pandas as pd
import asyncio
import nest_asyncio
import configparser
import time
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon.tl.functions.messages import GetHistoryRequest
from telethon import functions, types
from telethon import errors
from telethon.tl.types import PeerLocated, PeerUser
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
from telethon.tl.types import PeerChannel, InputPeerChannel

nest_asyncio.apply()


# Reading Configs
config = configparser.ConfigParser()
config.read("config.ini")

# Setting configuration values
api_id = config['Telegram']['api_id']
api_hash = config['Telegram']['api_hash']
api_hash = str(api_hash)
phone = config['Telegram']['phone']
username = config['Telegram']['username']

client = TelegramClient("tes", api_id, api_hash)

async def loc0():
    await client.start()
    print('client starting')
    await client.get_me()
    print('complete')
    point0 = await client(functions.contacts.GetLocatedRequest(
        geo_point=types.InputGeoPoint(lat=41.1, long=69.0)))
    user = point0.PeerLocated
    print(user)
    print('\n point0 \n {}'.format(point0.stringify()))

Tags: fromimportclientapiidconfigdatetimehash
1条回答
网友
1楼 · 发布于 2024-10-03 13:31:27

该代码返回一个用户列表,而不仅仅是一个用户。要访问它们,您需要使用列表索引

users = point0.updates[0].peers
for user in users:
    print(user.peer.user_id)

相关问题 更多 >