feat: add QR and status endpoints for external access
This commit is contained in:
@@ -7,6 +7,7 @@ app.use(express.json());
|
|||||||
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET;
|
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET;
|
||||||
const PORT = process.env.PORT || 3005;
|
const PORT = process.env.PORT || 3005;
|
||||||
const clients = new Map();
|
const clients = new Map();
|
||||||
|
const qrCodes = new Map();
|
||||||
|
|
||||||
const getClient = (userId) => {
|
const getClient = (userId) => {
|
||||||
if (clients.has(userId)) return clients.get(userId);
|
if (clients.has(userId)) return clients.get(userId);
|
||||||
@@ -20,17 +21,61 @@ const getClient = (userId) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
client.on('qr', (qr) => {
|
client.on('qr', (qr) => {
|
||||||
// Terminalde boğulma diye link veriyoruz
|
const qrUrl = `https://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qr)}`;
|
||||||
console.log(`\n[QR] KODU BURADAN OKUT: https://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qr)}\n`);
|
qrCodes.set(userId, qrUrl);
|
||||||
|
console.log(`\n[QR] ${userId} için kod: ${qrUrl}\n`);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('ready', () => console.log(`[OK] ${userId} oturumu başarıyla açıldı!`));
|
client.on('ready', () => {
|
||||||
|
qrCodes.delete(userId);
|
||||||
|
console.log(`[OK] ${userId} oturumu başarıyla açıldı!`);
|
||||||
|
});
|
||||||
|
|
||||||
client.initialize();
|
client.initialize();
|
||||||
clients.set(userId, client);
|
clients.set(userId, client);
|
||||||
return 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) => {
|
app.post('/send-message', async (req, res) => {
|
||||||
const { secret, userId, to, message } = req.body;
|
const { secret, userId, to, message } = req.body;
|
||||||
if (secret !== WEBHOOK_SECRET) return res.status(401).send('Yetkisiz erişim!');
|
if (secret !== WEBHOOK_SECRET) return res.status(401).send('Yetkisiz erişim!');
|
||||||
|
|||||||
Reference in New Issue
Block a user