Python Telegram Bot:如何衡量用户的互动 time/response 时间?

Python Telegram Bot: How to measure interaction time/response time of user?

如何衡量用户与机器人交互所用的时间?

例如

机器人发送消息。

我们想测量从收到消息到响应(点击在线键盘上的一个按钮)的时间。我需要在 python.

中使用 python 电报机器人库

这不是python-telegram-bot库能给你解决的,你得自己计算。但是 Telegram API 为您提供了您需要的所有信息。

每次发送消息时,Telegram 都会在响应中向您发送时间戳和已发送消息的 ID(参见 sendMessage 方法)。您可以将其保存在数据库中。

一旦用户与消息交互(例如单击按钮),您将收到另一个 Update which should also contain timestamp, and if the user action was an interaction with your previous message, it will also contain this message's ID. For example, if the user pressed a button, the Update will contain object callback_query,其中包含包含所按按钮的消息 ID。然后您可以在数据库中搜索它并测量用户交互所花费的时间。

这是一个小功能,希望你知道如何使用它..

这是使人类可读时间的代码

def get_readable_time(seconds: int) -> str:
    count = 0
    ping_time = ""
    time_list = []
    time_suffix_list = ["s", "m", "h", "days"]

    while count < 4:
        count += 1
        if count < 3:
            remainder, result = divmod(seconds, 60)
        else:
            remainder, result = divmod(seconds, 24)
        if seconds == 0 and remainder == 0:
            break
        time_list.append(int(result))
        seconds = int(remainder)

    for x in range(len(time_list)):
        time_list[x] = str(time_list[x]) + time_suffix_list[x]
    if len(time_list) == 4:
        rohith += time_list.pop() + ", "

    time_list.reverse()
    rohith += ":".join(time_list)

    return rohith

这里是使用upward函数的代码...

start = datetime.now()
x = bot.send_message(message.chat.id, "Pong! ")
end = datetime.now()
ms = (end - start).microseconds / 1000
uptime = get_readable_time((time.time() - StartTime))
x.edit_text(f"⪼ **Ping speed** : `{ms}`\n⪼ **Uptime** : `{uptime}`")

我认为这应该对您有所帮助,希望您会喜欢我的回答并且我给了您正确的答案