X
X
xxx_xxx_xxx_xxx2021-04-07 21:30:23
Python
xxx_xxx_xxx_xxx, 2021-04-07 21:30:23

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:
606df830391ea572552917.png
606df839e5491191413622.png
606df8426ab00093549962.png

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)

Code with a query from the database (I work with SQLite):
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()

Table in the database (url in the table is random. Only to check if the link is being followed):
606df9ba7aea0003457796.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-04-07
@xxx_xxx_xxx_xxx

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 question

Ask a Question

731 491 924 answers to any question