Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files- Dockerfile +3 -3
- README.md +9 -8
- frontend.py +31 -38
- src/app.ts +22 -57
Dockerfile
CHANGED
|
@@ -16,11 +16,11 @@ ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
|
|
| 16 |
|
| 17 |
WORKDIR /app
|
| 18 |
COPY package*.json ./
|
| 19 |
-
RUN node -e "const fs = require('fs'); const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); pkg.scripts.prepare = ''; pkg.scripts.start = '
|
| 20 |
RUN npm install --legacy-peer-deps
|
| 21 |
RUN pip3 install --break-system-packages gradio requests
|
|
|
|
| 22 |
COPY . .
|
| 23 |
RUN npm run build
|
| 24 |
-
RUN chmod +x start.sh
|
| 25 |
EXPOSE 7860
|
| 26 |
-
CMD ["
|
|
|
|
| 16 |
|
| 17 |
WORKDIR /app
|
| 18 |
COPY package*.json ./
|
| 19 |
+
RUN node -e "const fs = require('fs'); const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); pkg.scripts.prepare = ''; pkg.scripts.start = ''; pkg.dependencies.express = '^4.18.2'; pkg.devDependencies['@types/express'] = '^4.17.17'; fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2));"
|
| 20 |
RUN npm install --legacy-peer-deps
|
| 21 |
RUN pip3 install --break-system-packages gradio requests
|
| 22 |
+
|
| 23 |
COPY . .
|
| 24 |
RUN npm run build
|
|
|
|
| 25 |
EXPOSE 7860
|
| 26 |
+
CMD ["python3", "frontend.py"]
|
README.md
CHANGED
|
@@ -1,18 +1,19 @@
|
|
| 1 |
---
|
| 2 |
-
title: WPPConnect
|
| 3 |
emoji: 🤖
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
sdk: docker
|
| 7 |
app_port: 7860
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
# WPPConnect
|
| 11 |
|
| 12 |
-
WhatsApp automation
|
| 13 |
|
| 14 |
-
##
|
| 15 |
-
-
|
| 16 |
-
-
|
| 17 |
-
-
|
| 18 |
-
-
|
|
|
|
| 1 |
---
|
| 2 |
+
title: WPPConnect API
|
| 3 |
emoji: 🤖
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
sdk: docker
|
| 7 |
app_port: 7860
|
| 8 |
+
pinned: false
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# WPPConnect API
|
| 12 |
|
| 13 |
+
Node.js WhatsApp automation API running in Docker.
|
| 14 |
|
| 15 |
+
## Endpoints
|
| 16 |
+
- GET /health
|
| 17 |
+
- GET /qr
|
| 18 |
+
- POST /send-message
|
| 19 |
+
- POST /send-poll
|
frontend.py
CHANGED
|
@@ -1,48 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
import time
|
| 4 |
import os
|
| 5 |
|
| 6 |
NODE_URL = "http://localhost:3000"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def get_status():
|
| 9 |
try:
|
| 10 |
-
r = requests.get(f"{NODE_URL}/health", timeout=
|
| 11 |
-
|
| 12 |
-
data = r.json()
|
| 13 |
-
return data
|
| 14 |
except:
|
| 15 |
-
|
| 16 |
-
return None
|
| 17 |
|
| 18 |
def update_ui():
|
| 19 |
status = get_status()
|
| 20 |
if not status:
|
| 21 |
-
return "Backend Offline",
|
| 22 |
|
| 23 |
-
msg = f"Status: {status
|
| 24 |
|
| 25 |
-
|
| 26 |
try:
|
| 27 |
-
qr_r = requests.get(f"{NODE_URL}/qr-data", timeout=
|
| 28 |
if qr_r.status_code == 200:
|
| 29 |
-
|
| 30 |
-
# In a real app we might want to convert base64 to image,
|
| 31 |
-
# but for now let's just use the ascii in a pre tag
|
| 32 |
-
qr_img = f"<html><body style='background:white;padding:10px;'><pre style='font-size:6px;line-height:1;color:black;margin:0;'>{qr_data['ascii']}</pre></body></html>"
|
| 33 |
except:
|
| 34 |
pass
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
return msg,
|
| 39 |
|
| 40 |
-
def
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
-
return
|
| 44 |
except Exception as e:
|
| 45 |
-
return
|
| 46 |
|
| 47 |
def send_poll(number, title, options):
|
| 48 |
try:
|
|
@@ -52,37 +53,29 @@ def send_poll(number, title, options):
|
|
| 52 |
except Exception as e:
|
| 53 |
return {"error": str(e)}
|
| 54 |
|
| 55 |
-
with gr.Blocks(title="WPPConnect
|
| 56 |
-
gr.Markdown("# 🤖 WPPConnect
|
| 57 |
|
| 58 |
with gr.Row():
|
| 59 |
-
with gr.Column(
|
| 60 |
-
status_text = gr.
|
| 61 |
-
|
| 62 |
-
with gr.Column(
|
| 63 |
qr_display = gr.HTML(label="WhatsApp QR Code")
|
| 64 |
|
| 65 |
-
with gr.Tab("Send Message"):
|
| 66 |
-
msg_number = gr.Textbox(label="Phone Number (e.g. 5511999999999)")
|
| 67 |
-
msg_text = gr.Textbox(label="Message Text")
|
| 68 |
-
msg_send = gr.Button("Send Text")
|
| 69 |
-
msg_output = gr.JSON(label="Result")
|
| 70 |
-
|
| 71 |
with gr.Tab("Send Poll"):
|
| 72 |
-
poll_number = gr.Textbox(label="Phone Number")
|
| 73 |
poll_title = gr.Textbox(label="Poll Question")
|
| 74 |
poll_options = gr.Textbox(label="Options (comma separated)")
|
| 75 |
poll_send = gr.Button("Send Poll")
|
| 76 |
poll_output = gr.JSON(label="Result")
|
| 77 |
|
| 78 |
-
with gr.
|
| 79 |
-
log_display = gr.Code(label="
|
| 80 |
|
| 81 |
-
|
| 82 |
-
msg_send.click(send_msg, inputs=[msg_number, msg_text], outputs=msg_output)
|
| 83 |
poll_send.click(send_poll, inputs=[poll_number, poll_title, poll_options], outputs=poll_output)
|
| 84 |
|
| 85 |
-
# Auto refresh every 5 seconds
|
| 86 |
demo.load(update_ui, outputs=[status_text, qr_display, log_display], every=5)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import subprocess
|
| 4 |
import time
|
| 5 |
import os
|
| 6 |
|
| 7 |
NODE_URL = "http://localhost:3000"
|
| 8 |
|
| 9 |
+
# Start Node.js backend
|
| 10 |
+
print("Starting Node.js backend...")
|
| 11 |
+
subprocess.Popen(["node", "dist/app.js"])
|
| 12 |
+
|
| 13 |
def get_status():
|
| 14 |
try:
|
| 15 |
+
r = requests.get(f"{NODE_URL}/health", timeout=1)
|
| 16 |
+
return r.json()
|
|
|
|
|
|
|
| 17 |
except:
|
| 18 |
+
return None
|
|
|
|
| 19 |
|
| 20 |
def update_ui():
|
| 21 |
status = get_status()
|
| 22 |
if not status:
|
| 23 |
+
return "Backend Offline", "Waiting for Node.js to start...", ""
|
| 24 |
|
| 25 |
+
msg = f"WhatsApp Status: {status['whatsappStatus']}\nConnected: {status['connected']}\nReady: {status['ready']}"
|
| 26 |
|
| 27 |
+
qr_text = ""
|
| 28 |
try:
|
| 29 |
+
qr_r = requests.get(f"{NODE_URL}/qr-data", timeout=1)
|
| 30 |
if qr_r.status_code == 200:
|
| 31 |
+
qr_text = qr_r.json()['ascii']
|
|
|
|
|
|
|
|
|
|
| 32 |
except:
|
| 33 |
pass
|
| 34 |
|
| 35 |
+
qr_html = f"<html><body style='background:white;color:black;padding:10px;display:flex;justify-content:center;'><pre style='font-size:6px;line-height:1;margin:0;'>{qr_text}</pre></body></html>" if qr_text else "No QR code generated yet."
|
| 36 |
+
|
| 37 |
+
logs = "\n".join(status.get('logs', []))
|
| 38 |
|
| 39 |
+
return msg, qr_html, logs
|
| 40 |
|
| 41 |
+
def trigger_init():
|
| 42 |
try:
|
| 43 |
+
requests.post(f"{NODE_URL}/init")
|
| 44 |
+
return "Initialization triggered."
|
| 45 |
except Exception as e:
|
| 46 |
+
return f"Error: {str(e)}"
|
| 47 |
|
| 48 |
def send_poll(number, title, options):
|
| 49 |
try:
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
return {"error": str(e)}
|
| 55 |
|
| 56 |
+
with gr.Blocks(title="WPPConnect") as demo:
|
| 57 |
+
gr.Markdown("# 🤖 WPPConnect Control Panel")
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
+
with gr.Column():
|
| 61 |
+
status_text = gr.Label(label="System Status")
|
| 62 |
+
init_btn = gr.Button("🚀 Start/Retry WhatsApp", variant="primary")
|
| 63 |
+
with gr.Column():
|
| 64 |
qr_display = gr.HTML(label="WhatsApp QR Code")
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
with gr.Tab("Send Poll"):
|
| 67 |
+
poll_number = gr.Textbox(label="Phone Number (with country code)")
|
| 68 |
poll_title = gr.Textbox(label="Poll Question")
|
| 69 |
poll_options = gr.Textbox(label="Options (comma separated)")
|
| 70 |
poll_send = gr.Button("Send Poll")
|
| 71 |
poll_output = gr.JSON(label="Result")
|
| 72 |
|
| 73 |
+
with gr.Accordion("System Logs", open=False):
|
| 74 |
+
log_display = gr.Code(label="Logs", language="text", lines=10)
|
| 75 |
|
| 76 |
+
init_btn.click(trigger_init, outputs=status_text)
|
|
|
|
| 77 |
poll_send.click(send_poll, inputs=[poll_number, poll_title, poll_options], outputs=poll_output)
|
| 78 |
|
|
|
|
| 79 |
demo.load(update_ui, outputs=[status_text, qr_display, log_display], every=5)
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
src/app.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import express from 'express';
|
| 2 |
import * as wppconnect from './index';
|
| 3 |
-
import dns from 'dns';
|
| 4 |
-
import { Resolver } from 'dns';
|
| 5 |
import fs from 'fs';
|
| 6 |
import path from 'path';
|
| 7 |
|
|
@@ -9,17 +7,16 @@ const app = express();
|
|
| 9 |
const port = process.env.NODE_PORT || 3000;
|
| 10 |
|
| 11 |
let whatsappClient: wppconnect.Whatsapp | null = null;
|
| 12 |
-
let lastQR: { ascii: string
|
| 13 |
-
let statusMsg: string = '
|
| 14 |
let appLogs: string[] = [];
|
| 15 |
let isReady = false;
|
| 16 |
-
let isInitializing = false;
|
| 17 |
|
| 18 |
const log = (msg: string) => {
|
| 19 |
const entry = `${new Date().toISOString()} - ${msg}`;
|
| 20 |
console.log(entry);
|
| 21 |
appLogs.push(entry);
|
| 22 |
-
if (appLogs.length >
|
| 23 |
};
|
| 24 |
|
| 25 |
app.use(express.json());
|
|
@@ -36,20 +33,14 @@ app.get('/health', (req, res) => {
|
|
| 36 |
});
|
| 37 |
|
| 38 |
app.get('/qr-data', (req, res) => {
|
| 39 |
-
if (lastQR)
|
| 40 |
-
|
| 41 |
-
} else {
|
| 42 |
-
res.status(404).json({ error: 'No QR yet' });
|
| 43 |
-
}
|
| 44 |
});
|
| 45 |
|
| 46 |
-
app.post('/
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
const result = await whatsappClient.sendText(`${telnumber}@c.us`, message);
|
| 51 |
-
res.json({ success: true, result });
|
| 52 |
-
} catch (e) { res.status(500).json({ error: e.message }); }
|
| 53 |
});
|
| 54 |
|
| 55 |
app.post('/send-poll', async (req, res) => {
|
|
@@ -58,71 +49,45 @@ app.post('/send-poll', async (req, res) => {
|
|
| 58 |
try {
|
| 59 |
const result = await whatsappClient.sendPollMessage(`${telnumber}@c.us`, name, choices);
|
| 60 |
res.json({ success: true, result });
|
| 61 |
-
} catch (e) {
|
|
|
|
|
|
|
|
|
|
| 62 |
});
|
| 63 |
|
| 64 |
-
async function
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
-
try {
|
| 69 |
-
const tokensDir = path.join(process.cwd(), 'tokens');
|
| 70 |
-
if (fs.existsSync(tokensDir)) {
|
| 71 |
-
fs.rmSync(tokensDir, { recursive: true, force: true });
|
| 72 |
-
}
|
| 73 |
-
} catch (e) {}
|
| 74 |
-
|
| 75 |
-
let waIP = '157.240.22.60';
|
| 76 |
-
try {
|
| 77 |
-
const resolver = new Resolver();
|
| 78 |
-
resolver.setServers(['8.8.8.8']);
|
| 79 |
-
const addrs = await new Promise<string[]>((resolve, reject) => {
|
| 80 |
-
resolver.resolve4('web.whatsapp.com', (err, addresses) => err ? reject(err) : resolve(addresses));
|
| 81 |
-
});
|
| 82 |
-
waIP = addrs[0];
|
| 83 |
-
log(`Resolved to ${waIP}`);
|
| 84 |
-
} catch (e) {
|
| 85 |
-
log(`DNS failed, using ${waIP}`);
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
statusMsg = 'Creating Client...';
|
| 89 |
try {
|
| 90 |
whatsappClient = await wppconnect.create({
|
| 91 |
session: 'hf-session',
|
| 92 |
-
catchQR: (
|
| 93 |
-
lastQR = {
|
| 94 |
statusMsg = 'Waiting for scan';
|
| 95 |
},
|
| 96 |
statusFind: (status) => {
|
| 97 |
statusMsg = status;
|
|
|
|
| 98 |
if (status === 'inChat') { isReady = true; lastQR = null; }
|
| 99 |
},
|
| 100 |
headless: true,
|
| 101 |
useChrome: false,
|
| 102 |
puppeteerOptions: {
|
| 103 |
executablePath: '/usr/bin/chromium',
|
| 104 |
-
args: [
|
| 105 |
-
'--no-sandbox',
|
| 106 |
-
'--disable-setuid-sandbox',
|
| 107 |
-
'--disable-dev-shm-usage',
|
| 108 |
-
'--disable-gpu',
|
| 109 |
-
`--host-resolver-rules=MAP web.whatsapp.com ${waIP}`,
|
| 110 |
-
],
|
| 111 |
},
|
| 112 |
autoClose: 0,
|
| 113 |
updatesLog: false,
|
| 114 |
waitForLogin: false,
|
| 115 |
});
|
| 116 |
-
log('
|
| 117 |
-
isInitializing = false;
|
| 118 |
} catch (e) {
|
| 119 |
log('Init Error: ' + e.message);
|
| 120 |
-
|
| 121 |
-
setTimeout(start, 30000);
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
app.listen(port, () => {
|
| 126 |
log(`Node Backend started on port ${port}`);
|
| 127 |
-
start();
|
| 128 |
});
|
|
|
|
| 1 |
import express from 'express';
|
| 2 |
import * as wppconnect from './index';
|
|
|
|
|
|
|
| 3 |
import fs from 'fs';
|
| 4 |
import path from 'path';
|
| 5 |
|
|
|
|
| 7 |
const port = process.env.NODE_PORT || 3000;
|
| 8 |
|
| 9 |
let whatsappClient: wppconnect.Whatsapp | null = null;
|
| 10 |
+
let lastQR: { ascii: string } | null = null;
|
| 11 |
+
let statusMsg: string = 'Offline';
|
| 12 |
let appLogs: string[] = [];
|
| 13 |
let isReady = false;
|
|
|
|
| 14 |
|
| 15 |
const log = (msg: string) => {
|
| 16 |
const entry = `${new Date().toISOString()} - ${msg}`;
|
| 17 |
console.log(entry);
|
| 18 |
appLogs.push(entry);
|
| 19 |
+
if (appLogs.length > 100) appLogs.shift();
|
| 20 |
};
|
| 21 |
|
| 22 |
app.use(express.json());
|
|
|
|
| 33 |
});
|
| 34 |
|
| 35 |
app.get('/qr-data', (req, res) => {
|
| 36 |
+
if (lastQR) res.json(lastQR);
|
| 37 |
+
else res.status(404).json({ error: 'No QR' });
|
|
|
|
|
|
|
|
|
|
| 38 |
});
|
| 39 |
|
| 40 |
+
app.post('/init', (req, res) => {
|
| 41 |
+
log('Manual init triggered');
|
| 42 |
+
startWPP();
|
| 43 |
+
res.json({ success: true });
|
|
|
|
|
|
|
|
|
|
| 44 |
});
|
| 45 |
|
| 46 |
app.post('/send-poll', async (req, res) => {
|
|
|
|
| 49 |
try {
|
| 50 |
const result = await whatsappClient.sendPollMessage(`${telnumber}@c.us`, name, choices);
|
| 51 |
res.json({ success: true, result });
|
| 52 |
+
} catch (e) {
|
| 53 |
+
log('Poll Error: ' + e.message);
|
| 54 |
+
res.status(500).json({ error: e.message });
|
| 55 |
+
}
|
| 56 |
});
|
| 57 |
|
| 58 |
+
async function startWPP() {
|
| 59 |
+
statusMsg = 'Initializing...';
|
| 60 |
+
log('Starting WPPConnect...');
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
try {
|
| 63 |
whatsappClient = await wppconnect.create({
|
| 64 |
session: 'hf-session',
|
| 65 |
+
catchQR: (b64, ascii) => {
|
| 66 |
+
lastQR = { ascii };
|
| 67 |
statusMsg = 'Waiting for scan';
|
| 68 |
},
|
| 69 |
statusFind: (status) => {
|
| 70 |
statusMsg = status;
|
| 71 |
+
log('Status: ' + status);
|
| 72 |
if (status === 'inChat') { isReady = true; lastQR = null; }
|
| 73 |
},
|
| 74 |
headless: true,
|
| 75 |
useChrome: false,
|
| 76 |
puppeteerOptions: {
|
| 77 |
executablePath: '/usr/bin/chromium',
|
| 78 |
+
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage', '--disable-gpu'],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
},
|
| 80 |
autoClose: 0,
|
| 81 |
updatesLog: false,
|
| 82 |
waitForLogin: false,
|
| 83 |
});
|
| 84 |
+
log('Client Created');
|
|
|
|
| 85 |
} catch (e) {
|
| 86 |
log('Init Error: ' + e.message);
|
| 87 |
+
statusMsg = 'Error: ' + e.message;
|
|
|
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
app.listen(port, () => {
|
| 92 |
log(`Node Backend started on port ${port}`);
|
|
|
|
| 93 |
});
|