How can I get Live Location in telegram bot via python?

倾然丶 夕夏残阳落幕 提交于 2021-02-06 13:56:07

问题


I'm using the python-telegram-bot library. I want to track a user's Live Location, but I don't know how to do it. I try to use Job Queue:

def notification(bot, job):
  updates = bot.get_updates()
  print([u.message.location for u in updates])
# Add job to queue

job = job_queue.run_repeating(notification, 10, 1, context=chat_id)
chat_data['job'] = job

But updates are void. I want to track location every 1 minutes.


回答1:


Just to ellaborate on Seans answer: It can be done quite easily using the python-telegram-bot library. Whenever the location did update it can be found in update.edited_message. This only works if the user is manually sharing live location with the bot of course.

def location(bot, update):
    message = None
    if update.edited_message:
        message = update.edited_message
    else:
        message = update.message
    current_pos = (message.location.latitude, message.location.longitude)

location_handler = MessageHandler(Filters.location, location)
dispatcher.add_handler(location_handler)



回答2:


Your updates should look like this.

It will only contain first location in .message.location, the latest location is .edit_message.location, and others will be gone, so you need to record yourself.



来源:https://stackoverflow.com/questions/48238445/how-can-i-get-live-location-in-telegram-bot-via-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!