Answer the question
In order to leave comments, you need to log in
How to fasten and pull data from the database in the bot?
There is a bot that should replace the previous message by pressing the 'product name' button and display data from the database. I understand that the database should be screwed and I also don’t really understand how exactly to do this in the case of a bot.
@bot.callback_query_handler(func = lambda call: True)
def callback(call):
if call.data == 'catalog':
kb = types.InlineKeyboardMarkup(row_width=2)
item1 = types.InlineKeyboardButton('Эквадорская роза', callback_data='Ecuador')
kb.add(item1)
bot.edit_message_text(chat_id = call.message.chat.id,message_id=call.message.message_id,
text = 'Выберите категорию товара',reply_markup = kb)
elif call.data == 'Ecuador':
kb = types.InlineKeyboardMarkup(row_width=2)
item3 = types.InlineKeyboardButton('Следуюший товар',callback_data = 'next')
item4 = types.InlineKeyboardButton('Предыдущий товар', callback_data = 'previous')
item2 = types.InlineKeyboardButton('Добавить в корзину',callback_data = 'add_cart')
item1= types.InlineKeyboardButton('Назад',callback_data='bac')
kb.add(item3,item4,item2,item1)
bot.edit_message_text(chat_id = call.message.chat.id,message_id=call.message.message_id,
text = 'dude' ,reply_markup=kb)
Answer the question
In order to leave comments, you need to log in
This is the answer to
Python+mysqlite+bot telegarm can't add to database. How to do it right?
Also, if I'm not mistaken, edit_message_text works with messages that are no more than 24 hours
Haven't tried it on my store yet, but there is a chance that if the user clicks the "next" button, the bot will simply crash
. And it is also necessary to store the position of each user, where it is located, that is :
pressed the button "Jackets"
the bot writes to the database (section of the jacket position 1)
pressed the button "next"
the bot reads the section from the database and the position, adds or subtracts 1, checks that the number is not 0 or -1 or more than the count positions, otherwise it will collapse again
In short, I somehow did it, but due to the fact that this will be a trash-correspondence as a result, I think such an implementation of the store will not work
So that the head does not burst from the heap of elif
Divide into several handlers
@bot.callback_query_handler(func=lambda call: call.data == '7')
def seven(call):
bot.send_message(call.message.chat.id,text = 'seven')
@bot.callback_query_handler(func=lambda call: call.data == '1')
def one(call):
bot.send_message(call.message.chat.id,text = 'one')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question