Не работает callback кнопки Telebot

Рейтинг: 0Ответов: 0Опубликовано: 14.08.2023

Почему то не работают кнопки из

elif action == "reject_reason"

Бот пропускает код написанный для этих кнопок и почему-то переходит сам в

else:
    bot.send_message(call.from_user.id, "Извините, произошла ошибка при обработке заявки.")

В базе данных у меня все записывается как надо, благодарен любой помощи.

def handle_approve_reject(call):
    """Обработчик кнопок Одобрить/Отказать/Причина отказа."""
    db_connection = sqlite3.connect('applications.db')
    db_cursor = db_connection.cursor()
    action, *parts = call.data.split("_")
    application_id = parts[0]
    
    if len(parts) > 1:
        reason = "_".join(parts[1:])
    else:
        reason = None

    application_id = application_id.strip()

    # Get application data from the database
    db_cursor.execute("SELECT * FROM applications WHERE id=?", (application_id,))
    application_data = db_cursor.fetchone()

    if application_data:
        context = {
            'user_chat_id': application_data[2],
            'nickname': application_data[3],
            'current_position': application_data[4],
            'desired_position': application_data[5],
            'status': application_data[8],  
            
        }

        if action == "approve":
            new_status = f"Одобрено by: @{call.from_user.username}"
            # Отправляем пользователю сообщение о одобрении заявки
            user_chat_id = context['user_chat_id']
            bot.send_message(user_chat_id, f"🎉🎊🍾 Ваша заявка на повышение на должность '{context['desired_position']}' одобрена by: @{call.from_user.username}")
            new_message_text = f"Статус: {new_status}\n\n"\
                               f"От: {context['nickname']}\n" \
                               f"Текущая должность: {context['current_position']}\n" \
                               f"Желаемая должность: {context['desired_position']}\n"

            bot.edit_message_text(chat_id=management_chat_id, message_id=call.message.message_id,
                                  text=new_message_text)
            # Update status in the database
            db_cursor.execute("UPDATE applications SET status=? WHERE id=?", (new_status, application_id))
            db_connection.commit()

        elif action == "reject":
            inline_keyboard = InlineKeyboardMarkup(row_width=1)
            inline_keyboard.add(InlineKeyboardButton("Reason 1", callback_data=f"reject_reason_{application_id}_1"),
                            InlineKeyboardButton("Reason 2", callback_data=f"reject_reason_{application_id}_2"))
            bot.edit_message_text(chat_id=management_chat_id, message_id=call.message.message_id,
                                  text="Выберите причину отказа:", reply_markup=inline_keyboard)

        elif action=="reject_reason_":
            application_id, reason_num = action.split("_")[1:]
            
            if reason_num == "1":
                reason = "Reason 1" 
            elif reason_num == "2":
                reason = "Reason 2"
            if reason:
                new_status = f"Отказано by: @{call.from_user.username}, Причина: {reason}"
                
                user_chat_id = context['user_chat_id']
                bot.send_message(user_chat_id, f"Ваша заявка на повышение на должность '{context['desired_position']}' отклонена by: @{call.from_user.username}. Причина: {reason}")

                # Update message in management chat
                new_message_text = f"Статус: {new_status}\n\n" \
                                f"От: {context['nickname']}\n" \
                               f"Текущая должность: {context['current_position']}\n" \
                               f"Желаемая должность: {context['desired_position']}\n"

                bot.edit_message_text(chat_id=management_chat_id, message_id=call.message.message_id, text=new_message_text)

                db_cursor.execute("UPDATE applications SET status=?, reason=? WHERE id=?", (new_status, reason, application_id))
                db_connection.commit()

Ответы

Ответов пока нет.