Oct 17 11:16:16 vps1707806210 systemd[1]: Started My SMS Bot Service.
Oct 17 11:16:19 vps1707806210 python[16573]: Traceback (most recent call last):
Oct 17 11:16:19 vps1707806210 python[16573]:   File "/root/sms/main.py", line 8, in <module>
Oct 17 11:16:19 vps1707806210 python[16573]:     from bot.handlers import register_handlers
Oct 17 11:16:19 vps1707806210 python[16573]:   File "/root/sms/bot/handlers/__init__.py", line 9, in <module>
Oct 17 11:16:19 vps1707806210 python[16573]:     from .activation import register_activation_handlers
Oct 17 11:16:19 vps1707806210 python[16573]:   File "/root/sms/bot/handlers/activation.py", line 19, in <module>
Oct 17 11:16:19 vps1707806210 python[16573]:     async def current_activations_handler(callback_query: types.CallbackQuery, state: FSMContext):
Oct 17 11:16:19 vps1707806210 python[16573]: NameError: name 'FSMContext' is not defined
Oct 17 11:16:20 vps1707806210 systemd[1]: my_smsbot.service: Main process exited, code=exited, status=1/FAILURE
Oct 17 11:16:20 vps1707806210 systemd[1]: my_smsbot.service: Failed with result 'exit-code'.
Oct 17 11:16:20 vps1707806210 systemd[1]: my_smsbot.service: Consumed 4.141s CPU time.
Oct 17 11:16:20 vps1707806210 systemd[1]: my_smsbot.service: Scheduled restart job, restart counter is at 37.
Oct 17 11:16:20 vps1707806210 systemd[1]: Stopped My SMS Bot Service.
Oct 17 11:16:20 vps1707806210 systemd[1]: my_smsbot.service: Consumed 4.141s CPU time.
Oct 17 11:16:20 vps1707806210 systemd[1]: Started My SMS Bot Service.









# bot/handlers/activation.py

import logging
from aiogram import Dispatcher, types, F
from aiogram.utils.keyboard import InlineKeyboardBuilder

from ..data import (
    get_user_api_key,
    get_activations_by_user,
    get_activation,
    update_activation_status,
    delete_activation
)
from ..api import SmsHubAPI
from ..utils import safe_edit_or_send_message

logger = logging.getLogger(__name__)

async def current_activations_handler(callback_query: types.CallbackQuery, state: FSMContext):
    user_id = callback_query.from_user.id
    await callback_query.answer()

    activations = await get_activations_by_user(user_id)
    if not activations:
        keyboard = InlineKeyboardBuilder()
        keyboard.button(text="🔙 Назад", callback_data="back_to_main_menu")
        keyboard.adjust(1)
        await safe_edit_or_send_message(
            message=callback_query.message,
            new_text="У вас нет активных активаций.",
            state=state,
            reply_markup=keyboard.as_markup()
        )
        return

    keyboard = InlineKeyboardBuilder()
    for activation_id, details in activations.items():
        service = details.get("service", "Неизвестно")
        number = details.get("number", "Неизвестно")
        status = details.get("status", "Неизвестно")
        keyboard.button(
            text=f"{service} ({number}) - {status}",
            callback_data=f"manage_activation:{activation_id}"
        )
    keyboard.row()
    keyboard.button(text="🔙 Назад", callback_data="back_to_main_menu")
    keyboard.adjust(1)

    await safe_edit_or_send_message(
        message=callback_query.message,
        new_text="Ваши текущие активации:",
        state=state,
        reply_markup=keyboard.as_markup()
    )

async def manage_activation_handler(callback_query: types.CallbackQuery, state: FSMContext):
    activation_id = callback_query.data.split(":")[1]
    user_id = callback_query.from_user.id
    await callback_query.answer()

    activation = await get_activation(activation_id)
    if not activation or activation.get("user_id") != user_id:
        await safe_edit_or_send_message(
            message=callback_query.message,
            new_text="Активация не найдена.",
            state=state
        )
        return

    number = activation.get("number", "Неизвестно")
    service = activation.get("service", "Неизвестно")
    status = activation.get("status", "Неизвестно")

    keyboard = InlineKeyboardBuilder()
    if status == "Ожидание SMS":
        keyboard.button(text="❌ Отменить активацию", callback_data=f"cancel_activation:{activation_id}")
    elif status.startswith("Код получен"):
        keyboard.button(text="✅ Завершить активацию", callback_data=f"complete_activation:{activation_id}")
    keyboard.button(text="🔙 Назад", callback_data="current_activations")
    keyboard.adjust(1)

    message_text = (
        f"*Активация:* `{activation_id}`\n"
        f"*Сервис:* `{service}`\n"
        f"*Номер:* `{number}`\n"
        f"*Статус:* `{status}`"
    )

    await safe_edit_or_send_message(
        message=callback_query.message,
        new_text=message_text,
        state=state,
        reply_markup=keyboard.as_markup(),
        parse_mode="Markdown"
    )

async def cancel_activation_handler(callback_query: types.CallbackQuery, state: FSMContext):
    activation_id = callback_query.data.split(":")[1]
    user_id = callback_query.from_user.id
    await callback_query.answer()

    api_key = await get_user_api_key(user_id)
    if not api_key:
        await safe_edit_or_send_message(
            message=callback_query.message,
            new_text="API ключ не настроен.",
            state=state
        )
        return

    smshub_api = SmsHubAPI(api_key)
    response = await smshub_api.cancel_activation(activation_id)

    if response and response.startswith("ACCESS_CANCEL"):
        await update_activation_status(activation_id, "Активация отменена")
        keyboard = InlineKeyboardBuilder()
        keyboard.button(text="🔙 Назад", callback_data="current_activations")
        keyboard.adjust(1)
        await safe_edit_or_send_message(
            message=callback_query.message,
            new_text="Активация успешно отменена.",
            state=state,
            reply_markup=keyboard.as_markup()
        )
    else:
        await safe_edit_or_send_message(
            message=callback_query.message,
            new_text="Не удалось отменить активацию.",
            state=state
        )

async def complete_activation_handler(callback_query: types.CallbackQuery, state: FSMContext):
    activation_id = callback_query.data.split(":")[1]
    user_id = callback_query.from_user.id
    await callback_query.answer()

    api_key = await get_user_api_key(user_id)
    if not api_key:
        await safe_edit_or_send_message(
            message=callback_query.message,
            new_text="API ключ не настроен.",
            state=state
        )
        return

    smshub_api = SmsHubAPI(api_key)
    response = await smshub_api.set_status(activation_id, "6")  # 6: Завершено

    if response and response.startswith("ACCESS_ACTIVATION"):
        await update_activation_status(activation_id, "Завершено")
        keyboard = InlineKeyboardBuilder()
        keyboard.button(text="🔙 Назад", callback_data="current_activations")
        keyboard.adjust(1)
        await safe_edit_or_send_message(
            message=callback_query.message,
            new_text="Активация успешно завершена.",
            state=state,
            reply_markup=keyboard.as_markup()
        )
    else:
        await safe_edit_or_send_message(
            message=callback_query.message,
            new_text="Не удалось завершить активацию.",
            state=state
        )

def register_activation_handlers(dp: Dispatcher):
    dp.callback_query.register(current_activations_handler, F.data == "current_activations")
    dp.callback_query.register(manage_activation_handler, F.data.startswith("manage_activation:"))
    dp.callback_query.register(cancel_activation_handler, F.data.startswith("cancel_activation:"))
    dp.callback_query.register(complete_activation_handler, F.data.startswith("complete_activation:"))