feat: add /api/avatar endpoint and real-time poll vote event WS broadaster

This commit is contained in:
Hamza-Ayed
2026-05-18 19:47:42 +03:00
parent 39b028a85c
commit 1d20d40fd8

View File

@@ -364,6 +364,22 @@ function initWhatsApp() {
}
});
// Poll Vote updates (Listen to real-time votes on polls!)
waClient.on('vote', (vote) => {
try {
console.log(`[WA] Poll vote received from: ${vote.sender} for poll message: ${vote.pollMessageId}`);
broadcast({
type: 'poll_vote',
pollMessageId: vote.pollMessageId,
sender: vote.sender,
selectedOptions: vote.selectedOptions,
timestamp: vote.timestamp
});
} catch (err) {
console.error('[WA] Error broadcasting poll_vote event:', err.message);
}
});
// Disconnected
waClient.on('disconnected', (reason) => {
console.warn('[WA] Disconnected! Reason:', reason);
@@ -766,6 +782,22 @@ app.post('/api/send-poll', async (req, res) => {
// ─── HTTP Profile Pic Avatar Endpoint ───────────────────────────────────────
app.get('/api/avatar', async (req, res) => {
if (!clientReady) return res.status(503).json({ error: 'WhatsApp is not ready' });
const { phone } = req.query;
if (!phone) return res.status(400).json({ error: 'phone parameter is required' });
try {
const chatId = phone.includes('@') ? phone : `${phone}@c.us`;
const avatarUrl = await waClient.getProfilePicUrl(chatId);
res.status(200).json({ success: true, avatarUrl: avatarUrl || null });
} catch (err) {
console.error('[API] Avatar error:', err.message);
res.status(500).json({ error: err.message });
}
});
// ─── HTTP Health Endpoint ──────────────────────────────────────────────────
app.get('/health', (req, res) => {
res.status(200).json({