File size: 2,090 Bytes
8302859
 
 
 
 
139d5af
8302859
b9b0071
 
 
 
 
 
 
 
8302859
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514e98b
 
139d5af
8302859
 
 
 
139d5af
8302859
 
 
139d5af
 
 
 
 
 
 
 
 
b70cebc
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
import os
import shutil
import subprocess
import urllib.request
import tarfile
import gradio as gr

try:
    import spaces
    @spaces.GPU
    def dummy_gpu_fn():
        pass
except Exception:
    pass

def ensure_go():
    if shutil.which("go"):
        print("Go compiler found in system PATH.")
        return "go"
    
    go_bin = os.path.abspath("go_dist/bin/go")
    if os.path.exists(go_bin):
        print(f"Go compiler found at {go_bin}")
        return go_bin

    print("Downloading Go compiler (go1.21.6)...")
    url = "https://go.dev/dl/go1.21.6.linux-amd64.tar.gz"
    tar_path = "/tmp/go.tar.gz"
    urllib.request.urlretrieve(url, tar_path)

    print("Extracting Go...")
    os.makedirs("go_dist", exist_ok=True)
    with tarfile.open(tar_path, "r:gz") as tar:
        tar.extractall(path="go_dist_temp")
    
    if os.path.exists("go_dist_temp/go"):
        shutil.rmtree("go_dist", ignore_errors=True)
        os.rename("go_dist_temp/go", "go_dist")
        shutil.rmtree("go_dist_temp", ignore_errors=True)

    if os.path.exists(tar_path):
        os.remove(tar_path)
        
    return go_bin

# Setup Go environment & PATH
go_cmd = ensure_go()
env = os.environ.copy()
if os.path.exists(os.path.abspath("go_dist/bin")):
    env["PATH"] = os.path.abspath("go_dist/bin") + ":" + env.get("PATH", "")

# Set Go bot internal HTTP port to 8080 to prevent collision with Gradio (7860 & 7861)
env["PORT"] = "8080"

# Build Go Bot binary
print("Building Go-Filter-Bot binary...")
subprocess.run([go_cmd, "build", "-o", "Go-Filter-Bot", "."], check=True, env=env)

# Run Go Bot binary in background
print("Starting Go-Filter-Bot process...")
bot_process = subprocess.Popen(["./Go-Filter-Bot"], env=env)

# Create Gradio interface for HF Space supervisor
with gr.Blocks(title="Telegram Go Filter Bot") as demo:
    gr.Markdown("""
    # 🤖 Telegram Auto-Filter Bot
    ### Status: Online 24/7 🚀
    The bot is active and processing updates in the background.
    """)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)