0

I create a telegram bot whit python-telegram-bot. I have defined a list of words for the bot,and I want to manage the chat bot in the group, that is, if there is a word in the chats in the list defined, the bot will delete it. I added the bot to a group and admin it there, and the bot should control the messages sent to the group, and if there is a word in the message that is on the mlist, the bot should delete the message. my codes:

from telegram.ext import Updater
updater = Updater(token='TOKEN')
dispatcher = updater.dispatcher
mlist=['world', 'by', 'hello']

def delete_method(bot, update):
    bot.delete_message(chat_id=message.chat_id, message_id=message.message_id, *args, **kwargs)

if message in mlist:
    delete_method(bot, update)

updater.start_polling()

# for exit
# updater.idle()
Sajjad
  • 41
  • 1
  • 4
  • 13
  • 1
    What exactly is not working? The bot not deleting the messages? – laolux Aug 10 '17 at 11:24
  • "please tell me what is the problem" Why don't *you* tell us what the problem is? – Jean-François Corbett Aug 10 '17 at 11:34
  • Hi, welcome to SO. Please narrow down your question to a specific call that's giving you a problem. Your question, as it currently is will probably not receive a good answer, and may be closed. Please [take the tour](https://stackoverflow.com/tour) and read [how to ask](https://stackoverflow.com/help/how-to-ask) and [how to create a minimal, verifiable example](https://stackoverflow.com/help/mcve) for better results using this site. Good luck! – kenny_k Aug 10 '17 at 11:50
  • The robot is added to the group and it should be removed when the words in the mlist are posted in the group and by users;But the bot does not do it in the admin group! – Sajjad Aug 10 '17 at 12:20
  • I edited the question – Sajjad Aug 10 '17 at 12:48

1 Answers1

0

You hadn't registered any handler so nothing is handled.

You need to register handlers and the updates will be handled by that one when it is true.

Check this introduction. It is very clear.

EDIT: i write you an example of how your code should be (i didn't tested it, i wrote from my phone)

from telegram.ext import Updater, MessageHandler
import re

def delete_method(bot, update):
    if not update.message.text:
        print("it does not contain text")
        return

    mlist=['world', 'by', 'hello']

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

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

    updater.start_polling()

    updater.idle()

if __name__ == '__main__':
    main()
91DarioDev
  • 1,612
  • 1
  • 14
  • 31