Files
AyrisTech-WhatsApp-Gateway/whatsapp-worker.js

92 lines
3.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 qrCodes = 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) => {
const qrUrl = `https://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qr)}`;
qrCodes.set(userId, qrUrl);
console.log(`\n[QR] ${userId} için kod: ${qrUrl}\n`);
});
client.on('ready', () => {
qrCodes.delete(userId);
console.log(`[OK] ${userId} oturumu başarıyla açıldı!`);
});
client.initialize();
clients.set(userId, client);
return client;
};
app.get('/get-qr', async (req, res) => {
const { userId, secret } = req.query;
if (secret !== WEBHOOK_SECRET) return res.status(401).send('Yetkisiz!');
const client = getClient(userId);
// Zaten bağlıysa durum dön
try {
const state = await client.getState();
if (state === 'CONNECTED') return res.json({ status: 'connected' });
} catch (e) {}
// QR kodu bir kez yakalayıp dön (veya varsa Map'ten dön)
if (qrCodes.has(userId)) {
return res.json({ status: 'qr_ready', qr: qrCodes.get(userId) });
}
client.once('qr', (qr) => {
const qrUrl = `https://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qr)}`;
qrCodes.set(userId, qrUrl);
res.json({ status: 'qr_ready', qr: qrUrl });
});
});
app.get('/status', async (req, res) => {
const { userId, secret } = req.query;
if (secret !== WEBHOOK_SECRET) return res.status(401).send('Yetkisiz!');
if (!userId) return res.status(400).send('userId gerekli');
const client = clients.get(userId);
if (!client) return res.json({ status: 'disconnected', qr: null });
try {
const state = await client.getState();
return res.json({ status: state === 'CONNECTED' ? 'connected' : 'connecting', qr: qrCodes.get(userId) || null });
} catch (e) {
return res.json({ status: 'disconnected', qr: qrCodes.get(userId) || null });
}
});
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!`));