whatsapp_bridge/server.js aktualisiert
This commit is contained in:
@@ -6,6 +6,8 @@ const path = require('path');
|
|||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
|
// Statische Dateien aus dem "ui" Ordner servieren
|
||||||
app.use(express.static(path.join(__dirname, 'ui')));
|
app.use(express.static(path.join(__dirname, 'ui')));
|
||||||
|
|
||||||
let lastQr = "";
|
let lastQr = "";
|
||||||
@@ -25,6 +27,7 @@ const client = new Client({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// QR Code Handling
|
||||||
client.on('qr', (qr) => {
|
client.on('qr', (qr) => {
|
||||||
lastQr = qr;
|
lastQr = qr;
|
||||||
isReady = false;
|
isReady = false;
|
||||||
@@ -37,46 +40,55 @@ client.on('ready', () => {
|
|||||||
console.log('WhatsApp ist bereit!');
|
console.log('WhatsApp ist bereit!');
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('disconnected', () => {
|
// --- ROUTES ---
|
||||||
isReady = false;
|
|
||||||
console.log('WhatsApp Verbindung getrennt.');
|
// Hauptseite für Ingress
|
||||||
|
app.get('/', (req, res) => {
|
||||||
|
res.sendFile(path.join(__dirname, 'ui', 'index.html'));
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- API für die UI ---
|
// Status API
|
||||||
|
|
||||||
app.get('/api/status', async (req, res) => {
|
app.get('/api/status', async (req, res) => {
|
||||||
res.json({
|
try {
|
||||||
isReady: isReady,
|
let qrData = null;
|
||||||
hasQr: lastQr !== "",
|
if (lastQr) {
|
||||||
qrCode: lastQr ? await qrcode.toDataURL(lastQr) : null
|
qrData = await qrcode.toDataURL(lastQr);
|
||||||
});
|
}
|
||||||
|
res.json({
|
||||||
|
isReady: isReady,
|
||||||
|
hasQr: lastQr !== "",
|
||||||
|
qrCode: qrData
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
res.status(500).json({ error: e.message });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Logout
|
||||||
app.post('/auth/logout', async (req, res) => {
|
app.post('/auth/logout', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
console.log("Logging out...");
|
|
||||||
await client.logout();
|
await client.logout();
|
||||||
await fs.remove(SESSION_DATA);
|
await fs.remove(SESSION_DATA);
|
||||||
res.json({ success: true, message: "Logged out. Addon restarts now..." });
|
res.json({ success: true, message: "Erfolgreich abgemeldet. Addon startet neu..." });
|
||||||
setTimeout(() => process.exit(0), 2000);
|
setTimeout(() => process.exit(0), 2000);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
res.status(500).json({ error: err.message });
|
res.status(500).json({ error: err.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Reset
|
||||||
app.delete('/system/reset', async (req, res) => {
|
app.delete('/system/reset', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
console.log("Factory Reset triggered!");
|
|
||||||
await client.destroy();
|
await client.destroy();
|
||||||
await fs.emptyDir('/data');
|
await fs.emptyDir('/data');
|
||||||
res.json({ success: true, message: "All data deleted. Restarting..." });
|
res.json({ success: true, message: "Werkseinstellungen wiederhergestellt. Addon startet neu..." });
|
||||||
setTimeout(() => process.exit(0), 2000);
|
setTimeout(() => process.exit(0), 2000);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
res.status(500).json({ error: err.message });
|
res.status(500).json({ error: err.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Nachricht senden
|
// Sende-Schnittstelle
|
||||||
app.post('/send', async (req, res) => {
|
app.post('/send', async (req, res) => {
|
||||||
const { number, message } = req.body;
|
const { number, message } = req.body;
|
||||||
if (!isReady) return res.status(503).json({ error: 'Client nicht bereit' });
|
if (!isReady) return res.status(503).json({ error: 'Client nicht bereit' });
|
||||||
@@ -90,4 +102,4 @@ app.post('/send', async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
client.initialize();
|
client.initialize();
|
||||||
app.listen(3000, () => console.log('Add-on Server läuft auf Port 3000'));
|
app.listen(3000, () => console.log('WhatsApp Bridge Server läuft auf Port 3000'));
|
||||||
Reference in New Issue
Block a user