feat: expose REST API endpoints (/api/send, /api/send-media) for universal messaging proxy usage
This commit is contained in:
@@ -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 ──────────────────────────────────────────────────
|
// ─── HTTP Health Endpoint ──────────────────────────────────────────────────
|
||||||
app.get('/health', (req, res) => {
|
app.get('/health', (req, res) => {
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
@@ -712,6 +747,7 @@ app.get('/health', (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// ─── Start HTTP + WebSocket Server ─────────────────────────────────────────
|
// ─── Start HTTP + WebSocket Server ─────────────────────────────────────────
|
||||||
server.listen(PORT, () => {
|
server.listen(PORT, () => {
|
||||||
console.log(`[SERVER] Standalone WhatsApp Bridge running on port ${PORT}`);
|
console.log(`[SERVER] Standalone WhatsApp Bridge running on port ${PORT}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user