Как заставить telegram бота на telebot реагировать на сообщение после /test command?
Например используя официальную документацию я попробовал сделать это так:
from telebot.types import ReactionTypeEmoji
import telebot
api = "token"
bot = telebot.TeleBot(api)
@bot.message_handler(commands=['test'])
def handle_test_command(message):
reaction = ReactionTypeEmoji(type='emoji', emoji='?')
bot.send_reaction(message.chat.id, message.message_id, reaction)
bot.polling()
Но выходит ошибка:
Traceback (most recent call last):
File "/home/U7283837he/test.py", line 16, in <module>
bot.polling()
File "/home/U7283837he/.local/lib/python3.10/site-packages/telebot/init.py", line 1104, in polling
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "/home/U7283837he/.local/lib/python3.10/site-packages/telebot/init.py", line 1179, in __threaded_polling
raise e
File "/home/U7283837he/.local/lib/python3.10/site-packages/telebot/init.py", line 1141, in threaded_polling
self.worker_pool.raise_exceptions()
File "/home/U7283837he/.local/lib/python3.10/site-packages/telebot/util.py", line 149, in raise_exceptions
raise self.exception_info
File "/home/U7283837he/.local/lib/python3.10/site-packages/telebot/util.py", line 92, in run
task(*args, **kwargs)
File "/home/U7283837he/.local/lib/python3.10/site-packages/telebot/__init.py", line 7860, in _run_middlewares_and_handler
result = handler['function'](message)
File "/home/U7283837he/test.py", line 13, in handle_test_command
bot.send_reaction(message.chat.id, message.message_id, reaction)
AttributeError: 'TeleBot' object has no attribute 'send_reaction'
При этом если смотреть в документацию это можно сделать (pyTelegramBotAPI 4.15.4) это описанно здесь: https://pytba.readthedocs.io/en/latest/types.html#telebot.types.ReactionTypeEmoji
Можете помочь доработать мой код чтобы бот мог ставить реакцию на сообщение?
Ответы (2 шт):
Автор решения: Zaran
→ Ссылка
Я реализовал это так ...
import telebot
from random import choice
import requests
TOKEN = "TOKEN"
bot = telebot.TeleBot(TOKEN)
@bot.message_handler(content_types = ['text'])
def text(message):
chat_id = message.chat.id
message_id = message.message_id
if 'привет' in message.text.lower():
send_react(chat_id, message_id) # Обращение к функции send_react()
def send_react(chat_id, message_id):
emo = ["?", "?", "?"]
url = f'https://api.telegram.org/bot{TOKEN}/setMessageReaction'
data = {
'chat_id': chat_id,
'message_id': message_id,
'reaction': [
{
'type': 'emoji',
#'emoji': '?' # Обычный вариант с одним смайлом.
'emoji': choice(emo) # Вариант со списком из смайликов.
}
],
'is_big': False
}
response = requests.post(url, json=data)
result = response.json()
bot.polling(none_stop=True)
или 'reaction' в одну строчку ...
'reaction': [{'type': 'emoji', 'emoji': choice(emo)}],
Все реакции в документации ... https://core.telegram.org/bots/api#reactiontypeemoji
Автор решения: user749881
→ Ссылка
Вот так можно:
from telebot.types import ReactionTypeEmoji
bot.set_message_reaction(
chat_id=message.chat.id, message_id=message.id,
reaction=[ReactionTypeEmoji("❤")])