Не отвечает на сообщения в тг

Рейтинг: 1Ответов: 1Опубликовано: 26.01.2023

Бот добавляет кнопки,но не отвечает при нажатии на них

import telebot
import config
from telebot import types 


bot = telebot.TeleBot(config.TOKEN)

@bot.message_handler(commands=['start'])
def start(message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    button1 = types.KeyboardButton("Дароу")
    button2 = types.KeyboardButton("Зачем ты существуешь?")
    markup.add(button1,button2)
    bot.send_message(message.chat.id, "Привет, {0.first_name}!.".format(message.from_user), reply_markup=markup)
@bot.message_handler(content_types=['text'])
def message(message):
    if message.chat.type == 'privat':
        if message.text == button1:
            bot.send_message(message.chat.id,"Салам брооууу")
        elif message.text == button2:
            bot.send_message(message.chat.id,"Что бы быть брооууу")


print('Started')

bot.polling(none_stop=True)

Ответы

▲ 0Принят

Нужно сравнивать сообщение с текстом, который находится на кнопке:

if message.text == "Дароу":
    bot.send_message(message.chat.id,"Салам брооууу")
elif message.text == "Зачем ты существуешь?":
    bot.send_message(message.chat.id,"Что бы быть брооууу")

Еще надо заменить if message.chat.type == 'privat': на if message.chat.type == 'private':, так как 'private' - корректное написание типа чата.

Возможно, проблема еще в том, что вы не указали обработчик события для кнопок. Для этого нужно использовать декоратор @bot.callback_query_handler(func=lambda call: True).

@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    if call.message:
        if call.data == "button1":
            bot.send_message(call.message.chat.id,"Салам брооууу")
        elif call.data == "button2":
            bot.send_message(call.message.chat.id,"Что бы быть брооууу")

Или вы не добавили функцию callback_data для кнопок, которая указывает на то, что должно произойти, когда пользователь нажимает на кнопку.

button1 = types.KeyboardButton("Дароу", callback_data='button1')
button2 = types.KeyboardButton("Зачем ты существуешь?", callback_data='button2')