I’m trying to delete messages with the words "Escrow Wallet" in my telegram bot. I have the code to delete a message but when I try to use it, the bot isn’t deleting the message. My code so far is:
import os
import time
import telebot
import re
my_secret = os.environ['API_KEY']
API_KEY = os.getenv('API_KEY')
bot = telebot.TeleBot(my_secret)
@bot.message_handler(commands=["auth"])
def auth(message):
time.sleep(2 * 60)
bot.send_message(message.chat.id, "💬 @log2, the funds have been deposited in the escrow wallet. You can go ahead and deliver the product(s) to the buyer. When @Audio is satisfied, he can paste /release into the chat or /contact in case of a disputenn 💡 The seller can also /refund the buyer!")
@bot.message_handler(commands=["balance"])
def balance(message):
bot.send_message(message.chat.id, "📍 ESCROW WALLETnn💬 Wait for the balance to show up here, then continue with the deal. The funds will show up after 1 blockchain confirmation.nn💰 BALANCE: 0.00775835 BTC [$1730.94]")
@bot.message_handler(func=lambda message: re.search(r'escrow wallet', message.text, re.IGNORECASE))
def delete_escrow_wallet_message(message):
bot.delete_message(message.chat.id, message.message_id)
Can someone check the code where I tried to delete any message with the words "escrow wallet". I think something is wrong there. I want to delete any message with the words escrow wallet using the bot.
2
Answers
Telebot doesn’t use all handlers at once. If your message is intercepted by one handler, the second one will not receive the message. Try moving
delete_escrow_wallet_message
upper.To delete a message that contains "escrow wallet," you can’t delete it directly inside a message handler. Instead, you should flag the message for deletion and then delete it outside in the main program loop.