【发布时间】:2022-01-16 19:33:31
【问题描述】:
我是 python 和电报机器人的新手。我想循环来自 MySQL 数据库的数据并将其打印为按钮。 "Restaurant","Hotel","Flight" 是从 MySQL 数据库中获取的。现在,我想将这三个数据打印为 KeyboardButtons 来替换“Button 1”、“Button 2”、“Button 3”。是否可以从 MySQL 数据库中获取数据并将其作为 KeyboardButtons 循环?提前致谢!
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))
我的结果????
我的预期结果????
【问题讨论】:
标签: python-3.x telegram telegram-bot python-telegram-bot py-telegram-bot-api