ApolloAIs commited on
Commit
09c46e5
·
verified ·
1 Parent(s): 46241c8

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +28 -28
main.py CHANGED
@@ -12,7 +12,6 @@ TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}"
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 ---
@@ -31,31 +30,37 @@ async def run_agent(prompt: str, model_name: str) -> str:
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:
 
51
  await client.post(
52
- f"{TELEGRAM_API_URL}/sendMessage",
53
- json={
54
- "chat_id": chat_id,
55
- "text": response_text,
56
- "parse_mode": "Markdown"
57
- }
58
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  # --- ENDPOINTS ---
61
  @app.get("/health")
@@ -69,15 +74,10 @@ async def telegram_webhook(request: Request, background_tasks: BackgroundTasks):
69
 
70
  try:
71
  data = await request.json()
72
-
73
- # Extract the message from Telegram's JSON payload
74
  if "message" in data and "text" in data["message"]:
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)}"}
 
12
  app = FastAPI()
13
 
14
  # --- MEMORY BANK ---
 
15
  user_engines = {}
16
 
17
  # --- NATIVE AGENT ENGINE ---
 
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")
 
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
  user_msg = data["message"]["text"]
 
 
80
  background_tasks.add_task(process_and_reply, chat_id, user_msg)
 
81
  return {"status": "ok"}
82
  except Exception as e:
83
  return {"status": f"error reading json: {str(e)}"}