Reputation: 53
I'm new in python and telegram bot. I would like to loop the data from MySQL database and print it as buttons. "Restaurant","Hotel","Flight" are getting from MySQL database. Now, I would like to print these three data as KeyboardButtons to replace "Button 1", "Button 2", "Button 3". Is it possible to get data from MySQL database and loop it as KeyboardButtons ? Thanks in advance!
mydb = mysql.connector.connect(
host='localhost',
user='root',
passwd='',
database='my_telegram_bot')
sql = mydb.cursor()
def startCommand(update: Update, context: CallbackContext):
sql.execute("select name from types")
sql_result = sql.fetchall()
for x in sql_result:
context.bot.send_message(chat_id=update.effective_chat.id, text=x)
buttons = [[KeyboardButton(button1)], [KeyboardButton(button2)], [KeyboardButton(button3)]]
context.bot.send_message(chat_id=update.effective_chat.id, text="What kind of places are you looking for?", reply_markup=ReplyKeyboardMarkup(buttons))
My result👆
My expected result👇
Upvotes: 0
Views: 2775
Reputation: 101
You can make use of the markup.add() function:
markup = types.ReplyKeyboardMarkup()
for x in sql_result:
markup.add(types.ReplyKeyboardButton(x[0]))
context.bot.send_message(chat_id=update.effective_chat.id, text="What kind of places are you looking for?", reply_markup=markup)
Upvotes: 1