This commit is contained in:
ilyastar9999
2025-12-04 14:19:15 +03:00
parent 8ab144796f
commit 154c0cda75
2 changed files with 67 additions and 14 deletions

View File

@@ -140,8 +140,11 @@ async def socketio_listener():
)
if service_row:
service_id = service_row['id']
except:
pass
print(f" Found service_id: {service_id} for service: {service_name}")
else:
print(f" Service {service_name} not found in services table, buttons will use service_name only")
except Exception as e:
print(f" Error looking up service_id: {e}")
alert_id = await conn.fetchval("""
INSERT INTO attack_alerts (attack_id, alert_type, severity, message)
@@ -153,6 +156,8 @@ async def socketio_listener():
)
RETURNING id
""", attack_id, alert_message)
print(f" Calling send_telegram_alert with service_id={service_id}, service_name={service_name}")
await send_telegram_alert(alert_message, service_id=service_id, service_name=service_name)
await conn.execute("UPDATE attack_alerts SET notified = true WHERE id = $1", alert_id)
print(f" Alert sent successfully")

View File

@@ -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)