File size: 5,080 Bytes
1fd158f
 
 
 
 
4ccc89a
1fd158f
 
 
6dfbc12
aca0d15
bc28894
fcb47be
1fd158f
 
 
 
1a89404
 
7a12d0e
1a89404
7a12d0e
1a89404
7a12d0e
34728cc
 
 
 
 
fcb47be
34728cc
7a12d0e
 
c46657d
7a12d0e
34728cc
 
 
 
 
7a12d0e
 
 
 
 
 
1fd158f
aca0d15
1fd158f
4ccc89a
 
 
 
fcb47be
4ccc89a
 
 
 
36d7482
1a89404
1fd158f
aca0d15
1fd158f
87ab1e4
7a12d0e
1a89404
 
 
 
 
c46657d
7a12d0e
1fd158f
fcb47be
1fd158f
fcb47be
f1904af
fcb47be
 
f1904af
 
 
 
7a12d0e
 
1fd158f
 
6dfbc12
7a12d0e
fcb47be
c46657d
 
 
87ab1e4
6dfbc12
c46657d
1be4e49
7a12d0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fd158f
1be4e49
1a89404
36d7482
c46657d
1fd158f
7a12d0e
 
 
 
1fd158f
 
7a12d0e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import gradio as gr
import requests
import time
import os

NODE_URL = "http://127.0.0.1:3000"

def get_status():
    try:
        r = requests.get(f"{NODE_URL}/health", timeout=2)
        return r.json()
    except Exception as e:
        return {"whatsappStatus": "Offline", "connected": False, "ready": False, "logs": [str(e)]}

def update_ui():
    status = get_status()
    
    status_msg = status.get('whatsappStatus', 'Unknown')
    connected = "Connected" if status.get('connected') else "Disconnected"
    ready = "READY" if status.get('ready') else "Initializing"
    
    info_text = f"## 🚦 Status: {status_msg}\n**Instance:** {connected} | **API:** {ready}"
    
    qr_html = "<div style='text-align:center; padding:20px; color:#888;'>Waiting for WhatsApp initialization...</div>"
    if status.get('hasQR'):
        try:
            qr_r = requests.get(f"{NODE_URL}/qr-data", timeout=1)
            if qr_r.status_code == 200:
                qr_data = qr_r.json()
                ascii_qr = qr_data['ascii']
                qr_html = f"""
                <div style='background:white; color:black; padding:20px; display:flex; flex-direction:column; align-items:center; border-radius:12px; border: 4px solid #128c7e;'>
                    <h3 style='margin:0 0 10px 0; font-family:sans-serif; color:#128c7e;'>SCAN TO CONNECT</h3>
                    <pre style='font-size:7px; line-height:1; margin:0; font-family:monospace; font-weight:bold;'>{ascii_qr}</pre>
                    <p style='margin-top:10px; font-size:12px; color:#555;'>Open WhatsApp > Linked Devices > Link a Device</p>
                </div>
                """
        except:
            pass
    elif status.get('ready'):
        qr_html = """
        <div style='text-align:center; padding:40px; color:#4caf50; background:#f0f9f0; border-radius:12px; border:2px dashed #4caf50;'>
            <h2 style='margin:0;'>βœ… CONNECTED</h2>
            <p>Ready to send polls and messages.</p>
        </div>
        """
        
    logs = "\n".join(status.get('logs', []))
    
    screenshot_path = None
    try:
        sc_r = requests.get(f"{NODE_URL}/screenshot", timeout=5)
        if sc_r.status_code == 200:
            screenshot_path = "current_screen.png"
            with open(screenshot_path, "wb") as f:
                f.write(sc_r.content)
    except:
        pass
    
    return info_text, qr_html, logs, screenshot_path

def trigger_init():
    try:
        requests.post(f"{NODE_URL}/init")
        return "Init sequence started..."
    except Exception as e:
        return f"Error: {str(e)}"

def clear_session():
    try:
        requests.post(f"{NODE_URL}/clear-session")
        return "Session cleared. Please click Start."
    except Exception as e:
        return f"Error: {str(e)}"

def send_poll(number, title, options):
    try:
        opts = [o.strip() for o in options.split(",")]
        r = requests.post(f"{NODE_URL}/send-poll", json={"telnumber": number, "name": title, "choices": opts})
        return r.json()
    except Exception as e:
        return {"error": str(e)}

with gr.Blocks(title="WPPConnect Dashboard") as demo:
    gr.Markdown("# πŸ€– WPPConnect WhatsApp Hub")
    
    with gr.Row():
        with gr.Column(scale=1):
            status_display = gr.Markdown("Initializing...")
            with gr.Row():
                init_btn = gr.Button("πŸš€ Start", variant="primary")
                refresh_btn = gr.Button("πŸ”„ Refresh")
            clear_btn = gr.Button("πŸ—‘οΈ Reset", variant="stop")
        
        with gr.Column(scale=2):
            qr_display = gr.HTML(label="QR Code")
    
    with gr.Tabs():
        with gr.Tab("πŸ“Š Poll Automation"):
            with gr.Row():
                poll_num = gr.Textbox(label="Target Number", placeholder="e.g. 5511999999999")
                poll_title = gr.Textbox(label="Poll Name")
            poll_opts = gr.Textbox(label="Options (comma separated)")
            poll_send = gr.Button("Send Poll Message", variant="primary")
            poll_output = gr.JSON(label="Response")
            
        with gr.Tab("πŸ“‹ Debug Logs"):
            log_display = gr.Code(label="Internal Logs", lines=15, language="markdown")
            
        with gr.Tab("πŸ” Browser View"):
            gr.Markdown("Direct view of the headless browser state.")
            screenshot_display = gr.Image(label="Live View")

    init_btn.click(trigger_init, outputs=status_display)
    clear_btn.click(clear_session, outputs=status_display)
    refresh_btn.click(update_ui, outputs=[status_display, qr_display, log_display, screenshot_display])
    poll_send.click(send_poll, inputs=[poll_num, poll_title, poll_opts], outputs=poll_output)
    
    try:
        demo.load(update_ui, outputs=[status_display, qr_display, log_display, screenshot_display], every=10)
    except:
        demo.load(update_ui, outputs=[status_display, qr_display, log_display, screenshot_display])

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft(primary_hue="green"))