feat: implement native interactive WhatsApp Polls REST API endpoint (/api/send-poll)

This commit is contained in:
Hamza-Ayed
2026-05-18 19:44:09 +03:00
parent 9490a2d628
commit 39b028a85c

View File

@@ -743,6 +743,28 @@ app.post('/api/send-media', async (req, res) => {
}
});
app.post('/api/send-poll', async (req, res) => {
if (!clientReady) return res.status(503).json({ error: 'WhatsApp is not ready' });
const { phone, question, options, allowMultiple } = req.body;
if (!phone || !question || !options || !Array.isArray(options)) {
return res.status(400).json({ error: 'phone, question, and options (array of strings) are required' });
}
try {
const { Poll } = require('whatsapp-web.js');
const poll = new Poll(question, options, {
allowMultipleAnswers: allowMultiple !== false // default to true
});
const chatId = phone.includes('@') ? phone : `${phone}@c.us`;
const sentMsg = await waClient.sendMessage(chatId, poll);
res.status(200).json({ success: true, messageId: sentMsg.id.id });
} catch (err) {
console.error('[API] Send poll error:', err.message);
res.status(500).json({ error: err.message });
}
});
// ─── HTTP Health Endpoint ──────────────────────────────────────────────────
app.get('/health', (req, res) => {