Acytel commited on
Commit
9025b26
Β·
1 Parent(s): c69fb74

Fix adding brain to whatsapp

Browse files
Files changed (1) hide show
  1. whatsapp/webhook.py +26 -6
whatsapp/webhook.py CHANGED
@@ -65,9 +65,6 @@ async def send_whatsapp_message(to: str, text: str):
65
  clean_phone_id = get_secret("WHATSAPP_PHONE_NUMBER_ID")
66
  clean_token = get_secret("WHATSAPP_TOKEN")
67
 
68
- print(f"\nπŸš€ Initiating reply to {to}...")
69
- print(f"πŸ”‘ Keys Loaded -> Phone ID: '{clean_phone_id}' | Token Length: {len(clean_token)}")
70
-
71
  if not clean_phone_id or not clean_token:
72
  print("🚨 FATAL ERROR: API Keys are completely empty. Hugging Face failed to load secrets!")
73
  return
@@ -124,17 +121,40 @@ def check_and_increment_quota() -> bool:
124
 
125
  return True
126
 
 
127
  async def process_and_reply(from_number: str, query: str, language: str):
128
  if not check_and_increment_quota():
129
  await send_whatsapp_message(from_number, "⚠️ Our WhatsApp service has reached its monthly limit. Please visit govbridge.in for full access.")
130
  return
131
 
132
- answer = f"Echoing your RAG query: {query}"
133
- sources = ["Source 1", "Source 2"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  sources_text = ""
136
  if sources:
137
- sources_text = "\n\nπŸ“„ *Sources:*\n" + "\n".join([f"β€’ {s}" for s in sources[:3]])
 
138
 
139
  reply = f"πŸ›οΈ *GovBridge India*\n\n{answer}{sources_text}"
140
  await send_whatsapp_message(from_number, reply)
 
65
  clean_phone_id = get_secret("WHATSAPP_PHONE_NUMBER_ID")
66
  clean_token = get_secret("WHATSAPP_TOKEN")
67
 
 
 
 
68
  if not clean_phone_id or not clean_token:
69
  print("🚨 FATAL ERROR: API Keys are completely empty. Hugging Face failed to load secrets!")
70
  return
 
121
 
122
  return True
123
 
124
+ # ── AI Brain Connection ──────────────────────────────────────────────────────
125
  async def process_and_reply(from_number: str, query: str, language: str):
126
  if not check_and_increment_quota():
127
  await send_whatsapp_message(from_number, "⚠️ Our WhatsApp service has reached its monthly limit. Please visit govbridge.in for full access.")
128
  return
129
 
130
+ print(f"🧠 Querying GovBridge AI: '{query}'")
131
+
132
+ try:
133
+ # Internal API call to your existing RAG pipeline
134
+ async with httpx.AsyncClient(timeout=60.0) as client:
135
+ api_url = "http://127.0.0.1:7860/api/rag/query"
136
+ payload = {"question": query, "language": language}
137
+
138
+ resp = await client.post(api_url, json=payload)
139
+
140
+ if resp.status_code == 200:
141
+ answer = resp.text
142
+ # Extract sources from the headers you built in api.py
143
+ sources_raw = resp.headers.get("X-Sources", "")
144
+ sources = [s for s in sources_raw.split("|") if s.strip()]
145
+ else:
146
+ answer = "My AI brain is temporarily offline for upgrades. Please try again soon!"
147
+ sources = []
148
+
149
+ except Exception as e:
150
+ print(f"❌ Internal AI Connection Error: {e}")
151
+ answer = "I am having a little trouble thinking right now. Please try again in a minute!"
152
+ sources = []
153
 
154
  sources_text = ""
155
  if sources:
156
+ unique_sources = list(dict.fromkeys(sources))
157
+ sources_text = "\n\nπŸ“„ *Sources:*\n" + "\n".join([f"β€’ {s}" for s in unique_sources[:3]])
158
 
159
  reply = f"πŸ›οΈ *GovBridge India*\n\n{answer}{sources_text}"
160
  await send_whatsapp_message(from_number, reply)