Answer the question
In order to leave comments, you need to log in
I am developing a telegram bot with telebot in python. How to make a normal data output from the database with a button?
Good day. I am developing a telegram bot using telebot in python. It is necessary to make an output of the data from a DB with the button. But I get it like this:
You need to make sure that there is only one button and with a certain url from the database.
The code:
# ВИВІД ТОВАРІВ
@bot.callback_query_handler(func=lambda call: True)
def Tovars(call):
kb = Keyboard()
q = DB()
cnn = q.create_connection(echo_config.path)
tovars=q.execute_tovars(cnn)
tovarKeyboard=types.InlineKeyboardMarkup()
for tovar in tovars:
photo = open(tovar[3], 'rb')
caption = f"Назва: {tovar[1]}.\nОпис: {tovar[2]}.\nЦіна: {tovar[6]} грн."
tovarKeyboard.add(types.InlineKeyboardButton(text="Перейти до товару на сайті", url=tovar[4]))
bot.send_photo(call.message.chat.id, photo, caption, reply_markup=tovarKeyboard)
def execute_tovars(self, connection):
cursor=connection.cursor()
query="SELECT * FROM Tovar"
try:
cursor.execute(query)
result = cursor.fetchall()
return result
except Error as e:
print(f"The error '{e}' occured ")
cursor.close()
Answer the question
In order to leave comments, you need to log in
Then the keyboard must be re-created each time, and not add buttons to the existing one.
for tovar in tovars:
photo = open(tovar[3], 'rb')
caption = f"Назва: {tovar[1]}.\nОпис: {tovar[2]}.\nЦіна: {tovar[6]} грн."
tovarKeyboard=types.InlineKeyboardMarkup()
tovarKeyboard.add(types.InlineKeyboardButton(text="Перейти до товару на сайті", url=tovar[4]))
bot.send_photo(call.message.chat.id, photo, caption, reply_markup=tovarKeyboard)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question