upd
This commit is contained in:
parent
6152e927bf
commit
051e5d7cb2
19
Dockerfile
19
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 \
|
||||
|
||||
@ -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:
|
||||
|
||||
92
index.js
92
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'));
|
||||
|
||||
@ -1,18 +1,26 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head><title>Connexion WhatsApp</title></head>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Connexion WhatsApp</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Scanne le QR Code</h1>
|
||||
<div id="qr">Chargement...</div>
|
||||
<h1>Scanne le QR Code WhatsApp</h1>
|
||||
<div id="qrcode">Chargement...</div>
|
||||
|
||||
<script>
|
||||
async function refreshQR() {
|
||||
async function checkQR() {
|
||||
const res = await fetch('/api/qrcode');
|
||||
const data = await res.json();
|
||||
if (data.connected) location.href = '/connected';
|
||||
else if (data.qr) document.getElementById('qr').innerHTML = `<img src="${data.qr}" />`;
|
||||
setTimeout(refreshQR, 3000);
|
||||
if (data.connected) {
|
||||
document.getElementById('qrcode').innerHTML = '✅ Connecté à WhatsApp !';
|
||||
} else if (data.qr) {
|
||||
document.getElementById('qrcode').innerHTML = `<img src="${data.qr}" />`;
|
||||
}
|
||||
setTimeout(checkQR, 3000);
|
||||
}
|
||||
refreshQR();
|
||||
|
||||
checkQR();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user