Update main.py
Browse files
main.py
CHANGED
|
@@ -1,86 +1,36 @@
|
|
| 1 |
import os
|
| 2 |
import httpx
|
| 3 |
-
from fastapi import FastAPI, Request
|
| 4 |
import uvicorn
|
| 5 |
-
from litellm import acompletion
|
| 6 |
|
| 7 |
# --- CONFIG ---
|
| 8 |
-
os.environ["GEMINI_API_KEY"] = os.getenv("GEMINI_API_KEY", "")
|
| 9 |
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN", "")
|
| 10 |
TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}"
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
-
# --- MEMORY BANK ---
|
| 15 |
-
user_engines = {}
|
| 16 |
-
|
| 17 |
-
# --- NATIVE AGENT ENGINE ---
|
| 18 |
-
async def run_agent(prompt: str, model_name: str) -> str:
|
| 19 |
-
try:
|
| 20 |
-
response = await acompletion(
|
| 21 |
-
model=model_name,
|
| 22 |
-
messages=[{"role": "user", "content": prompt}],
|
| 23 |
-
temperature=0.2
|
| 24 |
-
)
|
| 25 |
-
return response.choices[0].message.content or "No response generated."
|
| 26 |
-
except Exception as e:
|
| 27 |
-
return f"⚠️ Engine Error ({model_name}): {str(e)}"
|
| 28 |
-
|
| 29 |
-
# --- BACKGROUND TASK: PROCESS AND REPLY ---
|
| 30 |
-
async def process_and_reply(chat_id: int, text: str):
|
| 31 |
-
user_msg = text.strip().lower()
|
| 32 |
-
|
| 33 |
-
async with httpx.AsyncClient() as client:
|
| 34 |
-
# 1. IMMEDIATE UX FEEDBACK: Trigger "Phoebus is typing..."
|
| 35 |
-
await client.post(
|
| 36 |
-
f"{TELEGRAM_API_URL}/sendChatAction",
|
| 37 |
-
json={"chat_id": chat_id, "action": "typing"}
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
# 2. SHIFT GEARS
|
| 41 |
-
if user_msg == "/pro":
|
| 42 |
-
user_engines[chat_id] = "gemini/gemini-1.5-pro"
|
| 43 |
-
response_text = "🧠 Engine Switched: Gemini 1.5 Pro\n(50 free requests/day active)."
|
| 44 |
-
elif user_msg == "/flash":
|
| 45 |
-
user_engines[chat_id] = "gemini/gemini-1.5-flash"
|
| 46 |
-
response_text = "⚡ Engine Switched: Gemini 1.5 Flash\n(1,500 free requests/day active)."
|
| 47 |
-
|
| 48 |
-
# 3. NORMAL CHAT
|
| 49 |
-
else:
|
| 50 |
-
current_model = user_engines.get(chat_id, "gemini/gemini-1.5-flash")
|
| 51 |
-
response_text = await run_agent(text, current_model)
|
| 52 |
-
|
| 53 |
-
# 4. SEND REPLY (Removed markdown parser to stop Telegram from crashing)
|
| 54 |
-
try:
|
| 55 |
-
await client.post(
|
| 56 |
-
f"{TELEGRAM_API_URL}/sendMessage",
|
| 57 |
-
json={
|
| 58 |
-
"chat_id": chat_id,
|
| 59 |
-
"text": response_text
|
| 60 |
-
}
|
| 61 |
-
)
|
| 62 |
-
except Exception as e:
|
| 63 |
-
print(f"DEBUG SEND ERROR: {e}")
|
| 64 |
-
|
| 65 |
-
# --- ENDPOINTS ---
|
| 66 |
-
@app.get("/health")
|
| 67 |
-
async def health_check():
|
| 68 |
-
return {"status": "alive"}
|
| 69 |
-
|
| 70 |
@app.api_route("/webhook", methods=["GET", "POST", "PUT"])
|
| 71 |
-
async def telegram_webhook(request: Request
|
| 72 |
-
if request.method == "GET":
|
| 73 |
-
return {"status": "Webhook is active. Send a POST request from Telegram!"}
|
| 74 |
-
|
| 75 |
try:
|
| 76 |
data = await request.json()
|
|
|
|
|
|
|
| 77 |
if "message" in data and "text" in data["message"]:
|
| 78 |
chat_id = data["message"]["chat"]["id"]
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
return {"status": "ok"}
|
| 82 |
except Exception as e:
|
| 83 |
-
|
|
|
|
| 84 |
|
| 85 |
if __name__ == "__main__":
|
| 86 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import httpx
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
import uvicorn
|
|
|
|
| 5 |
|
| 6 |
# --- CONFIG ---
|
|
|
|
| 7 |
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN", "")
|
| 8 |
TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}"
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@app.api_route("/webhook", methods=["GET", "POST", "PUT"])
|
| 13 |
+
async def telegram_webhook(request: Request):
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
data = await request.json()
|
| 16 |
+
print(f"DEBUG 1 (INCOMING): {data}") # Prints what Telegram sent
|
| 17 |
+
|
| 18 |
if "message" in data and "text" in data["message"]:
|
| 19 |
chat_id = data["message"]["chat"]["id"]
|
| 20 |
+
|
| 21 |
+
# Send a hardcoded reply immediately, bypassing all AI
|
| 22 |
+
async with httpx.AsyncClient() as client:
|
| 23 |
+
res = await client.post(
|
| 24 |
+
f"{TELEGRAM_API_URL}/sendMessage",
|
| 25 |
+
json={"chat_id": chat_id, "text": "PONG! The server connection is 100% working."}
|
| 26 |
+
)
|
| 27 |
+
print(f"DEBUG 2 (OUTGOING STATUS): {res.status_code}") # Prints if Telegram accepted it
|
| 28 |
+
print(f"DEBUG 3 (OUTGOING MSG): {res.text}")
|
| 29 |
+
|
| 30 |
return {"status": "ok"}
|
| 31 |
except Exception as e:
|
| 32 |
+
print(f"DEBUG CRASH: {e}")
|
| 33 |
+
return {"status": "error"}
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|