'Live location in pyTelegramBotAPI

I managed to get the users current location by creating a request that adds a key "share location" to the keyboard. However, I don't want to keep pressing a button each time my location should be updated. Therefore I want to be able to track the users live location. But I can't seem to achieve it.

When I share my live location in the telegram chat I'm able to detect this in my program and fetch the current location:

@bot.message_handler(content_types=['location'])
def handle_location(message):
    location = [message.location.latitude, message.location.longitude]

However, the program does this only once. I want to be able to update the location with a function without having to ask the user to share their location again. I can't find anything about this in the documentation.

If anyone has proof that it can't be done I'd be glad to hear it. Otherwise, I would like some help.



Solution 1:[1]

If the user uses the built-in "Share My Live Location for ..." then the updates can be "caught" by the bot using telethon, and this snippet of code:

@bot.on(events.MessageEdited)
async def handler(event):
    if event.geo:
        logger.info('Yes, an updated geo object')

        user = await bot.get_entity(event.message.peer_id) # could be expensive

        logger.debug(f'event: {event}')
        msg = (
            #f'{utils.get_display_name(user)}'
            #f' | {event.edit_date} | '
            f'{event.media.geo.long}'
            f' | {event.media.geo.lat}'
            f' | accuracy radius: {event.media.geo.accuracy_radius}'
            f' | heading: {event.media.heading}'
            f' | period: {event.media.period}'
            )
        
        logger.warning(msg)
        await event.respond(msg)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 johnedstone