File size: 2,103 Bytes
ef82a42
 
 
 
 
 
 
 
180f719
ef82a42
 
 
 
 
 
 
180f719
ef82a42
 
 
 
 
 
 
 
 
180f719
 
ef82a42
 
 
 
 
 
 
 
 
 
180f719
ef82a42
 
180f719
ef82a42
180f719
ef82a42
 
 
 
 
 
 
 
 
 
 
 
 
180f719
 
ef82a42
 
180f719
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
import os
import asyncio
import threading
from flask import Flask
from telethon import TelegramClient, events, errors
from telethon.sessions import StringSession

# --- 1. STAY-AWAKE WEB SERVER ---
# This page is for UptimeRobot/BetterStack to ping 24/7.
app = Flask(__name__)

@app.route('/')
def home():
    return "Status: Userbot is Active and Monitoring 24/7", 200

def run_web_server():
    # Hugging Face Spaces use port 7860
    app.run(host='0.0.0.0', port=7860)

# --- 2. TELEGRAM INSTANT FORWARDER ---
# Values are pulled from your "Secrets" in the Settings tab
API_ID = int(os.environ.get("API_ID"))
API_HASH = os.environ.get("API_HASH")
STRING_SESSION = os.environ.get("STRING_SESSION")
TARGET_ID = int(os.environ.get("TARGET_ID"))

# StringSession(STRING_SESSION) ensures it logs in using your code, not a file.
# connection_retries=None ensures it never gives up if the internet drops.
client = TelegramClient(
    StringSession(STRING_SESSION), 
    API_ID, 
    API_HASH, 
    connection_retries=None
)

@client.on(events.NewMessage(from_users=TARGET_ID, incoming=True))
async def instant_forwarder(event):
    try:
        # Copies the message to your 'Saved Messages' immediately.
        # This catches it before the sender can delete it.
        await event.forward_to('me')
        print(f"Captured: Message from {TARGET_ID} forwarded successfully.")
    except errors.FloodWaitError as e:
        # If Telegram tells us to slow down, we wait and continue.
        await asyncio.sleep(e.seconds)
    except Exception as e:
        print(f"Error during forwarding: {e}")

# --- 3. STARTING THE BOT ---
if __name__ == "__main__":
    # Start the web server in a background thread
    threading.Thread(target=run_web_server, daemon=True).start()
    
    print("Userbot is connecting to Telegram...")
    try:
        client.start()
        print("--- SUCCESS: Bot is now online and listening! ---")
        # This is the correct method to keep the bot running non-stop
        client.run_until_disconnected()
    except Exception as e:
        print(f"Failed to start client: {e}")