asd
This commit is contained in:
@@ -31,15 +31,61 @@ async def handle_button_click(callback_data: str, chat_id: int, message_id: int)
|
||||
if not callback_data.startswith("service_"):
|
||||
return
|
||||
|
||||
parts = callback_data.rsplit("_", 1)
|
||||
if len(parts) != 2:
|
||||
# Parse: service_{action}_{id_or_name}
|
||||
parts = callback_data.split("_", 2)
|
||||
if len(parts) != 3:
|
||||
return
|
||||
|
||||
action = parts[0].replace("service_", "")
|
||||
try:
|
||||
service_id = int(parts[1])
|
||||
except ValueError:
|
||||
return
|
||||
action = parts[1] # start, stop, restart
|
||||
identifier = parts[2] # numeric service_id or 'name_{service_name}'
|
||||
|
||||
# Determine if identifier is service_id (numeric) or service_name (prefixed with 'name_')
|
||||
if identifier.startswith('name_'):
|
||||
# Extract service name from identifier
|
||||
service_name = identifier[5:] # Remove 'name_' prefix
|
||||
service_id = None
|
||||
|
||||
# Look up service_id from controller API
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
headers = {"Authorization": f"Bearer {SECRET_TOKEN}"}
|
||||
async with session.get(f"{CONTROLLER_API}/services", headers=headers) as resp:
|
||||
if resp.status == 200:
|
||||
services = await resp.json()
|
||||
# Find service by name
|
||||
for svc in services:
|
||||
if svc.get('name') == service_name:
|
||||
service_id = svc.get('id')
|
||||
break
|
||||
|
||||
if not service_id:
|
||||
await bot.edit_message_text(
|
||||
chat_id=chat_id,
|
||||
message_id=message_id,
|
||||
text=f"❌ Service '{service_name}' not registered in controller"
|
||||
)
|
||||
return
|
||||
else:
|
||||
await bot.edit_message_text(
|
||||
chat_id=chat_id,
|
||||
message_id=message_id,
|
||||
text=f"❌ Failed to fetch services (HTTP {resp.status})"
|
||||
)
|
||||
return
|
||||
except Exception as e:
|
||||
await bot.edit_message_text(
|
||||
chat_id=chat_id,
|
||||
message_id=message_id,
|
||||
text=f"❌ Error: {str(e)[:100]}"
|
||||
)
|
||||
await log_message(chat_id, f"Service lookup error", False, str(e))
|
||||
return
|
||||
else:
|
||||
# Identifier is numeric service_id
|
||||
try:
|
||||
service_id = int(identifier)
|
||||
except ValueError:
|
||||
return
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
@@ -181,13 +227,15 @@ async def send_message(request: MessageRequest):
|
||||
"parse_mode": "HTML"
|
||||
}
|
||||
|
||||
# Add inline buttons for service control if service_id is provided
|
||||
if request.service_id and request.service_name:
|
||||
# Add inline buttons for service control if service_id or service_name is provided
|
||||
if request.service_id or request.service_name:
|
||||
# Use service_id if available, otherwise use service_name prefixed with 'name_'
|
||||
identifier = str(request.service_id) if request.service_id else f"name_{request.service_name}"
|
||||
keyboard = [
|
||||
[
|
||||
InlineKeyboardButton("▶️ Start", callback_data=f"service_start_{request.service_id}"),
|
||||
InlineKeyboardButton("⏹️ Stop", callback_data=f"service_stop_{request.service_id}"),
|
||||
InlineKeyboardButton("🔄 Restart", callback_data=f"service_restart_{request.service_id}")
|
||||
InlineKeyboardButton("▶️ Start", callback_data=f"service_start_{identifier}"),
|
||||
InlineKeyboardButton("⏹️ Stop", callback_data=f"service_stop_{identifier}"),
|
||||
InlineKeyboardButton("🔄 Restart", callback_data=f"service_restart_{identifier}")
|
||||
]
|
||||
]
|
||||
kwargs["reply_markup"] = InlineKeyboardMarkup(keyboard)
|
||||
|
||||
Reference in New Issue
Block a user