From a99e9cbf2e1a2340ee52af93e5471f992f56cd8e Mon Sep 17 00:00:00 2001 From: julien Date: Tue, 6 May 2025 11:54:23 +0200 Subject: [PATCH] update --- Dockerfile | 21 +----- docker-compose.yml | 9 ++- index.js | 160 ++++++++++++--------------------------------- package.json | 10 +-- public/login.html | 30 +++------ 5 files changed, 62 insertions(+), 168 deletions(-) diff --git a/Dockerfile b/Dockerfile index 08e10d5..f306e91 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,27 +1,10 @@ FROM node:20 WORKDIR /app - -COPY package.json ./ -COPY yarn.lock ./ +COPY package.json yarn.lock ./ RUN yarn install COPY . . -# Puppeteer/Chrome deps -RUN apt-get update && apt-get install -y \ - libnss3 \ - libxss1 \ - libasound2 \ - libatk-bridge2.0-0 \ - libgtk-3-0 \ - libgbm1 \ - libx11-xcb1 \ - libxcomposite1 \ - libxdamage1 \ - libxrandr2 \ - xdg-utils \ - --no-install-recommends - -EXPOSE 3001 +EXPOSE 3000 CMD ["yarn", "start"] diff --git a/docker-compose.yml b/docker-compose.yml index 1b9de8b..f1cfcc2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,12 +1,11 @@ version: '3.8' - services: - whatsapp-bot: + wweb: build: . - container_name: whatsapp-bot ports: - "3001:3001" volumes: - - ./tokens:/app/tokens - - ./session-web-api:/app/session-web-api + - .wweb-session:/root/.wwebjs_auth restart: unless-stopped +volumes: + .wweb-session: diff --git a/index.js b/index.js index 26df984..e8b0343 100644 --- a/index.js +++ b/index.js @@ -1,143 +1,69 @@ -const { create } = require('venom-bot'); -const express = require('express'); -const path = require('path'); - -let client; -let qrCodeBase64 = null; -let isConnected = false; +import { Client, LocalAuth, Buttons } from 'whatsapp-web.js'; +import express from 'express'; +import qrcode from 'qrcode'; +import path from 'path'; +import { fileURLToPath } from 'url'; +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const app = express(); app.use(express.static('public')); app.use(express.json()); -// 📄 Page avec QR Code +let qrCodeData = null; +let clientReady = false; + +const client = new Client({ + authStrategy: new LocalAuth(), + puppeteer: { + headless: true, + args: ['--no-sandbox', '--disable-setuid-sandbox'] + } +}); + +client.on('qr', async (qr) => { + qrCodeData = await qrcode.toDataURL(qr); + clientReady = false; +}); + +client.on('ready', () => { + console.log('✅ WhatsApp prĂȘt'); + clientReady = true; +}); + +client.initialize(); + +// --- QR PAGE --- app.get('/login', (req, res) => { res.sendFile(path.join(__dirname, 'public', 'login.html')); }); - -// 📄 Page "connectĂ©" app.get('/connected', (req, res) => { res.sendFile(path.join(__dirname, 'public', 'connected.html')); }); - -// 🔄 QR code en base64 + Ă©tat de connexion app.get('/api/qrcode', (req, res) => { - res.json({ qr: qrCodeBase64, connected: isConnected }); -}); - -// ✅ API POST : envoie message avec boutons -app.post('/sendButtons', async (req, res) => { - const { phone, title, message, buttons } = req.body; - - console.log('đŸ“„ RequĂȘte reçue :'); - console.log('Phone:', phone); - console.log('Title:', title); - console.log('Message:', message); - console.log('Buttons brut:', buttons); - - if (!client) return res.status(500).json({ error: 'Client WhatsApp non initialisĂ©' }); - if (!phone || !message || !title || !Array.isArray(buttons)) { - return res.status(400).json({ error: 'ParamĂštres manquants ou invalides' }); - } - - try { - const formattedButtons = buttons.map(btn => ({ - buttonText: { displayText: btn.text } - })); - - console.log('✅ Boutons formatĂ©s pour Venom :', formattedButtons); - - await client.sendButtons( - `${phone}@c.us`, - title, // ✅ OBLIGATOIRE en 1er - message, // ✅ Description - formattedButtons // ✅ Format conforme - ); - - res.json({ success: true }); - } catch (error) { - console.error('❌ Erreur sendButtons:', error); - res.status(500).json({ error: error.message || 'Erreur interne' }); - } + res.json({ qr: qrCodeData, connected: clientReady }); }); +// --- API ENVOI TEXTE --- app.post('/sendText', async (req, res) => { const { phone, message } = req.body; - - console.log('đŸ“„ RequĂȘte TEXT :'); - console.log('Phone:', phone); - console.log('Message:', message); - - if (!client) return res.status(500).json({ error: 'Client WhatsApp non initialisĂ©' }); - if (!phone || !message) { - return res.status(400).json({ error: 'ParamĂštres manquants' }); - } - try { - await client.sendText(`${phone}@c.us`, message); + await client.sendMessage(`${phone}@c.us`, message); res.json({ success: true }); - } catch (error) { - console.error('❌ Erreur sendText:', error); - res.status(500).json({ error: error.message || 'Erreur interne' }); + } catch (e) { + res.status(500).json({ error: e.message }); } }); -// ✅ API POST : envoie un menu interactif (liste) -app.post('/sendListMenu', async (req, res) => { - const { phone, title, subtitle, description, buttonText, sections } = req.body; - - console.log('đŸ“„ RequĂȘte LIST MENU :'); - console.log('Phone:', phone); - console.log('Title:', title); - console.log('Subtitle:', subtitle); - console.log('Description:', description); - console.log('ButtonText:', buttonText); - console.log('Sections:', sections); - - if (!client) return res.status(500).json({ error: 'Client WhatsApp non initialisĂ©' }); +// --- API ENVOI BOUTONS --- +app.post('/sendButtons', async (req, res) => { + const { phone, title, message, buttons } = req.body; try { - await client.sendListMenu( - `${phone}@c.us`, - title, - subtitle, - description, - buttonText, - sections - ); + const formatted = new Buttons(message, buttons.map(b => b.text), title, ''); + await client.sendMessage(`${phone}@c.us`, formatted); res.json({ success: true }); - } catch (error) { - console.error('❌ Erreur sendListMenu:', error); - res.status(500).json({ error: error.message || 'Erreur interne' }); + } catch (e) { + res.status(500).json({ error: e.message }); } }); - - -// ▶ DĂ©marrage + QR code init -create({ - session: 'session-web-api', - multidevice: false, - catchQR: (base64Qrimg, asciiQR) => { - qrCodeBase64 = base64Qrimg; - isConnected = false; - }, - puppeteerOptions: { - headless: 'new', - args: ['--no-sandbox', '--disable-setuid-sandbox'] - } -}).then((cl) => { - client = cl; - - client.onStateChange((state) => { - if (state === 'CONNECTED') isConnected = true; - }); - - client.onStreamChange((state) => { - if (state === 'CONNECTED') isConnected = true; - }); - -}).catch((err) => { - console.error('Erreur init Venom:', err); -}); - -app.listen(3001, () => console.log('🚀 Serveur lancĂ© sur http://localhost:3001/login')); +app.listen(3000, () => console.log('🚀 Serveur dĂ©marrĂ© sur http://localhost:3000/login')); diff --git a/package.json b/package.json index 52ab594..404b728 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { - "name": "whatsappServer", + "name": "whatsapp-wweb", "version": "1.0.0", "main": "index.js", - "license": "MIT", + "type": "module", "scripts": { "start": "node index.js" }, "dependencies": { - "body-parser": "^2.2.0", - "express": "^5.1.0", - "venom-bot": "^5.3.0" + "express": "^4.18.4", + "qrcode": "^1.5.1", + "whatsapp-web.js": "^1.21.0" } } diff --git a/public/login.html b/public/login.html index 3a75bf0..6181c54 100644 --- a/public/login.html +++ b/public/login.html @@ -1,32 +1,18 @@ - - Connexion WhatsApp - - +Connexion WhatsApp -

Scannez ce QR Code

-
Chargement du QR Code...
- +

Scanne le QR Code

+
Chargement...