This commit is contained in:
julien 2025-05-06 12:12:55 +02:00
parent 6152e927bf
commit 051e5d7cb2
4 changed files with 82 additions and 64 deletions

View File

@ -7,22 +7,13 @@ RUN yarn install
COPY . . COPY . .
# ✅ Puppeteer dependencies for Chromium
RUN apt-get update && apt-get install -y \ RUN apt-get update && apt-get install -y \
wget \
ca-certificates \
fonts-liberation \
libappindicator3-1 \
libasound2 \
libatk-bridge2.0-0 \
libatk1.0-0 \
libcups2 \
libdbus-1-3 \
libdrm2 \
libgbm1 \
libgtk-3-0 \
libnspr4 \
libnss3 \ libnss3 \
libatk-bridge2.0-0 \
libxss1 \
libasound2 \
libgtk-3-0 \
libgbm1 \
libx11-xcb1 \ libx11-xcb1 \
libxcomposite1 \ libxcomposite1 \
libxdamage1 \ libxdamage1 \

View File

@ -1,11 +1,12 @@
version: '3.8' version: '3.8'
services: services:
wweb: baileys:
build: . build: .
container_name: baileys-whatsapp
ports: ports:
- "3001:3001" - "3001:3001"
volumes: volumes:
- .wweb-session:/root/.wwebjs_auth - ./auth:/app/auth
- ./public:/app/public
restart: unless-stopped restart: unless-stopped
volumes:
.wweb-session:

View File

@ -1,73 +1,91 @@
import pkg from 'whatsapp-web.js'; // index.js
const { Client, LocalAuth, Buttons } = pkg; import makeWASocket, { useMultiFileAuthState, DisconnectReason, fetchLatestBaileysVersion } from '@whiskeysockets/baileys';
import express from 'express'; import express from 'express';
import { Boom } from '@hapi/boom';
import fs from 'fs';
import qrcode from 'qrcode'; import qrcode from 'qrcode';
import path from 'path'; import path from 'path';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url)); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express(); const app = express();
app.use(express.static('public'));
app.use(express.json()); app.use(express.json());
app.use(express.static('public'));
let qrCodeData = null; let sock;
let clientReady = false; let qrData = null;
let isConnected = false;
const client = new Client({ const initBaileys = async () => {
authStrategy: new LocalAuth(), const { version } = await fetchLatestBaileysVersion();
puppeteer: { const { state, saveCreds } = await useMultiFileAuthState('auth');
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
}
});
client.on('qr', async (qr) => { sock = makeWASocket({
qrCodeData = await qrcode.toDataURL(qr); version,
clientReady = false; auth: state,
}); printQRInTerminal: false
});
client.on('ready', () => { sock.ev.on('connection.update', async (update) => {
console.log('✅ WhatsApp prêt'); const { connection, lastDisconnect, qr } = update;
clientReady = true; if (qr) {
}); qrData = await qrcode.toDataURL(qr);
isConnected = false;
}
if (connection === 'close') {
const shouldReconnect = lastDisconnect?.error?.output?.statusCode !== DisconnectReason.loggedOut;
if (shouldReconnect) {
console.log('🔁 Reconnexion...');
initBaileys();
} else {
console.log('❌ Déconnecté.');
}
} else if (connection === 'open') {
console.log('✅ Connecté à WhatsApp');
isConnected = true;
}
});
client.initialize(); sock.ev.on('creds.update', saveCreds);
};
initBaileys();
// --- QR PAGE ---
app.get('/login', (req, res) => { app.get('/login', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'login.html')); res.sendFile(path.join(__dirname, 'public', 'login.html'));
}); });
app.get('/connected', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'connected.html'));
});
app.get('/api/qrcode', (req, res) => { app.get('/api/qrcode', (req, res) => {
res.json({ qr: qrCodeData, connected: clientReady }); res.json({ qr: qrData, connected: isConnected });
}); });
// --- API ENVOI TEXTE ---
app.post('/sendText', async (req, res) => { app.post('/sendText', async (req, res) => {
const { phone, message } = req.body; const { phone, message } = req.body;
if (!sock || !isConnected) return res.status(400).json({ error: 'Non connecté' });
try { try {
await client.sendMessage(`${phone}@c.us`, message); await sock.sendMessage(`${phone}@s.whatsapp.net`, { text: message });
res.json({ success: true }); res.json({ success: true });
} catch (e) { } catch (e) {
res.status(500).json({ error: e.message }); res.status(500).json({ error: e.message });
} }
}); });
// --- API ENVOI BOUTONS ---
app.post('/sendButtons', async (req, res) => { app.post('/sendButtons', async (req, res) => {
const { phone, body, buttons, title, footer } = req.body; const { phone, message, buttons } = req.body;
if (!sock || !isConnected) return res.status(400).json({ error: 'Non connecté' });
try { try {
const btn = new Buttons(body, buttons, title || '', footer || ''); const btnMsg = {
await client.sendMessage(`${phone}@c.us`, btn); text: message,
buttons: buttons.map((b, i) => ({ buttonId: `btn_${i}`, buttonText: { displayText: b }, type: 1 })),
headerType: 1
};
await sock.sendMessage(`${phone}@s.whatsapp.net`, btnMsg);
res.json({ success: true }); res.json({ success: true });
} catch (e) { } catch (e) {
console.error('❌ Erreur sendButtons:', e);
res.status(500).json({ error: e.message }); res.status(500).json({ error: e.message });
} }
}); });
app.listen(3001, () => console.log('🚀 Serveur démarré sur http://localhost:3000/login')); app.listen(3001, () => console.log('🚀 Serveur Baileys démarré sur http://localhost:3000'));

View File

@ -1,18 +1,26 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang="fr">
<head><title>Connexion WhatsApp</title></head> <head>
<meta charset="UTF-8">
<title>Connexion WhatsApp</title>
</head>
<body> <body>
<h1>Scanne le QR Code</h1> <h1>Scanne le QR Code WhatsApp</h1>
<div id="qr">Chargement...</div> <div id="qrcode">Chargement...</div>
<script> <script>
async function refreshQR() { async function checkQR() {
const res = await fetch('/api/qrcode'); const res = await fetch('/api/qrcode');
const data = await res.json(); const data = await res.json();
if (data.connected) location.href = '/connected'; if (data.connected) {
else if (data.qr) document.getElementById('qr').innerHTML = `<img src="${data.qr}" />`; document.getElementById('qrcode').innerHTML = '✅ Connecté à WhatsApp !';
setTimeout(refreshQR, 3000); } else if (data.qr) {
document.getElementById('qrcode').innerHTML = `<img src="${data.qr}" />`;
}
setTimeout(checkQR, 3000);
} }
refreshQR();
checkQR();
</script> </script>
</body> </body>
</html> </html>