A
A
Andrey Gilgenberg2021-07-31 01:30:42
Python
Andrey Gilgenberg, 2021-07-31 01:30:42

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


Case class:
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)


Create a markup and inline keyboard (it is shown):
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)


Button processing (it does not work, I wrote one button):
@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)

swears at the user, tried to add the message argument to the callback_key, but he swears at this too, I don’t understand what to do?
In general, the task is to work out the buttons, that is, if a person clicks on the "completed" button, the task is no longer displayed, I have been programming for a week, this part does not lend itself at all, I have already rewritten it 3 times and it is useless, the tasks do not disappear from the

list gives:
user = User.get_user(message.chat.id)
NameError: name 'message' is not defined
If you add message to the function after call:
TypeError: callback_key() missing 1 required positional argument: 'message'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Gilgenberg, 2021-08-02
@kkid404

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)

And the call is like this:
@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 question

Ask a Question

731 491 924 answers to any question