0

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))

enter image description here

My result👆

My expected result👇

enter image description here

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

7 Comments

sql = mydb.cursor() def startCommand(update: Update, context: CallbackContext): sql.execute("select name from types") sql_result = sql.fetchall() markup = types.ReplyKeyboardMarkup() for x in sql_result: markup.add(types.ReplyKeyboardMarkup(x)) context.bot.send_message(chat_id=update.effective_chat.id, text="What kind of places are you looking for?", reply_markup=markup)
No error handlers are registered, logging exception. Traceback (most recent call last): File "C:\Users\Admin\mambaforge\lib\site-packages\telegram\ext\dispatcher.py", line 555, in process_update handler.handle_update(update, self, check, context)
File "C:\Users\Admin\mambaforge\lib\site-packages\telegram\ext\handler.py", line 198, in handle_update return self.callback(update, context) File "C:\xampp\htdocs\telegram_bot_python.py", line 179, in startCommand markup.add(types.ReplyKeyboardMarkup(x)) File "C:\Users\Admin\mambaforge\lib\site-packages\telebot\types.py", line 966, in add button_array.append(button.to_dict()) AttributeError: 'ReplyKeyboardMarkup' object has no attribute 'to_dict'
What type of data structure is sql_result? Looking at it again, it seems to be a nested list. You can try: for x in sql_result: context.bot.send_message(chat_id=update.effective_chat.id, text=x[0])
Alternatively, you can flatten the nested list and use the same loop.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.