nvd commited on
Update main.py
Browse files
main.py
CHANGED
|
@@ -9,7 +9,7 @@ from fastapi import FastAPI, Request
|
|
| 9 |
from contextlib import asynccontextmanager
|
| 10 |
import uvicorn
|
| 11 |
|
| 12 |
-
# --- CONFIGURATION
|
| 13 |
TELEGRAM_API_ID = 35190771
|
| 14 |
TELEGRAM_API_HASH = 'a2bc0931f3ee06130ceab419802d5703'
|
| 15 |
BALE_BOT_TOKEN = '736303401:8hhKYE6A0Kg0O7rm_dBDWka8MHCS15aHBcE'
|
|
@@ -17,7 +17,7 @@ BALE_CHAT_ID = '447422307'
|
|
| 17 |
MONGO_URI = 'mongodb+srv://test1:Kjs93&kh39@cluster0.tx7plhr.mongodb.net/?appName=Cluster0'
|
| 18 |
BALE_API_URL = f"https://tapi.bale.ai/bot{BALE_BOT_TOKEN}"
|
| 19 |
|
| 20 |
-
# Target Channels
|
| 21 |
TARGET_CHANNELS = ['IranintlTV', 'vahidonline', 'Indypersian', 'mamlekate', 'nvd13']
|
| 22 |
|
| 23 |
# PGP Public Key
|
|
@@ -25,57 +25,57 @@ PUB_KEY_TEXT = """-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
| 25 |
Version: Mailvelope v6.2.1
|
| 26 |
Comment: https://mailvelope.com
|
| 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 |
-----END PGP PUBLIC KEY BLOCK-----"""
|
| 80 |
|
| 81 |
# --- INITIALIZE SERVICES ---
|
|
@@ -99,100 +99,55 @@ def send_to_bale(text, show_keyboard=False):
|
|
| 99 |
}
|
| 100 |
try:
|
| 101 |
requests.post(f"{BALE_API_URL}/sendMessage", json=payload, timeout=8)
|
| 102 |
-
except:
|
| 103 |
-
|
| 104 |
|
| 105 |
-
# ---
|
| 106 |
@tg_client.on(events.NewMessage(chats=TARGET_CHANNELS))
|
| 107 |
async def telegram_handler(event):
|
| 108 |
try:
|
| 109 |
raw_text = event.raw_text
|
| 110 |
-
# Telethon sometimes returns the ID, let's make sure we get the username correctly
|
| 111 |
chat = await event.get_chat()
|
| 112 |
channel_name = getattr(chat, 'username', 'Unknown')
|
| 113 |
|
| 114 |
-
print(f"📥
|
| 115 |
|
| 116 |
formatted_text = f"📢 Channel: @{channel_name}\n⏰ {datetime.now().strftime('%H:%M')}\n\n{raw_text}"
|
| 117 |
encrypted = encrypt_message(formatted_text)
|
| 118 |
|
| 119 |
-
#
|
| 120 |
-
|
| 121 |
"channel": channel_name,
|
| 122 |
"encrypted_text": encrypted,
|
| 123 |
"timestamp": datetime.utcnow()
|
| 124 |
-
}
|
| 125 |
-
result = messages_collection.insert_one(doc)
|
| 126 |
-
print(f"✅ Saved to MongoDB. ID: {result.inserted_id}") # DEBUG LOG
|
| 127 |
|
| 128 |
-
#
|
| 129 |
send_to_bale(encrypted, show_keyboard=True)
|
| 130 |
-
print(
|
| 131 |
-
|
| 132 |
-
except Exception as e:
|
| 133 |
-
print(f"❌ Error in Telegram Handler: {e}")
|
| 134 |
-
|
| 135 |
-
# --- UPDATED WEBHOOK ---
|
| 136 |
-
@app.post("/bale-webhook")
|
| 137 |
-
async def bale_webhook(request: Request):
|
| 138 |
-
try:
|
| 139 |
-
data = await request.json()
|
| 140 |
-
if "message" in data and "text" in data["message"]:
|
| 141 |
-
command = data["message"]["text"]
|
| 142 |
-
now = datetime.utcnow()
|
| 143 |
-
|
| 144 |
-
print(f"🕹️ User clicked: {command}") # DEBUG LOG
|
| 145 |
-
|
| 146 |
-
if "Today" in command:
|
| 147 |
-
# Set start to the very beginning of today (UTC)
|
| 148 |
-
start = now.replace(hour=0, minute=0, second=0, microsecond=0)
|
| 149 |
-
end = now + timedelta(hours=1) # Buffer
|
| 150 |
-
elif "Yesterday" in command:
|
| 151 |
-
start = (now - timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
|
| 152 |
-
end = start + timedelta(days=1)
|
| 153 |
-
else:
|
| 154 |
-
send_to_bale(encrypt_message("Menu updated."), show_keyboard=True)
|
| 155 |
-
return {"ok": True}
|
| 156 |
-
|
| 157 |
-
print(f"🔍 Searching DB from {start} to {end}") # DEBUG LOG
|
| 158 |
|
| 159 |
-
results = list(messages_collection.find({"timestamp": {"$gte": start, "$lt": end}}))
|
| 160 |
-
print(f"📊 Found {len(results)} messages in database.") # DEBUG LOG
|
| 161 |
-
|
| 162 |
-
if len(results) == 0:
|
| 163 |
-
send_to_bale(encrypt_message("No messages found in database for this period."))
|
| 164 |
-
else:
|
| 165 |
-
for m in results:
|
| 166 |
-
send_to_bale(m["encrypted_text"])
|
| 167 |
-
await asyncio.sleep(0.3)
|
| 168 |
-
|
| 169 |
-
except Exception as e:
|
| 170 |
-
print(f"❌ Webhook Error: {e}")
|
| 171 |
-
return {"ok": True}
|
| 172 |
-
|
| 173 |
-
# Send to Bale
|
| 174 |
-
send_to_bale(encrypted, show_keyboard=True)
|
| 175 |
except Exception as e:
|
| 176 |
-
print(f"
|
| 177 |
|
| 178 |
# --- BACKGROUND TASK ---
|
| 179 |
async def start_telegram():
|
| 180 |
-
print("
|
| 181 |
try:
|
| 182 |
await tg_client.start()
|
| 183 |
-
print("✅
|
| 184 |
await tg_client.run_until_disconnected()
|
| 185 |
except Exception as e:
|
| 186 |
-
print(f"❌
|
| 187 |
|
| 188 |
-
# ---
|
| 189 |
@asynccontextmanager
|
| 190 |
async def lifespan(app: FastAPI):
|
| 191 |
# Register Webhook
|
| 192 |
webhook_url = "https://nvd13-test1.hf.space/bale-webhook"
|
| 193 |
requests.get(f"{BALE_API_URL}/setWebhook?url={webhook_url}")
|
|
|
|
| 194 |
|
| 195 |
-
# Start Telegram
|
| 196 |
asyncio.create_task(start_telegram())
|
| 197 |
yield
|
| 198 |
await tg_client.disconnect()
|
|
@@ -203,50 +158,42 @@ app = FastAPI(lifespan=lifespan)
|
|
| 203 |
async def status():
|
| 204 |
return {"status": "running", "connected": tg_client.is_connected()}
|
| 205 |
|
| 206 |
-
@app.post("/bale-webhook")
|
| 207 |
@app.post("/bale-webhook")
|
| 208 |
async def bale_webhook(request: Request):
|
| 209 |
try:
|
| 210 |
data = await request.json()
|
| 211 |
if "message" in data and "text" in data["message"]:
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
sender_id = str(data["message"]["chat"]["id"])
|
| 215 |
|
| 216 |
-
print(f"
|
| 217 |
-
|
| 218 |
-
# 1. Handle /start or unknown commands by showing the menu
|
| 219 |
-
if user_text == "/start":
|
| 220 |
-
welcome_text = encrypt_message("Welcome! Monitoring is active. Use the buttons to fetch history.")
|
| 221 |
-
send_to_bale(welcome_text, show_keyboard=True)
|
| 222 |
-
return {"ok": True}
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
start = (now - timedelta(days=1)).replace(hour=0, minute=0, second=0)
|
| 231 |
end = start + timedelta(days=1)
|
| 232 |
else:
|
| 233 |
-
# If they type anything else, just send the keyboard back
|
| 234 |
-
send_to_bale(encrypt_message("Menu updated."), show_keyboard=True)
|
| 235 |
return {"ok": True}
|
| 236 |
|
| 237 |
-
|
| 238 |
-
results = messages_collection.find({"timestamp": {"$gte": start, "$lt": end}})
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
except Exception as e:
|
| 249 |
-
print(f"Webhook Error: {e}")
|
| 250 |
return {"ok": True}
|
| 251 |
|
| 252 |
if __name__ == "__main__":
|
|
|
|
| 9 |
from contextlib import asynccontextmanager
|
| 10 |
import uvicorn
|
| 11 |
|
| 12 |
+
# --- CONFIGURATION ---
|
| 13 |
TELEGRAM_API_ID = 35190771
|
| 14 |
TELEGRAM_API_HASH = 'a2bc0931f3ee06130ceab419802d5703'
|
| 15 |
BALE_BOT_TOKEN = '736303401:8hhKYE6A0Kg0O7rm_dBDWka8MHCS15aHBcE'
|
|
|
|
| 17 |
MONGO_URI = 'mongodb+srv://test1:Kjs93&kh39@cluster0.tx7plhr.mongodb.net/?appName=Cluster0'
|
| 18 |
BALE_API_URL = f"https://tapi.bale.ai/bot{BALE_BOT_TOKEN}"
|
| 19 |
|
| 20 |
+
# Target Channels (Verify you have JOINED these on your Telegram account)
|
| 21 |
TARGET_CHANNELS = ['IranintlTV', 'vahidonline', 'Indypersian', 'mamlekate', 'nvd13']
|
| 22 |
|
| 23 |
# PGP Public Key
|
|
|
|
| 25 |
Version: Mailvelope v6.2.1
|
| 26 |
Comment: https://mailvelope.com
|
| 27 |
|
| 28 |
+
xsFNBGh+SDQBEACkKCrIEkqlMrJx8jv3mTpjspOxbszCbZmpGdB0I/4qL14T
|
| 29 |
+
Xvig7WsUSuyviHL9oNnJ8uX8gUsaAUAk/jFcwi8m5KLFnHFfcL6hxcDpe1le
|
| 30 |
+
hFJlUmQpKhyyHnCaFMzp2FtMzGBjCp2NpMGg7ixJU12QE8uKaSTjdN+Pm30W
|
| 31 |
+
RDNnk2T7dfSJJ2E46JkWigR/P9p7M/HzfWBOz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E5
|
| 77 |
+
/kXlE3cLOpFBlL+Q/29qExVOHFaBq073N0oqk+iKlXncLY14AQ==
|
| 78 |
+
=yiS/
|
| 79 |
-----END PGP PUBLIC KEY BLOCK-----"""
|
| 80 |
|
| 81 |
# --- INITIALIZE SERVICES ---
|
|
|
|
| 99 |
}
|
| 100 |
try:
|
| 101 |
requests.post(f"{BALE_API_URL}/sendMessage", json=payload, timeout=8)
|
| 102 |
+
except Exception as e:
|
| 103 |
+
print(f"Bale Send Error: {e}")
|
| 104 |
|
| 105 |
+
# --- TELEGRAM LISTENER ---
|
| 106 |
@tg_client.on(events.NewMessage(chats=TARGET_CHANNELS))
|
| 107 |
async def telegram_handler(event):
|
| 108 |
try:
|
| 109 |
raw_text = event.raw_text
|
|
|
|
| 110 |
chat = await event.get_chat()
|
| 111 |
channel_name = getattr(chat, 'username', 'Unknown')
|
| 112 |
|
| 113 |
+
print(f"📥 MESSAGE RECEIVED from @{channel_name}")
|
| 114 |
|
| 115 |
formatted_text = f"📢 Channel: @{channel_name}\n⏰ {datetime.now().strftime('%H:%M')}\n\n{raw_text}"
|
| 116 |
encrypted = encrypt_message(formatted_text)
|
| 117 |
|
| 118 |
+
# Save with a clean UTC timestamp
|
| 119 |
+
messages_collection.insert_one({
|
| 120 |
"channel": channel_name,
|
| 121 |
"encrypted_text": encrypted,
|
| 122 |
"timestamp": datetime.utcnow()
|
| 123 |
+
})
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
# Instant forward
|
| 126 |
send_to_bale(encrypted, show_keyboard=True)
|
| 127 |
+
print("✅ Message encrypted, saved to DB, and sent to Bale.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
except Exception as e:
|
| 130 |
+
print(f"❌ Telegram Handler Error: {e}")
|
| 131 |
|
| 132 |
# --- BACKGROUND TASK ---
|
| 133 |
async def start_telegram():
|
| 134 |
+
print("Connecting to Telegram...")
|
| 135 |
try:
|
| 136 |
await tg_client.start()
|
| 137 |
+
print("✅ TELEGRAM CONNECTED SUCCESSFULLY")
|
| 138 |
await tg_client.run_until_disconnected()
|
| 139 |
except Exception as e:
|
| 140 |
+
print(f"❌ TELEGRAM CONNECTION FAILED: {e}")
|
| 141 |
|
| 142 |
+
# --- WEB SERVER ---
|
| 143 |
@asynccontextmanager
|
| 144 |
async def lifespan(app: FastAPI):
|
| 145 |
# Register Webhook
|
| 146 |
webhook_url = "https://nvd13-test1.hf.space/bale-webhook"
|
| 147 |
requests.get(f"{BALE_API_URL}/setWebhook?url={webhook_url}")
|
| 148 |
+
print(f"Webhook set to {webhook_url}")
|
| 149 |
|
| 150 |
+
# Start Telegram
|
| 151 |
asyncio.create_task(start_telegram())
|
| 152 |
yield
|
| 153 |
await tg_client.disconnect()
|
|
|
|
| 158 |
async def status():
|
| 159 |
return {"status": "running", "connected": tg_client.is_connected()}
|
| 160 |
|
|
|
|
| 161 |
@app.post("/bale-webhook")
|
| 162 |
async def bale_webhook(request: Request):
|
| 163 |
try:
|
| 164 |
data = await request.json()
|
| 165 |
if "message" in data and "text" in data["message"]:
|
| 166 |
+
command = data["message"]["text"]
|
| 167 |
+
now = datetime.utcnow()
|
|
|
|
| 168 |
|
| 169 |
+
print(f"🕹️ Bale Command: {command}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
if "Today" in command or command == "/start":
|
| 172 |
+
# Search from the start of today UTC
|
| 173 |
+
start = now.replace(hour=0, minute=0, second=0, microsecond=0)
|
| 174 |
+
end = now + timedelta(hours=2) # Buffer
|
| 175 |
+
elif "Yesterday" in command:
|
| 176 |
+
start = (now - timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
|
|
|
|
| 177 |
end = start + timedelta(days=1)
|
| 178 |
else:
|
|
|
|
|
|
|
| 179 |
return {"ok": True}
|
| 180 |
|
| 181 |
+
print(f"🔍 Searching DB between {start} and {end}...")
|
| 182 |
+
results = list(messages_collection.find({"timestamp": {"$gte": start, "$lt": end}}))
|
| 183 |
+
print(f"📊 Found {len(results)} messages.")
|
| 184 |
+
|
| 185 |
+
if len(results) == 0:
|
| 186 |
+
if command != "/start":
|
| 187 |
+
send_to_bale(encrypt_message("No messages found for this period yet."))
|
| 188 |
+
else:
|
| 189 |
+
send_to_bale(encrypt_message("System Active. Waiting for new messages..."), show_keyboard=True)
|
| 190 |
+
else:
|
| 191 |
+
for msg in results:
|
| 192 |
+
send_to_bale(msg["encrypted_text"])
|
| 193 |
+
await asyncio.sleep(0.3)
|
| 194 |
|
| 195 |
except Exception as e:
|
| 196 |
+
print(f"❌ Webhook Error: {e}")
|
| 197 |
return {"ok": True}
|
| 198 |
|
| 199 |
if __name__ == "__main__":
|