5

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.

James Z
  • 12,209
  • 10
  • 24
  • 44
Pavlo Zub
  • 51
  • 1
  • 1
  • 3

3 Answers3

7

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)
Lukas E.
  • 99
  • 1
  • 6
  • Just an update for 2019, the mentioned `edited_updates=True` is now unnecessary; it will now yield a `TelegramDeprecationWarning`. The [Transition Guide](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Transition-guide-to-Version-12.0#messagehandler) will essentially say, it now accepts all updates by default. – dTanMan Oct 27 '19 at 18:24
1

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.

Sean Wei
  • 7,433
  • 1
  • 19
  • 39
  • Unfortunately, it doesn't work, because `.edited_message` available only when user manually edit message. When user send Live Location, it doesn't update automatically. – Pavlo Zub Jan 14 '18 at 21:34
  • @PavloZub does that mean there's no way at all to get automatic location updates of user sharing location using api? – Andrey Stepanov Nov 14 '20 at 10:12
0

Use this simple function:

def location(update: Update, context: CallbackContext):
    current_pos = (update.message.location.latitude,update.message.location.longitude)
    print(current_pos)

dp.add_handler(MessageHandler(Filters.location, location))
KawaiKx
  • 9,558
  • 19
  • 72
  • 111