raghavNCI commited on
Commit
658ffaf
·
1 Parent(s): 913ccc3

another try

Browse files
Files changed (2) hide show
  1. routes/question.py +2 -1
  2. routes/wa_gateway.py +20 -6
routes/question.py CHANGED
@@ -79,7 +79,8 @@ async def ask_question(input: QuestionInput):
79
 
80
  return {
81
  "question": question,
82
- "answer": "Here are today’s top headlines:\n"+all_headlines
 
83
  }
84
 
85
  # 2️⃣ Keyword‑based flow for other intents
 
79
 
80
  return {
81
  "question": question,
82
+ "answer": "Here are today’s top headlines:",
83
+ "headlines": all_headlines,
84
  }
85
 
86
  # 2️⃣ Keyword‑based flow for other intents
routes/wa_gateway.py CHANGED
@@ -42,19 +42,33 @@ def send_whatsapp_text(to: str, text: str) -> dict:
42
  async def nuse_interact(to: str, name: str, prompt: str) -> dict:
43
  """
44
  1. Call ask_question() directly (no HTTP round-trip);
45
- 2. Extract the answer text (+sources if you like);
46
- 3. Push it back to the user via WhatsApp API.
47
  """
48
- # 1) Ask the news assistant
49
  qa_result = await ask_question(QuestionInput(question=prompt))
50
  answer_text = qa_result["answer"]
51
 
52
- # Optional: include sources as bullet list
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  if qa_result.get("sources"):
54
- bullet_list = "\n".join(f"• {s['title']}" for s in qa_result["sources"])
55
  answer_text = f"{answer_text}\n\nSources:\n{bullet_list}"
56
 
57
- # 2) Send to WhatsApp
58
  return send_whatsapp_text(to, answer_text)
59
 
60
  @wa_router.get("/webhook")
 
42
  async def nuse_interact(to: str, name: str, prompt: str) -> dict:
43
  """
44
  1. Call ask_question() directly (no HTTP round-trip);
45
+ 2. Extract the answer text (+sources or headlines);
46
+ 3. Push it back to the user via WhatsApp API using link previews.
47
  """
 
48
  qa_result = await ask_question(QuestionInput(question=prompt))
49
  answer_text = qa_result["answer"]
50
 
51
+ # If headlines were returned (qid == 13)
52
+ if "headlines" in qa_result:
53
+ headlines = qa_result["headlines"]
54
+ if not headlines:
55
+ return send_whatsapp_text(to, "Sorry, no headlines found today.")
56
+
57
+ # Build preview message
58
+ preview_msgs = []
59
+ for h in headlines:
60
+ preview_msgs.append(
61
+ f"🗞️ *{h['title']}* ({h.get('category', '').title()})\n{h['summary']}\n🔗 {h['url']}"
62
+ )
63
+
64
+ message = f"{answer_text}\n\n" + "\n\n".join(preview_msgs[:10]) # show up to 10
65
+ return send_whatsapp_text(to, message)
66
+
67
+ # Fallback: regular Q&A with optional sources
68
  if qa_result.get("sources"):
69
+ bullet_list = "\n".join(f"• {s['title']} – {s['url']}" for s in qa_result["sources"])
70
  answer_text = f"{answer_text}\n\nSources:\n{bullet_list}"
71
 
 
72
  return send_whatsapp_text(to, answer_text)
73
 
74
  @wa_router.get("/webhook")