D
D
David Park2020-05-10 16:56:50
Python
David Park, 2020-05-10 16:56:50

Telebot: Conditional statements in ReplyKeyboardMarkup?

Hello. I just recently started learning Telebot and I don't quite understand what to do with the ReplyKeyboardMarkup buttons, or rather how to check which button the user pressed. That is, let's say the user presses the album button and I need to provide him with certain information (or another keyboard with buttons), but how to check whether he pressed this button or not. Do you need a separate handler and check if the user wrote 'album'?

table = types.ReplyKeyboardMarkup()
  
  button_album = types.KeyboardButton('album')
  button_profile = types.KeyboardButton('profile')
  button_instagram = types.KeyboardButton('insta')
  button_facebook = types.KeyboardButton('facebook')
  button_youtube = types.KeyboardButton('youtube')
  
  table.row(button_album, button_profile)
  table.row(button_instagram, button_facebook, button_youtube)
  
  bot.send_message(mes.chat.id, reply_markup = table)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2020-05-10
@Xproz

Hello, First of all, I would suggest to change the code a bit and write as follows:
table = types.ReplyKeyboardMarkup()
button_album = types.KeyboardButton('album')
button_profile = types.KeyboardButton('profile')
button_instagram = types.KeyboardButton('insta ')
button_facebook = types.KeyboardButton('facebook')
button_youtube = types.KeyboardButton('youtube')
table.add(button_album, button_profile, button_instagram, button_facebook, button_youtube)
here you need to insert the text to which the keyboard will actually be attached and make another one handler that will start the bot:
@bot.message_handler(command=["start"])
def start(message):
bot.send_message (message.chat.id, " some text ", reply_markup = table)
Next, you need to add a handler. Since the bot writes a message when the button is clicked, you just need to write a handler like this:
"text"])
def lalala(message):
if message.chat.type == "private": # indicates that the message is written to this chat
if message.text == "text that will be written after pressing the button(button text) or the one that the user wrote to the bot": #and this
is what the bot will do

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question