From 915a2a004be861e691ceeee0fbc54bd5bafdecf9 Mon Sep 17 00:00:00 2001 From: Antigravity Date: Sun, 14 Jun 2026 00:42:01 -0400 Subject: [PATCH] Add default destination group configuration in whatsapp gateway UI --- gmail-agent/index.js | 2 +- whatsapp-gateway/public/app.js | 30 ++++++++++++++++++++++++- whatsapp-gateway/server.js | 41 +++++++++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/gmail-agent/index.js b/gmail-agent/index.js index b7703a4..227c07f 100644 --- a/gmail-agent/index.js +++ b/gmail-agent/index.js @@ -151,7 +151,7 @@ async function sendWhatsAppNotification(email, summary) { 'x-app-name': 'Gmail Agent' }, body: JSON.stringify({ - to: WHATSAPP_TO, + to: 'default', message: message }) }); diff --git a/whatsapp-gateway/public/app.js b/whatsapp-gateway/public/app.js index 5be6824..130fe2a 100644 --- a/whatsapp-gateway/public/app.js +++ b/whatsapp-gateway/public/app.js @@ -97,13 +97,23 @@ async function fetchGroups() { return; } + const settingsRes = await fetch('api/settings'); + const settingsData = await settingsRes.json(); + const defaultDest = settingsData.settings?.defaultDestination || ''; + groupsTableBody.innerHTML = ''; data.groups.forEach(g => { + const isDefault = g.id === defaultDest; const tr = document.createElement('tr'); tr.innerHTML = ` - ${g.name}
${g.id} + + ${g.name} + ${isDefault ? 'Default' : ''} +
${g.id} + ${g.participants} + ${!isDefault ? `` : ''} `; @@ -115,6 +125,24 @@ async function fetchGroups() { } } +async function setDefaultDestination(groupId) { + try { + const res = await fetch('api/settings/default-destination', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ destinationId: groupId }) + }); + const data = await res.json(); + if (data.success) { + fetchGroups(); + } else { + alert('Error setting default: ' + data.error); + } + } catch (e) { + alert('Failed to set default destination.'); + } +} + document.getElementById('btn-refresh-groups').addEventListener('click', fetchGroups); // Connected Apps functionality diff --git a/whatsapp-gateway/server.js b/whatsapp-gateway/server.js index 156fdad..575a5c0 100644 --- a/whatsapp-gateway/server.js +++ b/whatsapp-gateway/server.js @@ -140,11 +140,20 @@ const handleSendMessage = async (req, res) => { return res.status(503).json({ success: false, error: 'WhatsApp Web client is not ready. Please scan the QR code in the dashboard.' }); } - const { to, message } = req.body; + const { message } = req.body; + let to = req.body.to; if (!to || !message) { return res.status(400).json({ success: false, error: "Missing 'to' or 'message' fields." }); } + if (to === 'default') { + const settings = loadSettings(); + if (!settings.defaultDestination) { + return res.status(400).json({ success: false, error: 'No default destination configured. Please configure it in the dashboard.' }); + } + to = settings.defaultDestination; + } + try { let targetJid = to; @@ -218,6 +227,36 @@ app.post('/api/groups/create', async (req, res) => { } }); +// ─── Settings ───────────────────────────────────────────────────────────── +const fs = require('fs'); +const SETTINGS_FILE = path.join(__dirname, 'settings.json'); + +function loadSettings() { + if (fs.existsSync(SETTINGS_FILE)) { + return JSON.parse(fs.readFileSync(SETTINGS_FILE)); + } + return {}; +} + +function saveSettings(settings) { + fs.writeFileSync(SETTINGS_FILE, JSON.stringify(settings, null, 2)); +} + +app.get('/api/settings', (req, res) => { + res.json({ success: true, settings: loadSettings() }); +}); + +app.post('/api/settings/default-destination', (req, res) => { + const { destinationId } = req.body; + if (!destinationId) return res.status(400).json({ success: false, error: 'destinationId is required' }); + + const settings = loadSettings(); + settings.defaultDestination = destinationId; + saveSettings(settings); + + res.json({ success: true, message: 'Default destination updated.' }); +}); + server.listen(PORT, '0.0.0.0', () => { console.log(`[WhatsApp Gateway] Server running at http://0.0.0.0:${PORT}`); });