ApolloAIs commited on
Commit
3cf170d
·
verified ·
1 Parent(s): 6890e9b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +11 -17
main.py CHANGED
@@ -11,13 +11,11 @@ TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}"
11
 
12
  app = FastAPI()
13
 
14
- system_prompt = "You are Phoebus AI. Keep replies concise and use Markdown."
15
-
16
  async def run_agent(prompt: str) -> str:
17
  try:
18
  response = await acompletion(
19
- model="gemini/gemini-1.5-pro",
20
- messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": prompt}],
21
  temperature=0.2
22
  )
23
  return response.choices[0].message.content or "No response."
@@ -27,28 +25,24 @@ async def run_agent(prompt: str) -> str:
27
  async def process_and_reply(chat_id: int, text: str):
28
  response_text = await run_agent(text)
29
  async with httpx.AsyncClient() as client:
30
- await client.post(f"{TELEGRAM_API_URL}/sendMessage", json={"chat_id": chat_id, "text": response_text, "parse_mode": "Markdown"})
31
-
32
- @app.get("/health")
33
- async def health_check():
34
- return {"status": "alive"}
35
 
36
- # Listen on BOTH root and /webhook to be safe
37
- @app.post("/")
38
- @app.post("/webhook")
39
  async def telegram_webhook(request: Request, background_tasks: BackgroundTasks):
 
 
 
 
40
  try:
41
  data = await request.json()
42
- print(f"DEBUG RECEIVE: {data}") # This will show up in your HF logs!
43
-
44
  if "message" in data and "text" in data["message"]:
45
  chat_id = data["message"]["chat"]["id"]
46
  user_msg = data["message"]["text"]
47
  background_tasks.add_task(process_and_reply, chat_id, user_msg)
48
  return {"status": "ok"}
49
- except Exception as e:
50
- print(f"DEBUG ERROR: {e}")
51
- return {"status": "error"}
52
 
53
  if __name__ == "__main__":
54
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
11
 
12
  app = FastAPI()
13
 
 
 
14
  async def run_agent(prompt: str) -> str:
15
  try:
16
  response = await acompletion(
17
+ model="gemini/1.5-pro",
18
+ messages=[{"role": "user", "content": prompt}],
19
  temperature=0.2
20
  )
21
  return response.choices[0].message.content or "No response."
 
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(f"{TELEGRAM_API_URL}/sendMessage", json={"chat_id": chat_id, "text": response_text})
 
 
 
 
29
 
30
+ # Accept EVERY method to prevent "Method Not Allowed"
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 you testing in a browser), just say hi
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)