Add default destination group configuration in whatsapp gateway UI

This commit is contained in:
Antigravity 2026-06-14 00:42:01 -04:00
parent ba5026bc3f
commit 915a2a004b
3 changed files with 70 additions and 3 deletions

View File

@ -151,7 +151,7 @@ async function sendWhatsAppNotification(email, summary) {
'x-app-name': 'Gmail Agent'
},
body: JSON.stringify({
to: WHATSAPP_TO,
to: 'default',
message: message
})
});

View File

@ -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 = `
<td><strong>${g.name}</strong><br><small style="color:var(--text-muted)">${g.id}</small></td>
<td>
<strong>${g.name}</strong>
${isDefault ? '<span class="badge success" style="margin-left:8px;font-size:10px;">Default</span>' : ''}
<br><small style="color:var(--text-muted)">${g.id}</small>
</td>
<td>${g.participants}</td>
<td>
${!isDefault ? `<button class="primary-btn" onclick="setDefaultDestination('${g.id}')" style="margin-right: 8px;">Set Default</button>` : ''}
<button class="secondary-btn" onclick="getInviteLink('${g.id}')">Get Invite Link</button>
</td>
`;
@ -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

View File

@ -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}`);
});