Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files- frontend.py +17 -32
- src/app.ts +13 -4
frontend.py
CHANGED
|
@@ -14,17 +14,14 @@ def get_status():
|
|
| 14 |
try:
|
| 15 |
r = requests.get(f"{NODE_URL}/health", timeout=2)
|
| 16 |
return r.json()
|
| 17 |
-
except:
|
| 18 |
-
return
|
| 19 |
|
| 20 |
def update_ui():
|
| 21 |
status = get_status()
|
| 22 |
-
if not status:
|
| 23 |
-
return "Backend Offline", "Waiting for Node.js...", "", None
|
| 24 |
-
|
| 25 |
msg = f"Status: {status.get('whatsappStatus', 'Unknown')}\nConnected: {status.get('connected', False)}\nReady: {status.get('ready', False)}"
|
| 26 |
|
| 27 |
-
qr_html = "<div style='text-align:center; padding:20px; color:#666;'>QR code will appear here
|
| 28 |
if status.get('hasQR'):
|
| 29 |
try:
|
| 30 |
qr_r = requests.get(f"{NODE_URL}/qr-data", timeout=1)
|
|
@@ -34,33 +31,24 @@ def update_ui():
|
|
| 34 |
qr_html = f"""
|
| 35 |
<div style='background:white; color:black; padding:15px; display:flex; flex-direction:column; align-items:center; border-radius:8px; box-shadow: 0 4px 12px rgba(0,0,0,0.15);'>
|
| 36 |
<h3 style='margin:0 0 10px 0; font-family:sans-serif;'>Scan to Login</h3>
|
| 37 |
-
<pre style='font-size:7px; line-height:1; margin:0; font-family:monospace; font-weight:bold;
|
| 38 |
-
<p style='margin-top:10px; font-size:12px; font-family:sans-serif;'>Scan this with your WhatsApp mobile app</p>
|
| 39 |
</div>
|
| 40 |
"""
|
| 41 |
except:
|
| 42 |
pass
|
| 43 |
elif status.get('ready'):
|
| 44 |
-
qr_html = "<div style='text-align:center; padding:20px; color:#4caf50; font-weight:bold;'>WhatsApp is connected
|
| 45 |
|
| 46 |
logs = "\n".join(status.get('logs', []))
|
| 47 |
|
| 48 |
-
|
| 49 |
-
try:
|
| 50 |
-
sc_r = requests.get(f"{NODE_URL}/screenshot", timeout=5)
|
| 51 |
-
if sc_r.status_code == 200:
|
| 52 |
-
with open("current_screen.png", "wb") as f:
|
| 53 |
-
f.write(sc_r.content)
|
| 54 |
-
screenshot_path = "current_screen.png"
|
| 55 |
-
except:
|
| 56 |
-
pass
|
| 57 |
|
| 58 |
-
return msg, qr_html, logs,
|
| 59 |
|
| 60 |
def trigger_init():
|
| 61 |
try:
|
| 62 |
requests.post(f"{NODE_URL}/init")
|
| 63 |
-
return "Initialization triggered.
|
| 64 |
except Exception as e:
|
| 65 |
return f"Error: {str(e)}"
|
| 66 |
|
|
@@ -72,13 +60,13 @@ def send_poll(number, title, options):
|
|
| 72 |
except Exception as e:
|
| 73 |
return {"error": str(e)}
|
| 74 |
|
| 75 |
-
with gr.Blocks(title="WPPConnect Dashboard"
|
| 76 |
-
gr.Markdown("# π€ WPPConnect
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
with gr.Column(scale=1):
|
| 80 |
status_display = gr.Label(label="System Status")
|
| 81 |
-
init_btn = gr.Button("π Start/
|
| 82 |
refresh_btn = gr.Button("π Refresh UI")
|
| 83 |
|
| 84 |
with gr.Column(scale=2):
|
|
@@ -87,27 +75,24 @@ with gr.Blocks(title="WPPConnect Dashboard", theme=gr.themes.Soft()) as demo:
|
|
| 87 |
with gr.Tabs():
|
| 88 |
with gr.TabItem("π Send Poll"):
|
| 89 |
with gr.Row():
|
| 90 |
-
poll_num = gr.Textbox(label="
|
| 91 |
-
poll_title = gr.Textbox(label="Poll Question"
|
| 92 |
poll_opts = gr.Textbox(label="Choices (comma separated)", placeholder="Yes, No, Maybe")
|
| 93 |
poll_send = gr.Button("Send Poll Now", variant="primary")
|
| 94 |
poll_out = gr.JSON(label="Result")
|
| 95 |
|
| 96 |
with gr.TabItem("π System Logs"):
|
| 97 |
-
log_display = gr.Code(label="Recent
|
| 98 |
|
| 99 |
with gr.TabItem("π Browser Debug"):
|
| 100 |
-
screenshot_display = gr.Image(label="Live Browser
|
| 101 |
|
| 102 |
init_btn.click(trigger_init, outputs=status_display)
|
| 103 |
refresh_btn.click(update_ui, outputs=[status_display, qr_display, log_display, screenshot_display])
|
| 104 |
poll_send.click(send_poll, inputs=[poll_num, poll_title, poll_opts], outputs=poll_out)
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
-
demo.load(update_ui, outputs=[status_display, qr_display, log_display, screenshot_display], every=10)
|
| 109 |
-
except:
|
| 110 |
-
demo.load(update_ui, outputs=[status_display, qr_display, log_display, screenshot_display])
|
| 111 |
|
| 112 |
if __name__ == "__main__":
|
| 113 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 14 |
try:
|
| 15 |
r = requests.get(f"{NODE_URL}/health", timeout=2)
|
| 16 |
return r.json()
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return {"whatsappStatus": "Offline", "connected": False, "ready": False, "logs": [str(e)]}
|
| 19 |
|
| 20 |
def update_ui():
|
| 21 |
status = get_status()
|
|
|
|
|
|
|
|
|
|
| 22 |
msg = f"Status: {status.get('whatsappStatus', 'Unknown')}\nConnected: {status.get('connected', False)}\nReady: {status.get('ready', False)}"
|
| 23 |
|
| 24 |
+
qr_html = "<div style='text-align:center; padding:20px; color:#666;'>QR code will appear here. If it takes too long, check logs.</div>"
|
| 25 |
if status.get('hasQR'):
|
| 26 |
try:
|
| 27 |
qr_r = requests.get(f"{NODE_URL}/qr-data", timeout=1)
|
|
|
|
| 31 |
qr_html = f"""
|
| 32 |
<div style='background:white; color:black; padding:15px; display:flex; flex-direction:column; align-items:center; border-radius:8px; box-shadow: 0 4px 12px rgba(0,0,0,0.15);'>
|
| 33 |
<h3 style='margin:0 0 10px 0; font-family:sans-serif;'>Scan to Login</h3>
|
| 34 |
+
<pre style='font-size:7px; line-height:1; margin:0; font-family:monospace; font-weight:bold;'>{ascii_qr}</pre>
|
|
|
|
| 35 |
</div>
|
| 36 |
"""
|
| 37 |
except:
|
| 38 |
pass
|
| 39 |
elif status.get('ready'):
|
| 40 |
+
qr_html = "<div style='text-align:center; padding:20px; color:#4caf50; font-weight:bold;'>WhatsApp is connected!</div>"
|
| 41 |
|
| 42 |
logs = "\n".join(status.get('logs', []))
|
| 43 |
|
| 44 |
+
screenshot_url = f"{NODE_URL}/screenshot?t={int(time.time())}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
return msg, qr_html, logs, screenshot_url
|
| 47 |
|
| 48 |
def trigger_init():
|
| 49 |
try:
|
| 50 |
requests.post(f"{NODE_URL}/init")
|
| 51 |
+
return "Initialization triggered."
|
| 52 |
except Exception as e:
|
| 53 |
return f"Error: {str(e)}"
|
| 54 |
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
return {"error": str(e)}
|
| 62 |
|
| 63 |
+
with gr.Blocks(title="WPPConnect Dashboard") as demo:
|
| 64 |
+
gr.Markdown("# π€ WPPConnect Control Panel")
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
with gr.Column(scale=1):
|
| 68 |
status_display = gr.Label(label="System Status")
|
| 69 |
+
init_btn = gr.Button("π Start/Retry Session", variant="primary")
|
| 70 |
refresh_btn = gr.Button("π Refresh UI")
|
| 71 |
|
| 72 |
with gr.Column(scale=2):
|
|
|
|
| 75 |
with gr.Tabs():
|
| 76 |
with gr.TabItem("π Send Poll"):
|
| 77 |
with gr.Row():
|
| 78 |
+
poll_num = gr.Textbox(label="Phone Number", placeholder="5511999999999")
|
| 79 |
+
poll_title = gr.Textbox(label="Poll Question")
|
| 80 |
poll_opts = gr.Textbox(label="Choices (comma separated)", placeholder="Yes, No, Maybe")
|
| 81 |
poll_send = gr.Button("Send Poll Now", variant="primary")
|
| 82 |
poll_out = gr.JSON(label="Result")
|
| 83 |
|
| 84 |
with gr.TabItem("π System Logs"):
|
| 85 |
+
log_display = gr.Code(label="Recent Logs", lines=15, language="markdown")
|
| 86 |
|
| 87 |
with gr.TabItem("π Browser Debug"):
|
| 88 |
+
screenshot_display = gr.Image(label="Live Browser View")
|
| 89 |
|
| 90 |
init_btn.click(trigger_init, outputs=status_display)
|
| 91 |
refresh_btn.click(update_ui, outputs=[status_display, qr_display, log_display, screenshot_display])
|
| 92 |
poll_send.click(send_poll, inputs=[poll_num, poll_title, poll_opts], outputs=poll_out)
|
| 93 |
|
| 94 |
+
# Simple recurring update
|
| 95 |
+
demo.load(update_ui, outputs=[status_display, qr_display, log_display, screenshot_display])
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
src/app.ts
CHANGED
|
@@ -60,6 +60,15 @@ app.post('/init', (req, res) => {
|
|
| 60 |
res.json({ success: true });
|
| 61 |
});
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
app.post('/send-poll', async (req, res) => {
|
| 64 |
const { telnumber, name, choices } = req.body;
|
| 65 |
if (!whatsappClient || !isReady) return res.status(503).json({ error: 'WhatsApp not ready' });
|
|
@@ -75,7 +84,7 @@ async function startWPP() {
|
|
| 75 |
statusMsg = 'Initializing...';
|
| 76 |
log('Starting WPPConnect init...');
|
| 77 |
|
| 78 |
-
let waIP = '
|
| 79 |
try {
|
| 80 |
const resolver = new Resolver();
|
| 81 |
resolver.setServers(['8.8.8.8', '1.1.1.1']);
|
|
@@ -87,10 +96,10 @@ async function startWPP() {
|
|
| 87 |
log(`Resolved web.whatsapp.com to ${waIP}`);
|
| 88 |
}
|
| 89 |
} catch (e) {
|
| 90 |
-
log(`DNS
|
| 91 |
}
|
| 92 |
|
| 93 |
-
//
|
| 94 |
try {
|
| 95 |
const tokensDir = path.join(process.cwd(), 'tokens');
|
| 96 |
if (fs.existsSync(tokensDir)) fs.rmSync(tokensDir, { recursive: true, force: true });
|
|
@@ -106,7 +115,7 @@ async function startWPP() {
|
|
| 106 |
},
|
| 107 |
statusFind: (status) => {
|
| 108 |
statusMsg = status;
|
| 109 |
-
log('Status: ' + status);
|
| 110 |
if (status === 'inChat') { isReady = true; lastQR = null; }
|
| 111 |
},
|
| 112 |
headless: true,
|
|
|
|
| 60 |
res.json({ success: true });
|
| 61 |
});
|
| 62 |
|
| 63 |
+
app.post('/send-message', async (req, res) => {
|
| 64 |
+
const { telnumber, message } = req.body;
|
| 65 |
+
if (!whatsappClient || !isReady) return res.status(503).json({ error: 'WhatsApp not ready' });
|
| 66 |
+
try {
|
| 67 |
+
const result = await whatsappClient.sendText(`${telnumber}@c.us`, message);
|
| 68 |
+
res.json({ success: true, result });
|
| 69 |
+
} catch (e: any) { res.status(500).json({ error: e.message }); }
|
| 70 |
+
});
|
| 71 |
+
|
| 72 |
app.post('/send-poll', async (req, res) => {
|
| 73 |
const { telnumber, name, choices } = req.body;
|
| 74 |
if (!whatsappClient || !isReady) return res.status(503).json({ error: 'WhatsApp not ready' });
|
|
|
|
| 84 |
statusMsg = 'Initializing...';
|
| 85 |
log('Starting WPPConnect init...');
|
| 86 |
|
| 87 |
+
let waIP = '31.13.66.56'; // Use the IP that worked in logs
|
| 88 |
try {
|
| 89 |
const resolver = new Resolver();
|
| 90 |
resolver.setServers(['8.8.8.8', '1.1.1.1']);
|
|
|
|
| 96 |
log(`Resolved web.whatsapp.com to ${waIP}`);
|
| 97 |
}
|
| 98 |
} catch (e) {
|
| 99 |
+
log(`DNS failed, using ${waIP}`);
|
| 100 |
}
|
| 101 |
|
| 102 |
+
// Cleanup tokens to avoid locks
|
| 103 |
try {
|
| 104 |
const tokensDir = path.join(process.cwd(), 'tokens');
|
| 105 |
if (fs.existsSync(tokensDir)) fs.rmSync(tokensDir, { recursive: true, force: true });
|
|
|
|
| 115 |
},
|
| 116 |
statusFind: (status) => {
|
| 117 |
statusMsg = status;
|
| 118 |
+
log('Status Change: ' + status);
|
| 119 |
if (status === 'inChat') { isReady = true; lastQR = null; }
|
| 120 |
},
|
| 121 |
headless: true,
|