import telebot from telebot.types import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton import requests from bs4 import BeautifulSoup BOT_TOKEN = 'ваш токен' bot = telebot.TeleBot(BOT_TOKEN) def get_fact(): url = 'https://randstuff.ru/fact/' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') fact = soup.find('table', class_='text').find('td').text.strip() return fact def main_menu_keyboard(): keyboard = ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False) keyboard.add(KeyboardButton('Получить факт')) return keyboard def inline_keyboard(): keyboard = InlineKeyboardMarkup() keyboard.add(InlineKeyboardButton('Получить еще факт', callback_data='get_fact')) return keyboard @bot.message_handler(commands=['start', 'help']) def send_welcome(message): bot.reply_to(message, "Привет! Я бот, который генерирует случайные факты. Нажми на кнопку 'Получить факт', чтобы узнать что-то новое.", reply_markup=main_menu_keyboard()) @bot.message_handler(func=lambda message: message.text == 'Получить факт') @bot.message_handler(commands=['fact']) def send_fact(message): fact = get_fact() bot.reply_to(message, fact, reply_markup=inline_keyboard()) @bot.callback_query_handler(func=lambda call: call.data == 'get_fact') def callback_get_fact(call): fact = get_fact() bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=fact, reply_markup=inline_keyboard()) @bot.message_handler(func=lambda message: True) def echo_all(message): bot.reply_to(message, "Извини, я не понимаю. Используй кнопку 'Получить факт', чтобы узнать что-то новое.", reply_markup=main_menu_keyboard()) bot.polling()