Update main.py
Browse files
main.py
CHANGED
|
@@ -11,21 +11,40 @@ TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}"
|
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# --- NATIVE AGENT ENGINE ---
|
| 15 |
-
async def run_agent(prompt: str) -> str:
|
| 16 |
try:
|
| 17 |
response = await acompletion(
|
| 18 |
-
model=
|
| 19 |
messages=[{"role": "user", "content": prompt}],
|
| 20 |
temperature=0.2
|
| 21 |
)
|
| 22 |
return response.choices[0].message.content or "No response generated."
|
| 23 |
except Exception as e:
|
| 24 |
-
return f"⚠️ Engine Error: {str(e)}"
|
| 25 |
|
| 26 |
# --- BACKGROUND TASK: PROCESS AND REPLY ---
|
| 27 |
async def process_and_reply(chat_id: int, text: str):
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Send the final response back to Telegram
|
| 31 |
async with httpx.AsyncClient() as client:
|
|
@@ -33,7 +52,8 @@ async def process_and_reply(chat_id: int, text: str):
|
|
| 33 |
f"{TELEGRAM_API_URL}/sendMessage",
|
| 34 |
json={
|
| 35 |
"chat_id": chat_id,
|
| 36 |
-
"text": response_text
|
|
|
|
| 37 |
}
|
| 38 |
)
|
| 39 |
|
|
@@ -42,10 +62,8 @@ async def process_and_reply(chat_id: int, text: str):
|
|
| 42 |
async def health_check():
|
| 43 |
return {"status": "alive"}
|
| 44 |
|
| 45 |
-
# Accept EVERY method to prevent "Method Not Allowed" from HF routing
|
| 46 |
@app.api_route("/webhook", methods=["GET", "POST", "PUT"])
|
| 47 |
async def telegram_webhook(request: Request, background_tasks: BackgroundTasks):
|
| 48 |
-
# If it's a GET request (like a browser test), just confirm it's alive
|
| 49 |
if request.method == "GET":
|
| 50 |
return {"status": "Webhook is active. Send a POST request from Telegram!"}
|
| 51 |
|
|
@@ -57,10 +75,9 @@ async def telegram_webhook(request: Request, background_tasks: BackgroundTasks):
|
|
| 57 |
chat_id = data["message"]["chat"]["id"]
|
| 58 |
user_msg = data["message"]["text"]
|
| 59 |
|
| 60 |
-
# Run
|
| 61 |
background_tasks.add_task(process_and_reply, chat_id, user_msg)
|
| 62 |
|
| 63 |
-
# Telegram requires an immediate 200 OK so it doesn't get stuck in a retry loop
|
| 64 |
return {"status": "ok"}
|
| 65 |
except Exception as e:
|
| 66 |
return {"status": f"error reading json: {str(e)}"}
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
+
# --- MEMORY BANK ---
|
| 15 |
+
# This dictionary remembers which engine you are currently using.
|
| 16 |
+
user_engines = {}
|
| 17 |
+
|
| 18 |
# --- NATIVE AGENT ENGINE ---
|
| 19 |
+
async def run_agent(prompt: str, model_name: str) -> str:
|
| 20 |
try:
|
| 21 |
response = await acompletion(
|
| 22 |
+
model=model_name,
|
| 23 |
messages=[{"role": "user", "content": prompt}],
|
| 24 |
temperature=0.2
|
| 25 |
)
|
| 26 |
return response.choices[0].message.content or "No response generated."
|
| 27 |
except Exception as e:
|
| 28 |
+
return f"⚠️ Engine Error ({model_name}): {str(e)}"
|
| 29 |
|
| 30 |
# --- BACKGROUND TASK: PROCESS AND REPLY ---
|
| 31 |
async def process_and_reply(chat_id: int, text: str):
|
| 32 |
+
user_msg = text.strip().lower()
|
| 33 |
+
|
| 34 |
+
# 1. Check if the user is pulling the Gear Shifter (Slash Commands)
|
| 35 |
+
if user_msg == "/pro":
|
| 36 |
+
user_engines[chat_id] = "gemini/gemini-1.5-pro"
|
| 37 |
+
response_text = "🧠 **Engine Switched: Gemini 1.5 Pro**\n_You are now using the heavy-duty coding engine (50 free requests/day)._"
|
| 38 |
+
|
| 39 |
+
elif user_msg == "/flash":
|
| 40 |
+
user_engines[chat_id] = "gemini/gemini-1.5-flash"
|
| 41 |
+
response_text = "⚡ **Engine Switched: Gemini 1.5 Flash**\n_You are now using the ultra-fast casual engine (1,500 free requests/day)._"
|
| 42 |
+
|
| 43 |
+
# 2. If it's a normal message, route it to the currently active engine
|
| 44 |
+
else:
|
| 45 |
+
# Default to Flash if we haven't set an engine yet to protect the Pro quota
|
| 46 |
+
current_model = user_engines.get(chat_id, "gemini/gemini-1.5-flash")
|
| 47 |
+
response_text = await run_agent(text, current_model)
|
| 48 |
|
| 49 |
# Send the final response back to Telegram
|
| 50 |
async with httpx.AsyncClient() as client:
|
|
|
|
| 52 |
f"{TELEGRAM_API_URL}/sendMessage",
|
| 53 |
json={
|
| 54 |
"chat_id": chat_id,
|
| 55 |
+
"text": response_text,
|
| 56 |
+
"parse_mode": "Markdown"
|
| 57 |
}
|
| 58 |
)
|
| 59 |
|
|
|
|
| 62 |
async def health_check():
|
| 63 |
return {"status": "alive"}
|
| 64 |
|
|
|
|
| 65 |
@app.api_route("/webhook", methods=["GET", "POST", "PUT"])
|
| 66 |
async def telegram_webhook(request: Request, background_tasks: BackgroundTasks):
|
|
|
|
| 67 |
if request.method == "GET":
|
| 68 |
return {"status": "Webhook is active. Send a POST request from Telegram!"}
|
| 69 |
|
|
|
|
| 75 |
chat_id = data["message"]["chat"]["id"]
|
| 76 |
user_msg = data["message"]["text"]
|
| 77 |
|
| 78 |
+
# Run in the background
|
| 79 |
background_tasks.add_task(process_and_reply, chat_id, user_msg)
|
| 80 |
|
|
|
|
| 81 |
return {"status": "ok"}
|
| 82 |
except Exception as e:
|
| 83 |
return {"status": f"error reading json: {str(e)}"}
|