skip to Main Content

Let me quickly explain the situation. I am an administartor of a Telegram public chat (~800 members) and sometimes users are spamming animated emojis like basketball, football, casino slots and etc.

I’ve decided to write a Telegram bot using Python and Telebot library that will automaticly delete messages containing those emojis, but encountered an issue: I don’t understand how to work with them when they are sent alone.

Right now the code looks like this:

import telebot
from telebot import types

print('Starting the bot...')

# Getting the bot token
bot = telebot.TeleBot('edited')

print('Getting the bot API token...')
print('Bot started succefully!')

@bot.message_handler(commands=['ping'])
def test_message(message):
    bot.send_message(message.chat.id, 'pong')

@bot.message_handler(content_types=['text', 'sticker'])
def handle_text(message):
    print("Received message:", message.text)
    if '🎲' in message.text:
        print('Banned emoji in message: deleting message')
        bot.delete_message(message.chat.id, message.message_id)

bot.polling()

So, the ping coommand is there just for testing, the main code is under that.
At first I tried content_types=['text'] and when I started the bot I’ve noticed that it’s not deleting the message if the 🎲 emoji is sent without any text or smth like that. After that I’ve added print("Received message:", message.text) and print('Banned emoji in message: deleting message') to see what messages bot do receive and when function actually activates. I saw that bot just don’t see those emojis sent alone and thought it’s because of the content_types=['text'] and tried sending a simple emoji like 😊 and it appeared in console. I’ve added sticker to content_types and unfortunatelly it didn’t work.

How can make bot actually work with those emojis?

2

Answers


  1. Chosen as BEST ANSWER

    I actually understood how to do what I want. I checked the Telegram Bot API documentation again and did some research about those emojis. Their type is dice.

    I changed the code and now it looks like this:

    import telebot
    from telebot import types
    
    # Getting the bot token
    bot = telebot.TeleBot('edited')
    
    # Function to delete dice messages
    @bot.message_handler(content_types=['dice'])
    def handle_text(message):
        if message.dice:
            bot.delete_message(message.chat.id, message.message_id)
    
    bot.polling()
    

    Hope it helps somebody.


  2. When the animated emoji are sent alone, they are sent as animation and not for example text, you need to add "animation" to the list of content types your bot is listening, below is how you could do that.

    import telebot
    from telebot import types
    
    print('Starting the bot...')
    
    # Getting the bot token
    bot = telebot.TeleBot('edited')
    
    print('Getting the bot API token...')
    print('Bot started succefully!')
    
    @bot.message_handler(commands=['ping'])
    def test_message(message):
        bot.send_message(message.chat.id, 'pong')
    
    @bot.message_handler(content_types=['text', 'sticker', 'animation'])
    def handle_text(message):
        if message.text:
            print("Received message:", message.text)
            if '🎲' in message.text:
                print('Banned emoji in message: deleting message')
                bot.delete_message(message.chat.id, message.message_id)
        elif message.animation:
            print("Received animation:", message.animation.file_id)
            # Add the file_id of the banned animations to this list
            banned_animations = ['ANIMATION_FILE_ID_1', 'ANIMATION_FILE_ID_2']
            if message.animation.file_id in banned_animations:
                print('Banned animation in message: deleting message')
                bot.delete_message(message.chat.id, message.message_id)
    
    bot.polling()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search