Python电报机器人:如何测量用户的交互时间/响应时间?

发布于 2025-01-23 12:10:27 字数 120 浏览 0 评论 0原文

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

例如,

机器人发送消息。

我们想测量从消息接收到响应的时间(单击在线键盘上的放置)。我需要使用Python的Python Telegram bot库。

how can I measure the time the user uses to interact with the bot?

E.g.

The bot sends a message.

We want to measure the time from the reception of the message to a response (click on a put on the online keyboard). I need this using python telegram bot library in python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

游魂 2025-01-30 12:10:27

这不是python-telegram-bot库可以为您解决的问题,而必须自己进行计算。但是Telegram API为您提供所需的所有信息。

每次发送消息时,在响应中,Telegram在发送发送消息的时间戳和ID(请参阅 sendmessage 方法)。您可以将其保存在数据库中。

用户与消息进行交互后(例如单击按钮),您将收到另一个 update 也应该包含时间戳,如果用户操作是与您以前的消息的交互,则它还将包含此消息的ID。例如,如果用户按下按钮,则更新将包含对象 callback_query 带有包含按下按钮的消息的ID。然后,您可以在数据库中搜索此信息,并测量用户交互需要多长时间。

This is not something that the python-telegram-bot library can solve for you, you have to do the computation by yourself. But Telegram API provides you with all the information you need.

Every time you send a message, in the response Telegram sends you timestamp and ID of the sent message (see sendMessage method). You can save this in a database.

Once user interacts with the message (e.g. clicking on the button), you'll receive another 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 with the ID of the message containing the button that was pressed. You can then search for this in the database and measure how long it took until the user's interaction.

救赎№ 2025-01-30 12:10:27

这是这样做的小功能,希望您知道如何使用它。

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

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

,这是使用向上功能的代码...

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}`")

我认为这应该可以帮助您,希望您能爱我回答,我给了你正确的一个

here is small function to do it, i hope you know how to use it..

This is code for make human readable time

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

Here is the code for using the upward function...

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}`")

I think this should help you, Hope ya would love my answer and i gave you the correct one

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文