feat: implement native interactive WhatsApp Polls REST API endpoint (/api/send-poll)
This commit is contained in:
@@ -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 ──────────────────────────────────────────────────
|
// ─── HTTP Health Endpoint ──────────────────────────────────────────────────
|
||||||
app.get('/health', (req, res) => {
|
app.get('/health', (req, res) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user