Answer the question
In order to leave comments, you need to log in
Why don't inline buttons work?
Hello! I have inline buttons that should change message states. I tried several options, nothing works, if I didn’t add something necessary for the solution, write, I’ll add it.
user class:
class User:
registry = []
def __init__(self, tg_id):
self.tg_id = tg_id
self.buisnes = []
self.registry.append(self)
def get_user(tg_id):
for user in User.registry:
if user.tg_id == tg_id:
return user
def user_is_know(tg_id):
for user in User.registry:
if user.tg_id == tg_id:
return True
return False
def get_delo_by_text(self, delo_text):
for delo in self.buisnes:
if delo.text == delo_text:
return delo
def get_delo_by_id(self, delo_id):
for delo in self.buisnes:
if delo.delo_id == delo_id:
return delo
class Delo:
STATUS_DELETE = 0
STATUS_ACTIVE = 1
STATUS_COMPLETED = 2
def __init__(self, text):
self.text = text
self.delo_id = uuid.uuid1()
self.status = self.STATUS_ACTIVE
print(self.delo_id)
elif message.text == 'Посмотреть дела':
keyboard = telebot.types.ReplyKeyboardMarkup(True)
for itm in user.buisnes:
keyboard.row(itm.text)
keyboard.row('Назад')
bot.send_message(message.chat.id, 'Выберите дело', reply_markup=keyboard)
elif message.text in[delo.text for delo in user.buisnes]:
text = {message.text}
deluga = types.InlineKeyboardMarkup(row_width=2)
deluga.add(types. InlineKeyboardButton(text='Выполнить', callback_data='completed_'))
deluga.add(types.InlineKeyboardButton(text='Удалить', callback_data='delete_'))
deluga.add(types.InlineKeyboardButton(text='Меню', callback_data='menu'))
bot.send_message(message.chat.id, text, reply_markup=deluga)
@bot.callback_query_handler(func=lambda call: True)
def callback_key(call):
user = User.get_user(message.chat.id)
if user is None:
user = User(message.chat.id)
if call.data == 'completed_':
bot.answer_callback_query(call.id, 'Задание выполнено!')
delo_id = call.data.replace('completed_', '')
delo = user.get_delo_by_id(delo_id)
user.buisnes.remove(delo)
Answer the question
In order to leave comments, you need to log in
It worked, for this it was necessary to change the keyboard like this:
delo = user.get_delo_by_text(message.text)
deluga = telebot.types.InlineKeyboardMarkup(row_width=2)
deluga.add(telebot.types. InlineKeyboardButton(text='Выполнить', callback_data=f'completed_{delo.delo_id}'))
deluga.add(telebot.types.InlineKeyboardButton(text='Удалить', callback_data=f'delete_{delo.delo_id}'))
bot.send_message(message.chat.id, message.text, reply_markup=deluga)
@bot.callback_query_handler(func=lambda call: True)
def callback_key(call):
user = User.get_user(call.message.chat.id)
if user is None:
user = User(call.message.chat.id)
if 'completed_' in call.data:
bot.answer_callback_query(call.id, 'Задание выполнено!')
delo_id = call.data.replace('completed_', '')
delo = user.get_delo_by_id(delo_id)
delo.status = delo.STATUS_COMPLETED
if 'delete_' in call.data:
bot.answer_callback_query(call.id, 'Задание удалено.')
delo_id = call.data.replace('delete_', '')
delo = user.get_delo_by_id(delo_id)
delo.status = delo.STATUS_DELETE
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question