diff --git a/Dockerfile b/Dockerfile index 9e958d6..ad485dc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,22 +7,13 @@ RUN yarn install COPY . . -# ✅ Puppeteer dependencies for Chromium 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 \ + libatk-bridge2.0-0 \ + libxss1 \ + libasound2 \ + libgtk-3-0 \ + libgbm1 \ libx11-xcb1 \ libxcomposite1 \ libxdamage1 \ diff --git a/docker-compose.yml b/docker-compose.yml index f1cfcc2..0e78e05 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,11 +1,12 @@ version: '3.8' + services: - wweb: + baileys: build: . + container_name: baileys-whatsapp ports: - "3001:3001" volumes: - - .wweb-session:/root/.wwebjs_auth + - ./auth:/app/auth + - ./public:/app/public restart: unless-stopped -volumes: - .wweb-session: diff --git a/index.js b/index.js index 63f0a9e..0f955ff 100644 --- a/index.js +++ b/index.js @@ -1,73 +1,91 @@ -import pkg from 'whatsapp-web.js'; -const { Client, LocalAuth, Buttons } = pkg; - +// index.js +import makeWASocket, { useMultiFileAuthState, DisconnectReason, fetchLatestBaileysVersion } from '@whiskeysockets/baileys'; import express from 'express'; +import { Boom } from '@hapi/boom'; +import fs from 'fs'; import qrcode from 'qrcode'; import path from 'path'; 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(); -app.use(express.static('public')); app.use(express.json()); +app.use(express.static('public')); -let qrCodeData = null; -let clientReady = false; +let sock; +let qrData = null; +let isConnected = false; -const client = new Client({ - authStrategy: new LocalAuth(), - puppeteer: { - headless: true, - args: ['--no-sandbox', '--disable-setuid-sandbox'] - } -}); +const initBaileys = async () => { + const { version } = await fetchLatestBaileysVersion(); + const { state, saveCreds } = await useMultiFileAuthState('auth'); -client.on('qr', async (qr) => { - qrCodeData = await qrcode.toDataURL(qr); - clientReady = false; -}); + sock = makeWASocket({ + version, + auth: state, + printQRInTerminal: false + }); -client.on('ready', () => { - console.log('✅ WhatsApp prêt'); - clientReady = true; -}); + sock.ev.on('connection.update', async (update) => { + const { connection, lastDisconnect, qr } = update; + 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) => { 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) => { - res.json({ qr: qrCodeData, connected: clientReady }); + res.json({ qr: qrData, connected: isConnected }); }); -// --- API ENVOI TEXTE --- app.post('/sendText', async (req, res) => { const { phone, message } = req.body; + if (!sock || !isConnected) return res.status(400).json({ error: 'Non connecté' }); try { - await client.sendMessage(`${phone}@c.us`, message); + await sock.sendMessage(`${phone}@s.whatsapp.net`, { text: message }); res.json({ success: true }); } catch (e) { res.status(500).json({ error: e.message }); } }); -// --- API ENVOI BOUTONS --- 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 { - const btn = new Buttons(body, buttons, title || '', footer || ''); - await client.sendMessage(`${phone}@c.us`, btn); + const btnMsg = { + 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 }); } catch (e) { - console.error('❌ Erreur sendButtons:', e); 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')); diff --git a/public/login.html b/public/login.html index 6181c54..3702630 100644 --- a/public/login.html +++ b/public/login.html @@ -1,18 +1,26 @@ - -Connexion WhatsApp + + + + Connexion WhatsApp + -

Scanne le QR Code

-
Chargement...
+

Scanne le QR Code WhatsApp

+
Chargement...
+