Python Telegram bot not functioning as expected

I created a Telegram bot using the Python-Telegram-Bot library. The bot is supposed to monitor messages in a group where it is an admin. It should delete any message that includes words from a specified list, but it isn’t working as intended. Here is the revised version of my code:

import re
from telegram.ext import Updater, MessageHandler, Filters

def message_filter(bot, update):
    if not update.message.text:
        print("No text in message")
        return

    banned_words = ['hello', 'goodbye']

    for word in banned_words:
        if re.search(word, update.message.text):
            bot.delete_message(chat_id=update.message.chat_id, message_id=update.message.message_id)


def main():
    updater = Updater(token='YOUR_BOT_TOKEN')
    dispatcher = updater.dispatcher
    dispatcher.add_handler(MessageHandler(Filters.all, message_filter))

    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

Even though the bot has admin privileges in the group, it fails to remove messages containing words from the ‘banned_words’ list, and no error messages are displayed. What might be causing this problem?

hey mate, make sure ur bot has the right permissions in the group. sometimes it’s tricky. also, try adding a small delay before deletin messages, like time.sleep(0.5). that might help. and don’t forget to use re.IGNORECASE when searching for banned words. good luck!

As someone who’s worked extensively with Telegram bots, I can offer some insights into your issue. First, make sure your bot has the ‘Delete messages’ permission in the group settings. This is crucial and often overlooked.

Another thing to consider is the case sensitivity of your word matching. Try using re.IGNORECASE flag in your re.search() function to catch all variations of the banned words.

Also, I’ve found that sometimes there’s a slight delay in message processing. You might want to add a small time.sleep() before the delete action to ensure the message is fully registered.

Lastly, don’t forget to implement proper error handling and logging. This will help you pinpoint exactly where the issue is occurring. Something like try-except blocks around your delete_message call could reveal hidden exceptions.

Remember, Telegram’s API can be a bit quirky at times. Persistence and careful debugging usually solve most issues.

I’ve encountered similar issues with Telegram bots before. One crucial aspect you might want to check is the bot’s privacy settings. Sometimes, even with admin privileges, the bot can’t read all messages due to default privacy configurations. Try disabling privacy mode through BotFather using the /setprivacy command.

Additionally, consider implementing error handling and logging in your code. This can provide valuable insights into what’s happening behind the scenes. For instance:

import logging
logging.basicConfig(level=logging.DEBUG)

try:
    bot.delete_message(chat_id=update.message.chat_id, message_id=update.message.message_id)
    logging.info(f'Deleted message: {update.message.text}')
except Exception as e:
    logging.error(f'Failed to delete message: {e}')

This approach will help you identify any exceptions or issues that might be occurring silently. Remember, thorough debugging is often key to resolving these types of problems with Telegram bots.