几页内联按钮

时间:2018-04-12 20:06:28

标签: python telegram-bot

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

在内联键盘调用后,我放置了处理单击每个按钮的处理程序。除前进和后退按钮外,第一页按钮的工作正常。页面不会改变。我正在使用telebot lib。

     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)

1 个答案:

答案 0 :(得分:0)

点击=>按钮后,您只是创建了一个新键盘,但您没有使用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)