asd
This commit is contained in:
@@ -140,8 +140,11 @@ async def socketio_listener():
|
|||||||
)
|
)
|
||||||
if service_row:
|
if service_row:
|
||||||
service_id = service_row['id']
|
service_id = service_row['id']
|
||||||
except:
|
print(f" Found service_id: {service_id} for service: {service_name}")
|
||||||
pass
|
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("""
|
alert_id = await conn.fetchval("""
|
||||||
INSERT INTO attack_alerts (attack_id, alert_type, severity, message)
|
INSERT INTO attack_alerts (attack_id, alert_type, severity, message)
|
||||||
@@ -153,6 +156,8 @@ async def socketio_listener():
|
|||||||
)
|
)
|
||||||
RETURNING id
|
RETURNING id
|
||||||
""", attack_id, alert_message)
|
""", 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 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)
|
await conn.execute("UPDATE attack_alerts SET notified = true WHERE id = $1", alert_id)
|
||||||
print(f" Alert sent successfully")
|
print(f" Alert sent successfully")
|
||||||
|
|||||||
@@ -31,15 +31,61 @@ async def handle_button_click(callback_data: str, chat_id: int, message_id: int)
|
|||||||
if not callback_data.startswith("service_"):
|
if not callback_data.startswith("service_"):
|
||||||
return
|
return
|
||||||
|
|
||||||
parts = callback_data.rsplit("_", 1)
|
# Parse: service_{action}_{id_or_name}
|
||||||
if len(parts) != 2:
|
parts = callback_data.split("_", 2)
|
||||||
|
if len(parts) != 3:
|
||||||
return
|
return
|
||||||
|
|
||||||
action = parts[0].replace("service_", "")
|
action = parts[1] # start, stop, restart
|
||||||
try:
|
identifier = parts[2] # numeric service_id or 'name_{service_name}'
|
||||||
service_id = int(parts[1])
|
|
||||||
except ValueError:
|
# Determine if identifier is service_id (numeric) or service_name (prefixed with 'name_')
|
||||||
return
|
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:
|
try:
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
@@ -181,13 +227,15 @@ async def send_message(request: MessageRequest):
|
|||||||
"parse_mode": "HTML"
|
"parse_mode": "HTML"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Add inline buttons for service control if service_id is provided
|
# Add inline buttons for service control if service_id or service_name is provided
|
||||||
if request.service_id and request.service_name:
|
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 = [
|
keyboard = [
|
||||||
[
|
[
|
||||||
InlineKeyboardButton("▶️ Start", callback_data=f"service_start_{request.service_id}"),
|
InlineKeyboardButton("▶️ Start", callback_data=f"service_start_{identifier}"),
|
||||||
InlineKeyboardButton("⏹️ Stop", callback_data=f"service_stop_{request.service_id}"),
|
InlineKeyboardButton("⏹️ Stop", callback_data=f"service_stop_{identifier}"),
|
||||||
InlineKeyboardButton("🔄 Restart", callback_data=f"service_restart_{request.service_id}")
|
InlineKeyboardButton("🔄 Restart", callback_data=f"service_restart_{identifier}")
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
kwargs["reply_markup"] = InlineKeyboardMarkup(keyboard)
|
kwargs["reply_markup"] = InlineKeyboardMarkup(keyboard)
|
||||||
|
|||||||
Reference in New Issue
Block a user