feat(bot): comprehensive natural language command parsing

- Enhanced AI prompts with time/frequency conversion rules and 20+ examples
- Added smart medication name resolution and confirmation flows
- New medication commands: today, refills, snooze, adherence
- New routine commands: create_with_steps, add_steps, steps, schedule
- Added active session awareness with shortcuts (done, skip, pause, resume)
- Confirmation handling for destructive/create actions
- Improved help message with natural language examples
This commit is contained in:
2026-02-16 05:53:40 -06:00
parent 16d89d07f6
commit c903363f6e
4 changed files with 700 additions and 126 deletions

View File

@@ -32,58 +32,59 @@ async def handle_medication(message, session, parsed):
times = parsed.get("times", ["08:00"])
days_of_week = parsed.get("days_of_week", [])
interval_days = parsed.get("interval_days")
needs_confirmation = parsed.get("needs_confirmation", False)
confirmation_prompt = parsed.get("confirmation_prompt")
if not name or not dosage:
await message.channel.send("Please provide medication name and dosage.")
return
data = {
"name": name,
"dosage": dosage,
"unit": unit,
"frequency": frequency,
"times": times
}
# Add optional fields if present
if days_of_week:
data["days_of_week"] = days_of_week
if interval_days:
data["interval_days"] = interval_days
# Handle confirmation
if needs_confirmation:
# Store pending action in session for confirmation
if "pending_confirmations" not in session:
session["pending_confirmations"] = {}
resp, status = api_request("post", "/api/medications", token, data)
if status == 201:
await message.channel.send(f"Added **{name}** to your medications!")
else:
await message.channel.send(f"Error: {resp.get('error', 'Failed to add medication')}")
confirmation_id = f"med_add_{name}"
session["pending_confirmations"][confirmation_id] = {
"action": "add",
"name": name,
"dosage": dosage,
"unit": unit,
"frequency": frequency,
"times": times,
"days_of_week": days_of_week,
"interval_days": interval_days
}
schedule_desc = _format_schedule(frequency, times, days_of_week, interval_days)
await message.channel.send(
f"{confirmation_prompt}\n\n"
f"**Details:**\n"
f"- Name: {name}\n"
f"- Dosage: {dosage} {unit}\n"
f"- Schedule: {schedule_desc}\n\n"
f"Reply **yes** to confirm, or **no** to cancel."
)
return
await _add_medication(message, token, name, dosage, unit, frequency, times, days_of_week, interval_days)
elif action == "take":
med_id = parsed.get("medication_id")
name = parsed.get("name")
if not med_id and name:
# Look up medication by name
resp, status = api_request("get", "/api/medications", token)
if status == 200:
meds = resp if isinstance(resp, list) else []
# Find medication by name (case-insensitive partial match)
matching = [m for m in meds if name.lower() in m['name'].lower() or m['name'].lower() in name.lower()]
if matching:
med_id = matching[0]['id']
name = matching[0]['name']
else:
await message.channel.send(f"I couldn't find a medication called '{name}'. Use 'list' to see your medications.")
return
else:
await message.channel.send("Error looking up medication. Please try again.")
return
elif not med_id:
med_id, name, found = await _find_medication_by_name(message, token, med_id, name)
if not found:
return
if not med_id:
await message.channel.send("Which medication did you take?")
return
resp, status = api_request("post", f"/api/medications/{med_id}/take", token, {})
if status == 201:
await message.channel.send("Logged it! Great job staying on track.")
await message.channel.send(f"Logged **{name}**! Great job staying on track.")
else:
await message.channel.send(f"Error: {resp.get('error', 'Failed to log')}")
@@ -92,49 +93,225 @@ async def handle_medication(message, session, parsed):
name = parsed.get("name")
reason = parsed.get("reason", "Skipped by user")
if not med_id and name:
# Look up medication by name
resp, status = api_request("get", "/api/medications", token)
if status == 200:
meds = resp if isinstance(resp, list) else []
# Find medication by name (case-insensitive partial match)
matching = [m for m in meds if name.lower() in m['name'].lower() or m['name'].lower() in name.lower()]
if matching:
med_id = matching[0]['id']
name = matching[0]['name']
else:
await message.channel.send(f"I couldn't find a medication called '{name}'. Use 'list' to see your medications.")
return
else:
await message.channel.send("Error looking up medication. Please try again.")
return
elif not med_id:
med_id, name, found = await _find_medication_by_name(message, token, med_id, name)
if not found:
return
if not med_id:
await message.channel.send("Which medication are you skipping?")
return
resp, status = api_request("post", f"/api/medications/{med_id}/skip", token, {"reason": reason})
if status == 201:
await message.channel.send("OK, noted.")
await message.channel.send(f"Skipped **{name}**. {reason}")
else:
await message.channel.send(f"Error: {resp.get('error', 'Failed to log')}")
elif action == "today":
resp, status = api_request("get", "/api/medications/today", token)
if status == 200:
meds = resp if isinstance(resp, list) else []
if not meds:
await message.channel.send("No medications scheduled for today!")
else:
lines = []
for item in meds:
med = item.get("medication", {})
times = item.get("scheduled_times", [])
taken = item.get("taken_times", [])
skipped = item.get("skipped_times", [])
is_prn = item.get("is_prn", False)
med_name = med.get("name", "Unknown")
dosage = f"{med.get('dosage', '')} {med.get('unit', '')}".strip()
if is_prn:
status_icon = "💊"
lines.append(f"{status_icon} **{med_name}** {dosage} (as needed)")
elif times:
for time in times:
if time in taken:
status_icon = ""
elif time in skipped:
status_icon = "⏭️"
else:
status_icon = ""
lines.append(f"{status_icon} **{med_name}** {dosage} at {time}")
else:
lines.append(f"💊 **{med_name}** {dosage}")
await message.channel.send("**Today's Medications:**\n" + "\n".join(lines))
else:
await message.channel.send(f"Error: {resp.get('error', 'Failed to fetch today\'s schedule')}")
elif action == "refills":
resp, status = api_request("get", "/api/medications/refills-due", token)
if status == 200:
meds = resp if isinstance(resp, list) else []
if not meds:
await message.channel.send("No refills due soon! 🎉")
else:
lines = []
for med in meds:
name = med.get("name", "Unknown")
qty = med.get("quantity_remaining")
refill_date = med.get("refill_date")
if qty is not None and qty <= 7:
lines.append(f"⚠️ **{name}**: Only {qty} remaining")
elif refill_date:
lines.append(f"📅 **{name}**: Refill due by {refill_date}")
await message.channel.send("**Refills Due:**\n" + "\n".join(lines))
else:
await message.channel.send(f"Error: {resp.get('error', 'Failed to fetch refills')}")
elif action == "snooze":
med_id = parsed.get("medication_id")
name = parsed.get("name")
minutes = parsed.get("minutes", 15)
med_id, name, found = await _find_medication_by_name(message, token, med_id, name)
if not found:
return
if not med_id:
await message.channel.send("Which medication reminder should I snooze?")
return
resp, status = api_request("post", f"/api/medications/{med_id}/snooze", token, {"minutes": minutes})
if status == 200:
await message.channel.send(f"⏰ Snoozed **{name}** for {minutes} minutes.")
else:
await message.channel.send(f"Error: {resp.get('error', 'Failed to snooze')}")
elif action == "adherence":
med_id = parsed.get("medication_id")
name = parsed.get("name")
if name and not med_id:
# Look up by name first
med_id, name, found = await _find_medication_by_name(message, token, None, name)
if not found:
return
if med_id:
resp, status = api_request("get", f"/api/medications/{med_id}/adherence", token)
else:
resp, status = api_request("get", "/api/medications/adherence", token)
if status == 200:
if isinstance(resp, list):
lines = [f"- {m['name']}: {m['adherence_percent']}% adherence" for m in resp]
await message.channel.send("**Adherence:**\n" + "\n".join(lines))
lines = []
for m in resp:
adherence = m.get('adherence_percent')
if adherence is not None:
lines.append(f"- **{m['name']}**: {adherence}% adherence ({m.get('taken', 0)}/{m.get('expected', 0)} doses)")
else:
lines.append(f"- **{m['name']}**: PRN medication (no adherence tracking)")
await message.channel.send("**Adherence Stats:**\n" + "\n".join(lines))
else:
await message.channel.send(f"**{resp.get('name')}**: {resp.get('adherence_percent')}% adherence")
adherence = resp.get('adherence_percent')
if adherence is not None:
await message.channel.send(
f"**{resp.get('name')}**:\n"
f"- Adherence: {adherence}%\n"
f"- Taken: {resp.get('taken', 0)}/{resp.get('expected', 0)} doses\n"
f"- Skipped: {resp.get('skipped', 0)}"
)
else:
await message.channel.send(f"**{resp.get('name')}**: PRN medication (no adherence tracking)")
else:
await message.channel.send(f"Error: {resp.get('error', 'Failed to fetch adherence')}")
else:
await message.channel.send(f"Unknown action: {action}. Try: list, add, take, skip, or adherence.")
await message.channel.send(f"Unknown action: {action}. Try: list, add, take, skip, today, refills, snooze, or adherence.")
async def _add_medication(message, token, name, dosage, unit, frequency, times, days_of_week, interval_days):
"""Helper to add a medication."""
data = {
"name": name,
"dosage": dosage,
"unit": unit,
"frequency": frequency,
"times": times
}
# Add optional fields if present
if days_of_week:
data["days_of_week"] = days_of_week
if interval_days:
data["interval_days"] = interval_days
resp, status = api_request("post", "/api/medications", token, data)
if status == 201:
schedule_desc = _format_schedule(frequency, times, days_of_week, interval_days)
await message.channel.send(f"✅ Added **{name}** ({dosage} {unit}) - {schedule_desc}")
else:
error_msg = resp.get('error', 'Failed to add medication')
if "invalid input syntax" in error_msg.lower():
await message.channel.send(f"Error: I couldn't understand the schedule format. Try saying it differently, like 'every day at 9am' or 'on Monday and Wednesday at 8pm'.")
else:
await message.channel.send(f"Error: {error_msg}")
async def _find_medication_by_name(message, token, med_id, name):
"""Helper to find medication by name if ID not provided. Returns (med_id, name, found)."""
if med_id:
return med_id, name, True
if not name:
return None, None, True # Will prompt user later
# Look up medication by name
resp, status = api_request("get", "/api/medications", token)
if status != 200:
await message.channel.send("Error looking up medication. Please try again.")
return None, None, False
meds = resp if isinstance(resp, list) else []
# Try exact match first
matching = [m for m in meds if m['name'].lower() == name.lower()]
# Then try partial match
if not matching:
matching = [m for m in meds if name.lower() in m['name'].lower() or m['name'].lower() in name.lower()]
if not matching:
await message.channel.send(f"❌ I couldn't find a medication called '{name}'.\n\nYour medications:\n" +
"\n".join([f"- {m['name']}" for m in meds[:10]]))
return None, None, False
if len(matching) > 1:
# Multiple matches - show list
lines = [f"{i+1}. {m['name']}" for i, m in enumerate(matching[:5])]
await message.channel.send(
f"🔍 I found multiple medications matching '{name}':\n" +
"\n".join(lines) +
"\n\nPlease specify which one (e.g., 'take 1' or say the full name)."
)
return None, None, False
return matching[0]['id'], matching[0]['name'], True
def _format_schedule(frequency, times, days_of_week, interval_days):
"""Helper to format schedule description."""
if frequency == "daily":
return f"daily at {', '.join(times)}"
elif frequency == "twice_daily":
return f"twice daily at {', '.join(times)}"
elif frequency == "specific_days":
days_str = ', '.join(days_of_week) if days_of_week else 'certain days'
return f"on {days_str} at {', '.join(times)}"
elif frequency == "every_n_days":
return f"every {interval_days} days at {', '.join(times)}"
elif frequency == "as_needed":
return "as needed"
else:
return f"{frequency} at {', '.join(times)}"
def api_request(method, endpoint, token, data=None):