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
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:
Hope it helps somebody.
When the animated emoji are sent alone, they are sent as
animation
and not for exampletext
, you need to add "animation" to the list of content types your bot is listening, below is how you could do that.