Предыдущая и следующая страницы в боте Telegram, с использованием InlineKeyboard
Как упростить реализацию страниц в боте? У меня есть три блока - «fire», «right_arrow_fire», «left_arrow_fire» в @bot.callback_query_handler, и один из них явно лишний. Я изучал подобные вопросы на этом сайте и понял, что я могу использовать bot.edit_message_reply_markup, но я не могу понять, как это сделать ..
И как сделать так, чтобы вообще не появлялся текст "???" (в строке bot.edit_message_text) каждый раз, когда нажимается кнопка страницы. Параметр text= там обязателен, но в данном случае он совсем не нужен. Может есть уловка позволяющая его не выводить/игнорировать.
Спасибо вам. Полный код:
# -*- coding: utf-8 -*-
import telebot
from telebot import types
from telegram import (ReplyKeyboardMarkup, ReplyKeyboardRemove)
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton, Message
bot = telebot.TeleBot('********************************');
@bot.message_handler(commands=['start'])
def welcome(message):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
item1 = types.KeyboardButton("Spell")
markup.add(item1)
bot.send_message(message.chat.id, "Hi", parse_mode='html', reply_markup=markup)
@bot.message_handler(content_types=['text'])
def value_search(message):
if message.text == "Spell":
markup = types.InlineKeyboardMarkup(row_width=2)
item1 = types.InlineKeyboardButton("Fire", callback_data='fire')
markup.add(item1)
bot.send_message(message.chat.id, 'Select element', reply_markup=markup)
else:
bot.send_message(message.chat.id, "Error")
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
try:
if call.message:
if call.data == 'fire':
markup = types.InlineKeyboardMarkup(row_width=2)
item1 = types.InlineKeyboardButton("Fireball", callback_data='fireball')
item2 = types.InlineKeyboardButton("Fire Wall", callback_data='firewall')
item3 = types.InlineKeyboardButton("Fire Arrow", callback_data='firearrow')
item4 = types.InlineKeyboardButton("Fire Explosion", callback_data='fireexplosion')
item5 = types.InlineKeyboardButton("Next Page --->", callback_data='right_arrow_fire')
markup.add(item1, item2, item3, item4, item5)
bot.send_message(call.message.chat.id, 'Select spell', reply_markup=markup)
elif call.data == 'right_arrow_fire':
markup = types.InlineKeyboardMarkup(row_width=2)
item6 = types.InlineKeyboardButton("Fire Circle", callback_data='firecircle')
item7 = types.InlineKeyboardButton("Fire Elemental", callback_data='fireelemental')
item8 = types.InlineKeyboardButton("Fire Power", callback_data='firepower')
item9 = types.InlineKeyboardButton("Fire Storm", callback_data='firestorm')
item10 = types.InlineKeyboardButton("<--- Previous Page", callback_data='left_arrow_fire')
markup.add(item6, item7, item8, item9, item10)
bot.send_message(call.message.chat.id, 'Select spell', reply_markup=markup)
elif call.data == 'left_arrow_fire':
markup = types.InlineKeyboardMarkup(row_width=2)
item1 = types.InlineKeyboardButton("Fireball", callback_data='fireball')
item2 = types.InlineKeyboardButton("Fire Wall", callback_data='firewall')
item3 = types.InlineKeyboardButton("Fire Arrow", callback_data='firearrow')
item4 = types.InlineKeyboardButton("Fire Explosion", callback_data='fireexplosion')
item5 = types.InlineKeyboardButton("Next Page --->", callback_data='right_arrow_fire')
markup.add(item1, item2, item3, item4, item5)
bot.send_message(call.message.chat.id, 'Select spell', reply_markup=markup)
elif call.data == "fireball":
bot.answer_callback_query(callback_query_id=call.id, show_alert=True, text="+1 damage")
elif call.data == "firewall":
bot.answer_callback_query(callback_query_id=call.id, show_alert=True, text="+2 damage")
elif call.data == "firearrow":
bot.answer_callback_query(callback_query_id=call.id, show_alert=True, text="+3 damage")
elif call.data == "fireexplosion":
bot.answer_callback_query(callback_query_id=call.id, show_alert=True, text="+4 damage")
elif call.data == "firecircle":
bot.answer_callback_query(callback_query_id=call.id, show_alert=True, text="+5 damage")
elif call.data == "fireelemental":
bot.answer_callback_query(callback_query_id=call.id, show_alert=True, text="+6 damage")
elif call.data == "firepower":
bot.answer_callback_query(callback_query_id=call.id, show_alert=True, text="+7 damage")
elif call.data == "firestorm":
bot.answer_callback_query(callback_query_id=call.id, show_alert=True, text="+8 damage")
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='???', reply_markup=None)
except Exception as e:
print(repr(e))
bot.polling(none_stop=True)