【问题标题】:Get geolocation via Telegram bot using inline使用内联通过 Telegram 机器人获取地理位置
【发布时间】:2021-10-22 08:25:47
【问题描述】:

如果您通过 Telegram 中的内联使用机器人,机器人可以请求用户的位置,如果在机器人设置中启用了此功能。 telethon.events.inlinequery.InlineQuery 类负责获取地理位置。

这是我尝试获取地理位置以便将来使用纬度和经度的伪代码:

from telethon import TelegramClient, events

@client.on(events.InlineQuery)
async def handler(event):
    location = event.geo
    builder = event.builder

    await event.answer([
        builder.article("Coordinates: ", text="Long: " + location.long + "\nLat: " + location.lat),
    ])

但我什么都做不了。不断输出AttributeError: 'NoneType' object has no attribute 'lat'

如果您能帮助我获取和使用这些数据,我将不胜感激。

【问题讨论】:

  • 这是一个错误。我已经打开github.com/LonamiWebs/Telethon/issues/3136 来跟踪这个问题。
  • @Lonami 谢谢!我将等待修复此错误,因为我确实需要获取地理位置。其实我的bot整个工作都是基于这个的。
  • 这段代码是必须只使用telethon库执行还是你也使用其他库?
  • @NematilloOchilov 这一切都只适用于电视节目。获取地理位置嵌入在电报 api 本身中,但由于某种原因,telethon 只是执行返回,如果您查看上面来自 Lonami 的链接。据我了解,我只需要等待修复此错误即可。
  • 在 pyrogram 或 pytelegrambotapi 库中,我可以快速创建您所要求的内容

标签: python python-3.x telegram telethon telegram-api


【解决方案1】:

这是 Telethon v1.23.0 中的一个错误。解决方案是更新到更高版本(一旦发布)。同时,您仍然可以通过原始更新获取geo:

from telethon import TelegramClient, events

@client.on(events.InlineQuery)
async def handler(event):
    location = event.query.geo
    #                ^^^^^ raw update query
    builder = event.builder

    if location is None:
        # you still should check if the location is None because user may deny it or not have the GPS on
        return await event.answer([builder.article('Geo must be enabled!', text='Please enable GPS to use this bot'])

    await event.answer([
        builder.article("Coordinates: ", text="Long: " + location.long + "\nLat: " + location.lat),
    ])

【讨论】:

  • 谢谢。在更新发布之前,我会使用你的方法。
猜你喜欢
  • 2018-07-17
  • 2020-05-30
  • 1970-01-01
  • 2021-11-07
  • 2020-08-01
  • 2022-12-26
  • 1970-01-01
  • 2013-01-16
  • 1970-01-01
相关资源
最近更新 更多