feat: integrate WAHA WhatsApp notifications for tasks, notes, and passwords with admin configuration settings
This commit is contained in:
42
db.php
42
db.php
@@ -52,3 +52,45 @@ function notify_group($group_id, $message) {
|
||||
notify_user($u['user_id'], $message);
|
||||
}
|
||||
}
|
||||
|
||||
function notify_waha($action, $module, $item_name) {
|
||||
global $pdo;
|
||||
try {
|
||||
$u_id = $_SESSION['user_id'] ?? 0;
|
||||
$current_user_name = "System";
|
||||
if($u_id) {
|
||||
$u = get_user($u_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;
|
||||
|
||||
$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"
|
||||
]);
|
||||
|
||||
$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);
|
||||
curl_exec($ch);
|
||||
curl_close($ch);
|
||||
}
|
||||
} catch(Exception $e) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user