Reputation:
I am writing a bot in Python. What is the task: I need to get the time of the sent message. The bot user enters a message and sends it, and I need to get the time when the user wrote it and add this time to the database (mySQL). How to implement this? I'm using PyTelegramBotApi. Сan the bot reply to the sent message by the time when it was sent?
Upvotes: 3
Views: 10500
Reputation: 3067
When Telegram sends a message to your bot, "Message" object inside has a special field named "date", which has UNIX timestamp when the message was sent.
Here's a simple handler which replies to the message with its timestamp:
@bot.message_handler()
def getting_message_time(message):
bot.reply_to(message, message.date)
If you want to extract some values from that timestamp, like hour, minute or month number, please consider using Python's built-in datetime module. Good luck!
Upvotes: 4