skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search