Spaces:
Running
Running
Upload gateway.py with huggingface_hub
Browse files- gateway.py +27 -2
gateway.py
CHANGED
|
@@ -576,14 +576,39 @@ def devos_webhook(payload: DevOSWebhookPayload):
|
|
| 576 |
}
|
| 577 |
|
| 578 |
# -------------------------------------------------------------
|
| 579 |
-
# Telegram
|
| 580 |
# -------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
def start_telegram_daemon():
|
| 582 |
bot_token = os.environ.get("TELEGRAM_BOT_TOKEN", "").strip()
|
| 583 |
if not bot_token:
|
| 584 |
return
|
| 585 |
|
| 586 |
-
print("🤖 TELEGRAM BOT ACTIVATED!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
import telegram_bot
|
| 588 |
t = Thread(target=telegram_bot.run_bot, daemon=True)
|
| 589 |
t.start()
|
|
|
|
| 576 |
}
|
| 577 |
|
| 578 |
# -------------------------------------------------------------
|
| 579 |
+
# Telegram Webhook & Background Daemon
|
| 580 |
# -------------------------------------------------------------
|
| 581 |
+
@app.post("/api/telegram")
|
| 582 |
+
@app.post("/telegram")
|
| 583 |
+
async def telegram_webhook(request: Request):
|
| 584 |
+
bot_token = os.environ.get("TELEGRAM_BOT_TOKEN", "").strip()
|
| 585 |
+
if not bot_token:
|
| 586 |
+
return {"status": "skipped", "reason": "TELEGRAM_BOT_TOKEN not set"}
|
| 587 |
+
try:
|
| 588 |
+
body = await request.json()
|
| 589 |
+
import telegram_bot
|
| 590 |
+
telegram_bot.handle_update(body, bot_token)
|
| 591 |
+
return {"status": "ok"}
|
| 592 |
+
except Exception as e:
|
| 593 |
+
print(f"❌ Telegram Webhook error: {e}", flush=True)
|
| 594 |
+
return {"status": "error", "detail": str(e)}
|
| 595 |
+
|
| 596 |
def start_telegram_daemon():
|
| 597 |
bot_token = os.environ.get("TELEGRAM_BOT_TOKEN", "").strip()
|
| 598 |
if not bot_token:
|
| 599 |
return
|
| 600 |
|
| 601 |
+
print("🤖 TELEGRAM BOT ACTIVATED! Setting up Webhook & Listener...", flush=True)
|
| 602 |
+
webhook_url = "https://professorceo-kora-ai.hf.space/api/telegram"
|
| 603 |
+
try:
|
| 604 |
+
set_url = f"https://api.telegram.org/bot{bot_token}/setWebhook?url={webhook_url}"
|
| 605 |
+
req = urllib.request.Request(set_url)
|
| 606 |
+
with urllib.request.urlopen(req, timeout=10) as resp:
|
| 607 |
+
data = json.loads(resp.read().decode("utf-8"))
|
| 608 |
+
print(f"✅ Telegram Webhook registered to {webhook_url}: {data}", flush=True)
|
| 609 |
+
except Exception as err:
|
| 610 |
+
print(f"⚠️ Telegram Webhook setup warning: {err}", flush=True)
|
| 611 |
+
|
| 612 |
import telegram_bot
|
| 613 |
t = Thread(target=telegram_bot.run_bot, daemon=True)
|
| 614 |
t.start()
|