feat: migrate WAHA configuration from system-wide to per-user settings with API key support
This commit is contained in:
48
db.php
48
db.php
@@ -56,41 +56,47 @@ function notify_group($group_id, $message) {
|
||||
function notify_waha($action, $module, $item_name) {
|
||||
global $pdo;
|
||||
try {
|
||||
$u_id = $_SESSION['user_id'] ?? 0;
|
||||
// Fetch all users with WhatsApp channel enabled and WAHA configured
|
||||
$stmt = $pdo->query("SELECT id, username, waha_endpoint, waha_chat_id, waha_template, waha_session, waha_api_key FROM users WHERE notify_channels LIKE '%whatsapp%' AND waha_endpoint IS NOT NULL AND waha_endpoint != ''");
|
||||
$wa_users = $stmt->fetchAll();
|
||||
if (empty($wa_users)) return;
|
||||
|
||||
$current_user_name = "System";
|
||||
if($u_id) {
|
||||
$u = get_user($u_id);
|
||||
if($u) $current_user_name = $u['username'];
|
||||
if (isset($_SESSION['user_id'])) {
|
||||
$u = get_user($_SESSION['user_id']);
|
||||
if ($u) $current_user_name = $u['username'];
|
||||
}
|
||||
|
||||
$stmt = $pdo->query("SELECT waha_endpoint, waha_chat_id, waha_template FROM users WHERE notify_channels LIKE '%whatsapp%' AND waha_endpoint IS NOT NULL AND waha_endpoint != ''");
|
||||
while ($user = $stmt->fetch()) {
|
||||
$endpoint = $user['waha_endpoint'];
|
||||
$chat_id = $user['waha_chat_id'];
|
||||
$template = $user['waha_template'] ?: "*Update:* {action} on {module} '{item}' by {user}";
|
||||
|
||||
if(!$endpoint || !$chat_id) continue;
|
||||
|
||||
|
||||
foreach ($wa_users as $wu) {
|
||||
$endpoint = $wu['waha_endpoint'];
|
||||
$chat_id = $wu['waha_chat_id'];
|
||||
if (!$endpoint || !$chat_id) continue;
|
||||
|
||||
$template = $wu['waha_template'] ?: "*Update:* {action} on {module} '{item}' by {user}";
|
||||
$text = str_replace(
|
||||
['{action}', '{module}', '{item}', '{user}'],
|
||||
[$action, $module, $item_name, $current_user_name],
|
||||
$template
|
||||
);
|
||||
|
||||
|
||||
$payload = json_encode([
|
||||
"chatId" => $chat_id,
|
||||
"text" => $text,
|
||||
"session" => "default"
|
||||
"chatId" => $chat_id,
|
||||
"text" => $text,
|
||||
"session" => $wu['waha_session'] ?: 'default'
|
||||
]);
|
||||
|
||||
|
||||
$ch = curl_init($endpoint);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
|
||||
$headers = ['Content-Type: application/json'];
|
||||
if (!empty($wu['waha_api_key'])) {
|
||||
$headers[] = 'X-Api-Key: ' . $wu['waha_api_key'];
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
|
||||
curl_exec($ch);
|
||||
curl_close($ch);
|
||||
}
|
||||
} catch(Exception $e) {}
|
||||
} catch (Exception $e) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user