38443-vm/api_v1_webhook.php
Flatlogic Bot 4883125cda v2
2026-02-15 10:30:17 +00:00

45 lines
1.3 KiB
PHP

<?php
header('Content-Type: application/json');
require_once 'db/config.php';
$token = $_GET['token'] ?? '';
$data = json_decode(file_get_contents('php://input'), true);
$content = $data['content'] ?? '';
$username = $data['username'] ?? null;
if (empty($token)) {
http_response_code(401);
echo json_encode(['success' => false, 'error' => 'Missing token']);
exit;
}
$stmt = db()->prepare("SELECT * FROM webhooks WHERE token = ?");
$stmt->execute([$token]);
$webhook = $stmt->fetch();
if (!$webhook) {
http_response_code(401);
echo json_encode(['success' => false, 'error' => 'Invalid token']);
exit;
}
if (empty($content)) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'Empty content']);
exit;
}
try {
// We'll use a special System user or a placeholder user_id for webhooks
// Or we could create a bot user for each webhook.
// For now, let's assume we use user_id 1 (System) but override the name if provided.
$stmt = db()->prepare("INSERT INTO messages (channel_id, user_id, content) VALUES (?, ?, ?)");
$stmt->execute([$webhook['channel_id'], 1, $content]);
echo json_encode(['success' => true]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}