From 5717d7047e835039b3fb2a7bd9c5b39dadf08611 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Mon, 18 May 2026 19:32:44 +0300 Subject: [PATCH] feat: expose REST API endpoints (/api/send, /api/send-media) for universal messaging proxy usage --- whatsapp_bridge/server.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/whatsapp_bridge/server.js b/whatsapp_bridge/server.js index 603197c..f2997d8 100644 --- a/whatsapp_bridge/server.js +++ b/whatsapp_bridge/server.js @@ -702,6 +702,41 @@ wss.on('connection', (ws, req) => { }); }); +// ─── HTTP REST API Endpoints (For easy integration as a Proxy/API) ─────── +app.use(express.json({ limit: '50mb' })); + +app.post('/api/send', async (req, res) => { + if (!clientReady) return res.status(503).json({ error: 'WhatsApp is not ready' }); + const { phone, message } = req.body; + if (!phone || !message) return res.status(400).json({ error: 'phone and message are required' }); + + try { + const chatId = phone.includes('@') ? phone : `${phone}@c.us`; + const sentMsg = await waClient.sendMessage(chatId, message); + res.status(200).json({ success: true, messageId: sentMsg.id.id }); + } catch (err) { + console.error('[API] Send error:', err.message); + res.status(500).json({ error: err.message }); + } +}); + +app.post('/api/send-media', async (req, res) => { + if (!clientReady) return res.status(503).json({ error: 'WhatsApp is not ready' }); + const { phone, base64, mimetype, filename, caption } = req.body; + if (!phone || !base64 || !mimetype) return res.status(400).json({ error: 'phone, base64, and mimetype are required' }); + + try { + const { MessageMedia } = require('whatsapp-web.js'); + const media = new MessageMedia(mimetype, base64, filename || 'file'); + const chatId = phone.includes('@') ? phone : `${phone}@c.us`; + const sentMsg = await waClient.sendMessage(chatId, media, { caption: caption || '' }); + res.status(200).json({ success: true, messageId: sentMsg.id.id }); + } catch (err) { + console.error('[API] Send media error:', err.message); + res.status(500).json({ error: err.message }); + } +}); + // ─── HTTP Health Endpoint ────────────────────────────────────────────────── app.get('/health', (req, res) => { res.status(200).json({ @@ -712,6 +747,7 @@ app.get('/health', (req, res) => { }); }); + // ─── Start HTTP + WebSocket Server ───────────────────────────────────────── server.listen(PORT, () => { console.log(`[SERVER] Standalone WhatsApp Bridge running on port ${PORT}`);