Hey everyone! I’m stuck with a problem in my Telegram bot. It’s throwing an error when I try to delete a message. The error says something about a bad request and that the message can’t be deleted.
I’m using Python with the telebot library. Here’s a simplified version of my code:
from telebot import TeleBot
my_bot = TeleBot('your_token_here')
@my_bot.message_handler(content_types=['text'])
def echo_and_remove(msg):
my_bot.reply_to(msg, 'Got your message!')
my_bot.delete_message(msg.chat.id, msg.message_id)
if __name__ == '__main__':
my_bot.infinity_polling()
Any ideas why this isn’t working? Am I missing something obvious? Thanks for any help!
I’ve run into this issue before, and it can be frustrating. One thing to consider is the timing of your delete operation. Telegram has rate limits on how quickly you can perform actions, especially deletions. Try adding a small delay before deleting:
import time
@my_bot.message_handler(content_types=['text'])
def echo_and_remove(msg):
reply = my_bot.reply_to(msg, 'Got your message!')
time.sleep(1) # Wait for 1 second
my_bot.delete_message(chat_id=reply.chat.id, message_id=reply.message_id)
This gives Telegram’s servers a moment to process the reply before attempting to delete it. Also, make sure you’re using the latest version of the telebot library, as older versions might have bugs that interfere with message deletion. If all else fails, you might want to look into using the python-telegram-bot library instead, which I’ve found to be more reliable for complex operations.
hey tom, sounds like a permission issue. make sure ur bot is an admin in the chat with delete msgs rights. also, check if the msg is too old (48+ hrs) cuz telegram won’t let u delete those. hope this helps!
I encountered a similar issue with my Telegram bot. The problem might be that you’re trying to delete the user’s message, not your bot’s reply. Telegram bots can only delete their own messages or messages in channels/groups where they have admin rights.
Try modifying your code to delete the bot’s reply instead:
@my_bot.message_handler(content_types=['text'])
def echo_and_remove(msg):
reply = my_bot.reply_to(msg, 'Got your message!')
my_bot.delete_message(chat_id=reply.chat.id, message_id=reply.message_id)
This should work unless there are permission issues. If it still doesn’t, double-check your bot’s permissions in the chat.