简体   繁体   English

Python-telegram-bot 检测实时位置的开始和结束

[英]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.在调试模式下,我检查了生成的消息,确实有 GPS 坐标。 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"有“edit_date” Has "live_period"有“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:当实时位置开始时,我计划在message["location"]["live_period"]秒后运行一个事件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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