Помогите разобраться с телеграм ботом

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

Кто может помочь разобраться?

Пишу телеграм бота с telebot, бот не реагирует на кнопки. Пересмотрела кучу туториалов, пыталась в интернете найти ответ... Я пишу чисто для себя, потренироваться. С программированием сама никак вообще не связана (разве что бесплатный курс Яндекс Практикума по Python прошла), просто мне это интересно стало, вот и разбираюсь.

import telebot
from telebot import types

api_token = "***"

bot = telebot.TeleBot(api_token)

@bot.message_handler(commands=["start"])
def start(message):
    markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True, row_width = 2)
    item_glossary = types.InlineKeyboardButton(text = "Отработка глоссариев", callback_data = "glossary")
    item_books = types.InlineKeyboardButton(text = "Книги", callback_data = "books")
    markup.add(item_glossary, item_books)
    bot.send_message(message.chat.id, f"Привет, {message.from_user.first_name}! Это бот Насти @stacielingua."
                                      f" Вы можете использовать меня для отработки глоссариев <b>Seasons</b>. "
                                      f"А еще я могу дать вам ссылку на подборку детских книг по этой теме. Что вас интересует?", parse_mode="html", reply_markup=markup)

@bot.callback_query_handler(func = lambda call: True)
def answer(answer1):
    if answer1.data == "glossary":
        markup1 = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True, row_width=2)
        item_winter = types.KeyboardButton(text="WINTER", callback_data="winter")
        item_spring = types.KeyboardButton(text="SPRING", callback_data="spring")
        item_summer = types.KeyboardButton(text="SUMMER", callback_data="summer")
        item_autumn = types.KeyboardButton(text="AUTUMN", callback_data="autumn")
        markup_reply.add(item_winter, item_spring, item_summer, item_autumn)
        bot.send_message(answer1.message.chat.id, "Какой глоссарий вы хотите отработать?", reply_markup = markup1)

    elif answer1.data == "books":
        markup2 = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
        item_click_button = types.KeyboardButton(text="ТЫК", url="https://drive.google.com/drive/folders/1saUABpx3Psbrc8SsNvLX1T7suFonFts7")
        markup_reply.add(item_click_button)
        bot.send_message(answer1.message.chat.id, "Переходите по ссылке ниже и читайте с удовольствием!", reply_markup = markup2)

bot.polling(none_stop=True)

Ответы

▲ 0Принят

Для использования callback необходимо использовать другую клавиатуру. Я всё же предположу что вы хотели использовать ReplyKeyboardMarkup и подправлю код под её использование:

import telebot
from telebot import types

api_token = "***"

bot = telebot.TeleBot(api_token)


@bot.message_handler(commands=["start"])
def start(message):
    markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True, row_width=2)
    item_glossary = types.InlineKeyboardButton(text="Отработка глоссариев")
    item_books = types.InlineKeyboardButton(text="Книги")
    markup.add(item_glossary, item_books)
    bot.send_message(message.chat.id, f"Привет, {message.from_user.first_name}! Это бот Насти @stacielingua."
                                      f" Вы можете использовать меня для отработки глоссариев <b>Seasons</b>. "
                                      f"А еще я могу дать вам ссылку на подборку детских книг по этой теме. Что вас интересует?",
                     parse_mode="html", reply_markup=markup)


@bot.message_handler(content_types=['text'])
def answer(answer1):
    if answer1.text == "Отработка глоссариев":
        markup1 = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True, row_width=2)
        item_winter = types.KeyboardButton(text="WINTER")
        item_spring = types.KeyboardButton(text="SPRING")
        item_summer = types.KeyboardButton(text="SUMMER")
        item_autumn = types.KeyboardButton(text="AUTUMN")
        markup1.add(item_winter, item_spring, item_summer, item_autumn)
        bot.send_message(answer1.chat.id, "Какой глоссарий вы хотите отработать?", reply_markup=markup1)

    elif answer1.text == "Книги":
        markup2 = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
        item_click_button = types.KeyboardButton(text="ТЫК")
        markup2.add(item_click_button)
        bot.send_message(answer1.chat.id, "Переходите по ссылке ниже и читайте с удовольствием!", reply_markup=markup2)
    elif answer1.text == "ТЫК":
        bot.send_message(answer1.chat.id, "https://drive.google.com/drive/folders/1saUABpx3Psbrc8SsNvLX1T7suFonFts7")


bot.polling(none_stop=True)