几页内联按钮



我有一个关于电报中的内联按钮的问题。所以我有很多内联按钮,我想将它们分成几页。在每一页上,我都有前进=>和后退<=按钮在页面之间移动。单击每个按钮返回文档。

内联键盘调用后,我放置了处理单击每个按钮的处理程序。首页按钮工作正常,前进和后退按钮除外。页面不会更改。我正在使用远程机器人库。

     bot = telebot.TeleBot(token)
    @bot.message_handler(commands=['start','help'])
    def start(o):
          bot.send_message(o.chat.id,'Hi,use buttons!')
    def name(m):
            keyboard = types.InlineKeyboardMarkup()
            keyboard.add(*[types.InlineKeyboardButton(text=name,callback_data=name) for name
                        in ['button1','button2']])
            keyboard.add(*[types.InlineKeyboardButton(text=name,callback_data=name) for name
                        in ['button3','button4']])
                  keyboard.add(*[types.InlineKeyboardButton(text=name,callback_data=name) for name
                        in ['=>']])
            msg = bot.send_message(m.chat.id,'-----------Choose button----------',reply_markup=keyboard) 
    @bot.callback_query_handler(func=lambda c:True)
    def inline(c):
        if c.data == 'button1':
            bot.send_document(c.message.chat.id,open('button1.pdf', 'rb'))
        elif c.data == 'button2':
            bot.send_document(c.message.chat.id,open('button2.pdf', 'rb'))
        elif c.data == 'button3':
            bot.send_document(c.message.chat.id,open('button3.pdf', 'rb'))
        elif c.data == 'button4' :
            bot.send_document(c.message.chat.id,open('button4.pdf', 'rb'))
        elif c.data == '=>' : 
            keyboard = types.InlineKeyboardMarkup()
            keyboard.add(*[types.InlineKeyboardButton(text=name,callback_data=name) for name
                        in ['button5','button6']])
while True: 
    try:
        bot.polling(none_stop=True)
    except Exception as e:
        print(e)
        time.sleep(15)

单击=>按钮时,您只是在创建一个新键盘,但您不会发布带有 edit_message_reply_markup 的新键盘。

keyboard = types.InlineKeyboardMarkup()
keyboard.add(*[types.InlineKeyboardButton(text=name,callback_data=name) for name
                    in ['button5','button6']])
bot.edit_message_reply_markup(chat_id=c.message.chat.id, message_id=c.message.message_id, reply_markup=keyboard)

最新更新