require('dotenv').config(); const { Client, LocalAuth } = require('whatsapp-web.js'); const express = require('express'); const app = express(); app.use(express.json()); const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET; const PORT = process.env.PORT || 3005; const clients = new Map(); const getClient = (userId) => { if (clients.has(userId)) return clients.get(userId); const client = new Client({ authStrategy: new LocalAuth({ clientId: userId }), puppeteer: { headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] } }); client.on('qr', (qr) => { // Terminalde boğulma diye link veriyoruz console.log(`\n[QR] KODU BURADAN OKUT: https://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qr)}\n`); }); client.on('ready', () => console.log(`[OK] ${userId} oturumu başarıyla açıldı!`)); client.initialize(); clients.set(userId, client); return client; }; app.post('/send-message', async (req, res) => { const { secret, userId, to, message } = req.body; if (secret !== WEBHOOK_SECRET) return res.status(401).send('Yetkisiz erişim!'); try { const client = getClient(userId); await client.sendMessage(`${to}@c.us`, message); res.send({ success: true }); } catch (err) { res.status(500).send({ success: false, error: err.message }); } }); app.listen(PORT, () => console.log(`WhatsApp Worker ${PORT} portunda aktif!`));