繁体   English   中英

使用 InlineKeyboard 在电报机器人中的上一页和下一页

[英]Previous and next page in telegram bot with using InlineKeyboard

如何简化 bot 中页面的实现? 我在@bot.callback_query_handler中有三个块 - "fire""right_arrow_fire""left_arrow_fire" ,其中一个显然是多余的。 我在这个网站上研究了类似的问题,我意识到我可以使用bot.edit_message_reply_markup ,但我不知道该怎么做..

而且,如何使不出现文本“???” (在bot.edit_message_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)

来自 bot.edit_message_reply_markup 的bot.edit_message_reply_markup文档,

       """
        Use this method to edit only the reply markup of messages.
        :param chat_id:
        :param message_id:
        :param inline_message_id:
        :param reply_markup:
        :return:
       """

在您的情况下,您可以通过以下方式实现这一点,

bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id, reply_markup = markup)

代替,

bot.send_message(call.message.chat.id, 'Select spell', reply_markup=markup)

只有 InlineKeyboardButtons 将被编辑/更新,文本保持原样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM