ApolloAIs commited on
Commit
e4789eb
·
verified ·
1 Parent(s): 7021a1f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +42 -15
main.py CHANGED
@@ -1,35 +1,62 @@
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__":
 
1
  import os
 
2
  from fastapi import FastAPI, Request
3
  import uvicorn
4
+ from litellm import acompletion
5
 
6
+ # Strip the API key to kill any hidden spaces or newlines!
7
+ os.environ["GEMINI_API_KEY"] = os.getenv("GEMINI_API_KEY", "").strip()
 
8
 
9
  app = FastAPI()
10
 
11
+ # --- MEMORY BANK ---
12
+ user_engines = {}
13
+
14
+ async def run_agent(prompt: str, model_name: str) -> str:
15
+ try:
16
+ response = await acompletion(
17
+ model=model_name,
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
  @app.api_route("/webhook", methods=["GET", "POST", "PUT"])
26
  async def telegram_webhook(request: Request):
27
+ if request.method == "GET":
28
+ return {"status": "alive"}
29
+
30
  try:
31
  data = await request.json()
 
32
 
33
  if "message" in data and "text" in data["message"]:
34
  chat_id = data["message"]["chat"]["id"]
35
+ user_msg = data["message"]["text"].strip()
36
+
37
+ # 1. GEAR SHIFTER
38
+ if user_msg.lower() == "/pro":
39
+ user_engines[chat_id] = "gemini/gemini-1.5-pro"
40
+ return {"method": "sendMessage", "chat_id": chat_id, "text": "🧠 Engine Switched: Gemini 1.5 Pro (50 free requests/day)."}
41
+ elif user_msg.lower() == "/flash":
42
+ user_engines[chat_id] = "gemini/gemini-1.5-flash"
43
+ return {"method": "sendMessage", "chat_id": chat_id, "text": "⚡ Engine Switched: Gemini 1.5 Flash (1,500 free requests/day)."}
44
+
45
+ # 2. RUN AI
46
+ current_model = user_engines.get(chat_id, "gemini/gemini-1.5-flash")
47
+ response_text = await run_agent(user_msg, current_model)
48
+
49
+ # 3. DIRECT WEBHOOK REPLY
50
+ # We respond directly to Telegram's knock. No background tasks, no httpx, no crashing.
51
+ return {
52
+ "method": "sendMessage",
53
+ "chat_id": chat_id,
54
+ "text": response_text
55
+ }
56
 
 
 
 
 
 
 
 
 
 
57
  return {"status": "ok"}
58
  except Exception as e:
59
+ print(f"CRASH: {repr(e)}")
60
  return {"status": "error"}
61
 
62
  if __name__ == "__main__":