让我快速解释一下情况。我是 Telegram 公共(public)聊天室(约 800 名成员)的管理员,有时用户会发送垃圾邮件动画表情符号,例如篮球、足球、赌场老虎机等。
我决定使用 Python 和 Telebot 库编写一个 Telegram 机器人,它会自动删除包含这些表情符号的消息,但遇到了一个问题:当它们单独发送时,我不明白如何使用它们。
现在代码如下所示:
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()
所以,ping
命令只是为了测试,主要代码在下面。
起初我尝试了 content_types=['text']
,当我启动机器人时,我注意到如果发送 🎲 表情符号时没有任何文本或类似的内容,它不会删除消息。之后,我添加了 print("Received message:", message.text)
和 print('Banned emoji in message:deleting message')
以查看机器人发送的消息确实接收以及功能实际激活时。我看到机器人看不到那些单独发送的表情符号,并认为这是因为 content_types=['text']
并尝试发送像 😊 这样的简单表情符号,它出现在控制台中。我已将 sticker
添加到 content_types
中,但不幸的是它不起作用。
如何让机器人真正使用这些表情符号?
最佳答案
当单独发送动画表情符号时,它们会作为动画
发送,而不是例如文本
,您需要将“动画”添加到您的内容类型列表中机器人正在监听,下面是您可以如何做到这一点。
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()
关于python - 如何让 Telegram 机器人与动画表情符号一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75941588/