# Импорты
import telebot
from telebot import types
import os
# Определение бота
bot = telebot.TeleBot("TOKEN", parse_mode="MARKDOWN")
bot.set_webhook()
# Убирание кнопок
none = telebot.types.ReplyKeyboardRemove()
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# команда /start
@bot.message_handler(commands=['start'])
def start(message):
buttons = types.InlineKeyboardMarkup()
books = [book.replace('_', ' ') for book in os.listdir('books')]
for book in books:
buttons.add(types.InlineKeyboardButton(f"{book}", callback_data=f"-book-{book}"))
bot.send_message(message.chat.id, f"Выбери учебник", reply_markup=buttons)
# Обработчик callback
@bot.callback_query_handler(func=lambda c: True)
def callback(c):
if '-book-' in c.data:
book = c.data.split('-book-', maxsplit=1)[1]
exercises = os.listdir(f"books\{book.replace(' ', '_')}")
buttons = types.InlineKeyboardMarkup()
btns = []
for exercise in exercises:
btns.append(
types.InlineKeyboardButton(
f"{exercise.replace('.png','')}",
callback_data=fr"-send-{book.replace(' ', '_')}\{exercise}"
)
)
for i in range(0, len(btns), 3):
try: buttons.add(btns[i], btns[i+1], btns[i+2])
except:
try: buttons.add(btns[i], btns[i+1])
except:
try: buttons.add(btns[i])
except: pass
bot.send_message(c.message.chat.id, f"Выбери номер", reply_markup=buttons)
if '-send-' in c.data:
path = f"books\{c.data.split('-send-', maxsplit=1)[1]}"
bot.send_photo(c.message.chat.id, photo=open(path,'rb'))
bot.polling(none_stop=True)