Update main.py
Browse files
main.py
CHANGED
|
@@ -11,38 +11,59 @@ TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}"
|
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
|
|
|
| 14 |
async def run_agent(prompt: str) -> str:
|
| 15 |
try:
|
| 16 |
response = await acompletion(
|
| 17 |
-
model="gemini/
|
| 18 |
messages=[{"role": "user", "content": prompt}],
|
| 19 |
temperature=0.2
|
| 20 |
)
|
| 21 |
-
return response.choices[0].message.content or "No response."
|
| 22 |
except Exception as e:
|
| 23 |
return f"⚠️ Engine Error: {str(e)}"
|
| 24 |
|
|
|
|
| 25 |
async def process_and_reply(chat_id: int, text: str):
|
| 26 |
response_text = await run_agent(text)
|
|
|
|
|
|
|
| 27 |
async with httpx.AsyncClient() as client:
|
| 28 |
-
await client.post(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
@app.api_route("/webhook", methods=["GET", "POST", "PUT"])
|
| 32 |
async def telegram_webhook(request: Request, background_tasks: BackgroundTasks):
|
| 33 |
-
# If it's a GET request (like
|
| 34 |
if request.method == "GET":
|
| 35 |
return {"status": "Webhook is active. Send a POST request from Telegram!"}
|
| 36 |
|
| 37 |
try:
|
| 38 |
data = await request.json()
|
|
|
|
|
|
|
| 39 |
if "message" in data and "text" in data["message"]:
|
| 40 |
chat_id = data["message"]["chat"]["id"]
|
| 41 |
user_msg = data["message"]["text"]
|
|
|
|
|
|
|
| 42 |
background_tasks.add_task(process_and_reply, chat_id, user_msg)
|
|
|
|
|
|
|
| 43 |
return {"status": "ok"}
|
| 44 |
-
except:
|
| 45 |
-
return {"status": "error reading json"}
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 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="gemini/gemini-2.5-pro", # Locked onto 2.5 Pro
|
| 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 |
response_text = await run_agent(text)
|
| 29 |
+
|
| 30 |
+
# Send the final response back to Telegram
|
| 31 |
async with httpx.AsyncClient() as client:
|
| 32 |
+
await client.post(
|
| 33 |
+
f"{TELEGRAM_API_URL}/sendMessage",
|
| 34 |
+
json={
|
| 35 |
+
"chat_id": chat_id,
|
| 36 |
+
"text": response_text
|
| 37 |
+
}
|
| 38 |
+
)
|
| 39 |
|
| 40 |
+
# --- ENDPOINTS ---
|
| 41 |
+
@app.get("/health")
|
| 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 |
|
| 52 |
try:
|
| 53 |
data = await request.json()
|
| 54 |
+
|
| 55 |
+
# Extract the message from Telegram's JSON payload
|
| 56 |
if "message" in data and "text" in data["message"]:
|
| 57 |
chat_id = data["message"]["chat"]["id"]
|
| 58 |
user_msg = data["message"]["text"]
|
| 59 |
+
|
| 60 |
+
# Run the AI generation in the background so we can instantly reply "OK" to Telegram
|
| 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)}"}
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|