简体   繁体   中英

Python-telegram-bot detect start and end of live location

I'm able to detect the current location of a user if they share their live location. In debug mode I inspected the generated message and indeed the GPS coordinates are there. However, there is no information in the message that tells me whether the live location has started or ended.

When I start the live location, it just updates the position. What's weirder is that when I end the live location, the program takes this as just any other location update. So ending the live location cannot be distinguished from starting the live location or sending a single current (non live) location.

My question to you is: how to detect the start and end of a live location?

This is my code so far:

def location(update: Update, context: CallbackContext):
    user = update.effective_user
    message = None

    global prev_message

    if update.edited_message:
        message = update.edited_message
    else:
        message = update.message

    current_pos = (message.location.latitude, message.location.longitude)

def main() -> None:
    updater = Updater(API_KEY)
    dispatcher = updater.dispatcher

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

    updater.start_polling()
    updater.idle()


if __name__ == '__main__':
    main()

EDIT: I found a slight difference in the message properties of each kind of handled location related callback:

Has "edit_date" Has "live_period"
Single (non-live) location update - -
Start of live location - V
End of live location V -
Live location update V V

Using this information I can exactly determine what kind of location callback I'm dealing with:

def location(update: Update, context: CallbackContext):
    user = update.effective_user
    msg_type = 0

    if update.edited_message:
        message = update.edited_message
    else:
        message = update.message

    if message["edit_date"] is not None:
        msg_type += 1
    if message["location"]["live_period"] is not None:
        msg_type += 1 << 1

    if msg_type == 0:
        context.bot.send_message(user.id, "Single (non-live) location update.")
    elif msg_type == 1:
        context.bot.send_message(user.id, "End of live period.")
    elif msg_type == 2:
        context.bot.send_message(user.id, "Start of live period")
    elif msg_type == 3:
        context.bot.send_message(user.id, "Live location update.")

The problem with this method is that it only works on callbacks. When you end the live period manually, it works. However, when the live period ends on its own, there is no callback. That means that this method does not detect automatic live period endings.

I solved it by creating a timed event. When the live location has started I plan an event to run after message["location"]["live_period"] seconds:

threading.Timer(interval=message["location"]["live_period"], function=end_live_period,
                                                            args=(this_user, context))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM