refactor: replace global WAHA notifications with granular user and group-specific templates and dispatch logic

This commit is contained in:
root
2026-03-30 01:02:23 +07:00
parent 68a764c0df
commit 596a233898
8 changed files with 502 additions and 131 deletions

142
db.php
View File

@@ -29,74 +29,108 @@ function get_user_groups($user_id) {
return $stmt->fetchAll();
}
function notify_user($user_id, $message) {
function compile_waha_template($template, $context) {
if (!$template) return "";
$action = $context['action'] ?? 'Update';
$module = $context['module'] ?? 'System';
$item = $context['item'] ?? '';
$user = $context['user'] ?? null;
if (!$user && isset($_SESSION['user_id'])) {
$u = get_user($_SESSION['user_id']);
$user = $u ? $u['username'] : 'System';
} elseif (!$user) {
$user = 'System';
}
$attachment_count = $context['attachment_count'] ?? '0';
$due_date = $context['due_date'] ?? 'None';
$created_date = $context['created_date'] ?? date('Y-m-d H:i');
$updated_date = $context['updated_date'] ?? date('Y-m-d H:i');
$assignor = $context['assignor'] ?? 'N/A';
$assignee = $context['assignee'] ?? 'N/A';
return str_replace(
['{action}', '{module}', '{item}', '{user}', '{attachment_count}', '{due_date}', '{created_date}', '{updated_date}', '{assignor}', '{assignee}'],
[$action, $module, $item, $user, $attachment_count, $due_date, $created_date, $updated_date, $assignor, $assignee],
$template
);
}
function dispatch_waha_message($endpoint, $chat_id, $session, $api_key, $text) {
try {
$payload = json_encode([
"chatId" => $chat_id,
"text" => $text,
"session" => $session ?: 'default'
]);
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$headers = ['Content-Type: application/json'];
if (!empty($api_key)) $headers[] = 'X-Api-Key: ' . $api_key;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_exec($ch);
curl_close($ch);
} catch(Exception $e) {}
}
function notify_user($user_id, $message, $context = []) {
global $pdo;
$stmt = $pdo->prepare("INSERT INTO notifications (user_id, message) VALUES (?, ?)");
$stmt->execute([$user_id, $message]);
// Virtual hook for Email/Telegram/WhatsApp based on user features
$user = get_user($user_id);
if($user && isset($user['notify_channels'])) {
$channels = explode(',', $user['notify_channels']);
// Here we would actually send the message to the third party API.
// For MVP we just assume it's sent.
if (in_array('whatsapp', $channels) && $user['waha_endpoint'] && $user['waha_chat_id']) {
$module = $context['module'] ?? '';
$template = '';
if (stripos($module, 'Note') !== false) $template = $user['waha_template_note'];
elseif (stripos($module, 'Password') !== false) $template = $user['waha_template_password'];
elseif (stripos($module, 'File') !== false || stripos($module, 'Folder') !== false) $template = $user['waha_template_file'];
else $template = $user['waha_template_task'];
if (!$template) $template = "*Alert:* {action} on {module} '{item}' by {user}";
$text = compile_waha_template($template, $context);
dispatch_waha_message($user['waha_endpoint'], $user['waha_chat_id'], $user['waha_session'], $user['waha_api_key'], $text);
}
}
}
function notify_group($group_id, $message) {
function notify_group($group_id, $message, $context = []) {
global $pdo;
// Notify all individual group members in their UI notifications
$stmt = $pdo->prepare("SELECT user_id FROM user_groups WHERE group_id = ?");
$stmt->execute([$group_id]);
$users = $stmt->fetchAll();
foreach ($users as $u) {
notify_user($u['user_id'], $message);
$s = $pdo->prepare("INSERT INTO notifications (user_id, message) VALUES (?, ?)");
$s->execute([$u['user_id'], $message]);
}
// Check if the group itself has WAHA configured
$stmt = $pdo->prepare("SELECT * FROM groups WHERE id = ?");
$stmt->execute([$group_id]);
$group = $stmt->fetch();
if($group && isset($group['notify_channels'])) {
$channels = explode(',', $group['notify_channels']);
if (in_array('whatsapp', $channels) && $group['waha_endpoint'] && $group['waha_chat_id']) {
$module = $context['module'] ?? '';
$template = '';
if (stripos($module, 'Note') !== false) $template = $group['waha_template_note'];
elseif (stripos($module, 'Password') !== false) $template = $group['waha_template_password'];
elseif (stripos($module, 'File') !== false || stripos($module, 'Folder') !== false) $template = $group['waha_template_file'];
else $template = $group['waha_template_task'];
if (!$template) $template = "*Group Alert:* {action} on {module} '{item}' by {user}";
$text = compile_waha_template($template, $context);
dispatch_waha_message($group['waha_endpoint'], $group['waha_chat_id'], $group['waha_session'], $group['waha_api_key'], $text);
}
}
}
function notify_waha($action, $module, $item_name) {
global $pdo;
try {
// 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 (isset($_SESSION['user_id'])) {
$u = get_user($_SESSION['user_id']);
if ($u) $current_user_name = $u['username'];
}
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" => $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);
$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) {}
}