2

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?

wxnzks
  • 43
  • 6

2 Answers2

1

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()
Saxtheowl
  • 4,136
  • 5
  • 23
  • 32
  • 1
    Thank you for reply! Could you please also help me finding animations IDs? I've tried to run the code but it still doesn't show me anything except for simple (but still animated) emojis in console output. It seems that bowling, basketball and other emojis of their type are somehow randomized and have several animations because there are a lot of different animations for them with different chance of receiving. – wxnzks Apr 05 '23 at 16:34
  • You can check the width and height of the received animation, and if they match the expected dimensions of the animated emojis you want to ban, you can then delete the message – Saxtheowl Apr 05 '23 at 16:45
1

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.

wxnzks
  • 43
  • 6