0

I built a telegram bot with Python-Telegram-Bot.I added the bot to a group and got the bot in the admin group.I have defined a list(mlist) for the bot and put it in a list of words.The bot should check the messages the users send to the group.And if users send a message to the group in which the words defined in the list(mlist) are there, the bot must delete it(delete message).

# -*- coding: utf-8 -*-
import os, sys
from telegram.ext import Updater, MessageHandler, Fliters
import re


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

    mlist=['سلام', 'شادي']


   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()
# for exit
# updater.idle()

(The bot should delete the messages that are sent to the group and contain the list(mlist) words) ;But the bot does not work, and does not give error.

Sajjad
  • 41
  • 1
  • 4
  • 13

1 Answers1

1

Try to replace the words in mlist with english ones and see if it works then. Just to check if that's causing the problem.

EDIT: So it works with english words. The reason is, that Telegram API only supports UTF-8, but Python works with Unicode. Unicode ≠ UTF-8. You have to encode your text with UTF-8. Take a string and add:

.encode('utf-8')

Endogen
  • 589
  • 2
  • 12
  • 24
  • @EndogenWorks with English words – Sajjad Aug 17 '17 at 10:55
  • Here is what happens: Telegram API only supports UTF-8, but Python works with Unicode. Unicode is not the same as UTF-8. Please see [this post](https://github.com/python-telegram-bot/python-telegram-bot/issues/27) from python-telegram-bot library. – Endogen Aug 18 '17 at 19:08
  • @EndogenWhich line of code do I use for this command? .encode('utf-8') – Sajjad Aug 24 '17 at 11:35
  • You need to add that to a string. So you have a string `my_string = 'سلام'` and then you do `my_string.encode('utf-8')`. Then you can add that string to the list: `mlist.append(my_string)` – Endogen Aug 24 '17 at 19:50
  • If that helped you, it would be nice to upvote my post :) – Endogen Aug 25 '17 at 19:26