This commit is contained in:
2026-06-26 22:55:08 +03:00
parent b71632fe4d
commit 4ec8127971
2 changed files with 140 additions and 63 deletions

View File

@@ -8,6 +8,7 @@ from aiogram.filters import Command, StateFilter
from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import State, StatesGroup
from aiogram.fsm.storage.memory import MemoryStorage
from aiogram.exceptions import TelegramBadRequest
from config import (
TELEGRAM_BOT_TOKEN, ADMIN_TELEGRAM_IDS,
@@ -45,6 +46,17 @@ def generate_secret(length: int = 16) -> str:
alphabet = string.ascii_letters + string.digits
return ''.join(secrets.choice(alphabet) for _ in range(length))
async def safe_edit_text(callback: CallbackQuery, text: str, reply_markup=None, parse_mode=None):
"""Edit message text, ignoring 'message is not modified' errors."""
try:
await callback.message.edit_text(text, reply_markup=reply_markup, parse_mode=parse_mode)
except TelegramBadRequest as e:
if "message is not modified" in str(e).lower():
pass
else:
raise
await callback.answer()
# ============== KEYBOARDS ==============
def main_menu_keyboard(is_admin_user: bool = False):
@@ -99,12 +111,12 @@ async def cmd_start(message: Message):
@dp.callback_query(F.data == "register")
async def start_registration(callback: CallbackQuery, state: FSMContext):
await callback.message.edit_text(
await safe_edit_text(
callback,
"Enter the desired extension number (digits only, e.g. 1001):\n"
"Or send /cancel to abort."
)
await state.set_state(RegistrationStates.waiting_for_number)
await callback.answer()
@dp.message(RegistrationStates.waiting_for_number)
async def process_number(message: Message, state: FSMContext):
@@ -121,6 +133,12 @@ async def process_number(message: Message, state: FSMContext):
await message.answer("This number is already taken. Please choose another one.")
return
# Check if number already exists in MikoPBX
miko_existing = await mikopbx.get_employee_by_number(number)
if miko_existing:
await message.answer("This number is already occupied in MikoPBX. Please choose another one.")
return
# Check if this user already has an account (one user = one number)
user_accounts = await db.get_user_sip_accounts(message.from_user.id)
if user_accounts:
@@ -163,14 +181,15 @@ async def use_generated_secret(callback: CallbackQuery, state: FSMContext):
username = data.get("username", f"User {number}")
secret = generate_secret(12)
await callback.answer()
await create_account_and_reply(callback.message, state, number, username, secret, callback.from_user.id)
@dp.callback_query(F.data == "custom_secret", StateFilter(RegistrationStates.waiting_for_custom_secret))
async def ask_for_custom_secret(callback: CallbackQuery, state: FSMContext):
await callback.message.edit_text(
await safe_edit_text(
callback,
"Please enter the password you want to use for this SIP account (min 6 characters):"
)
await callback.answer()
@dp.message(RegistrationStates.waiting_for_custom_secret)
async def process_custom_secret(message: Message, state: FSMContext):
@@ -248,7 +267,8 @@ async def show_my_accounts(callback: CallbackQuery):
accounts = await db.get_user_sip_accounts(callback.from_user.id)
if not accounts:
await callback.message.edit_text(
await safe_edit_text(
callback,
"You don't have any registered accounts yet.",
reply_markup=main_menu_keyboard(is_admin(callback.from_user.id))
)
@@ -267,11 +287,11 @@ async def show_my_accounts(callback: CallbackQuery):
InlineKeyboardButton(text="Back", callback_data="back_main")
])
await callback.message.edit_text(
await safe_edit_text(
callback,
"Your SIP accounts. Select one to view details:",
reply_markup=keyboard
)
await callback.answer()
@dp.callback_query(F.data.startswith("my_account_"))
async def show_my_account_details(callback: CallbackQuery):
@@ -295,8 +315,7 @@ async def show_my_account_details(callback: CallbackQuery):
[InlineKeyboardButton(text="Back to my accounts", callback_data="my_accounts")]
])
await callback.message.edit_text(text, reply_markup=keyboard)
await callback.answer()
await safe_edit_text(callback, text, reply_markup=keyboard)
@dp.callback_query(F.data.startswith("show_params_"))
async def show_connection_parameters(callback: CallbackQuery):
@@ -328,8 +347,7 @@ async def show_connection_parameters(callback: CallbackQuery):
[InlineKeyboardButton(text="Back", callback_data=f"my_account_{number}")]
])
await callback.message.edit_text(connection_info, reply_markup=keyboard)
await callback.answer()
await safe_edit_text(callback, connection_info, reply_markup=keyboard)
@dp.callback_query(F.data.startswith("delete_my_account_"))
async def confirm_delete_my_account(callback: CallbackQuery):
@@ -340,12 +358,12 @@ async def confirm_delete_my_account(callback: CallbackQuery):
[InlineKeyboardButton(text="Cancel", callback_data=f"my_account_{number}")]
])
await callback.message.edit_text(
await safe_edit_text(
callback,
f"Are you sure you want to delete extension {number}?\n"
"This action cannot be undone.",
reply_markup=keyboard
)
await callback.answer()
@dp.callback_query(F.data.startswith("confirm_delete_my_"))
async def delete_my_account(callback: CallbackQuery):
@@ -357,11 +375,11 @@ async def delete_my_account(callback: CallbackQuery):
# Delete from local DB
await db.delete_sip_account(number)
await callback.message.edit_text(
await safe_edit_text(
callback,
f"Extension {number} has been deleted.",
reply_markup=main_menu_keyboard(is_admin(callback.from_user.id))
)
await callback.answer()
# ============== ADMIN HANDLERS ==============
@@ -371,12 +389,12 @@ async def show_admin_menu(callback: CallbackQuery):
await callback.answer("Access denied.")
return
await callback.message.edit_text(
await safe_edit_text(
callback,
"Administrator panel\n\n"
"Select an action:",
reply_markup=admin_menu_keyboard()
)
await callback.answer()
# ============== LINK EXISTING MIKOPBX ACCOUNT ==============
@@ -386,11 +404,11 @@ async def admin_start_link_existing(callback: CallbackQuery, state: FSMContext):
await callback.answer("Access denied.")
return
await callback.message.edit_text(
await safe_edit_text(
callback,
"Enter the existing MikoPBX extension number (exactly 7 digits) that you want to link to a Telegram user:"
)
await state.set_state(AdminEditStates.waiting_for_existing_number)
await callback.answer()
@dp.message(AdminEditStates.waiting_for_existing_number)
async def admin_process_existing_number(message: Message, state: FSMContext):
@@ -479,16 +497,17 @@ async def admin_list_accounts(callback: CallbackQuery):
accounts = await db.get_all_sip_accounts()
if not accounts:
await callback.message.edit_text(
await safe_edit_text(
callback,
"No accounts registered yet.",
reply_markup=admin_menu_keyboard()
)
else:
await callback.message.edit_text(
await safe_edit_text(
callback,
"Registered accounts (click to view details):",
reply_markup=account_list_keyboard(accounts, prefix="admin_view")
)
await callback.answer()
@dp.callback_query(F.data.startswith("admin_view_"))
async def admin_view_account(callback: CallbackQuery):
@@ -528,8 +547,7 @@ async def admin_view_account(callback: CallbackQuery):
[InlineKeyboardButton(text="Back to list", callback_data="admin_list")]
])
await callback.message.edit_text(text, reply_markup=keyboard)
await callback.answer()
await safe_edit_text(callback, text, reply_markup=keyboard)
@dp.callback_query(F.data.startswith("admin_change_number_"))
async def admin_start_change_number(callback: CallbackQuery, state: FSMContext):
@@ -540,11 +558,11 @@ async def admin_start_change_number(callback: CallbackQuery, state: FSMContext):
number = callback.data.replace("admin_change_number_", "")
await state.update_data(old_number=number)
await callback.message.edit_text(
await safe_edit_text(
callback,
f"Enter new number for extension {number}:"
)
await state.set_state(AdminEditStates.waiting_for_new_number)
await callback.answer()
@dp.message(AdminEditStates.waiting_for_new_number)
async def admin_process_new_number(message: Message, state: FSMContext):
@@ -616,12 +634,12 @@ async def admin_delete_account(callback: CallbackQuery):
[InlineKeyboardButton(text="Cancel", callback_data="admin_list")]
])
await callback.message.edit_text(
await safe_edit_text(
callback,
f"Are you sure you want to delete extension {number}?\n"
f"This will remove it from both the database and MikoPBX.",
reply_markup=keyboard
)
await callback.answer()
@dp.callback_query(F.data.startswith("confirm_delete_"))
async def admin_confirm_delete(callback: CallbackQuery):
@@ -631,7 +649,7 @@ async def admin_confirm_delete(callback: CallbackQuery):
number = callback.data.replace("confirm_delete_", "")
await callback.message.edit_text("Deleting account...")
await safe_edit_text(callback, "Deleting account...")
# Delete from MikoPBX
mikopbx_result = await mikopbx.delete_extension(number)
@@ -640,16 +658,17 @@ async def admin_confirm_delete(callback: CallbackQuery):
db_success = await db.delete_sip_account(number)
if mikopbx_result.get("success") or db_success:
await callback.message.edit_text(
await safe_edit_text(
callback,
f"Extension {number} has been deleted.",
reply_markup=admin_menu_keyboard()
)
else:
await callback.message.edit_text(
await safe_edit_text(
callback,
f"Failed to delete {number}.",
reply_markup=admin_menu_keyboard()
)
await callback.answer()
@dp.callback_query(F.data.startswith("admin_change_secret_"))
async def admin_start_change_secret(callback: CallbackQuery, state: FSMContext):
@@ -660,11 +679,11 @@ async def admin_start_change_secret(callback: CallbackQuery, state: FSMContext):
number = callback.data.replace("admin_change_secret_", "")
await state.update_data(edit_number=number)
await callback.message.edit_text(
await safe_edit_text(
callback,
f"Enter new password (secret) for extension {number}:"
)
await state.set_state(AdminEditStates.waiting_for_new_secret)
await callback.answer()
@dp.message(AdminEditStates.waiting_for_new_secret)
async def admin_process_new_secret(message: Message, state: FSMContext):
@@ -709,11 +728,11 @@ async def admin_process_new_secret(message: Message, state: FSMContext):
async def back_to_main(callback: CallbackQuery):
is_admin_user = is_admin(callback.from_user.id)
keyboard = main_menu_keyboard(is_admin_user)
await callback.message.edit_text(
await safe_edit_text(
callback,
"Main menu:",
reply_markup=keyboard
)
await callback.answer()
@dp.message(Command("cancel"))
async def cancel_handler(message: Message, state: FSMContext):