Update app.py
Browse files
app.py
CHANGED
|
@@ -7,12 +7,58 @@ from agno.models.google import Gemini
|
|
| 7 |
from agno.tools.googlecalendar import GoogleCalendarTools
|
| 8 |
import datetime
|
| 9 |
from zoneinfo import ZoneInfo
|
| 10 |
-
import
|
|
|
|
|
|
|
| 11 |
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
@app.post("/sms")
|
| 17 |
async def sms_reply(Body: str = Form(...), From: str = Form(...)):
|
| 18 |
|
|
@@ -24,7 +70,8 @@ async def sms_reply(Body: str = Form(...), From: str = Form(...)):
|
|
| 24 |
id="gemini-2.5-pro-exp-03-25",
|
| 25 |
api_key=os.getenv("gemini_key")
|
| 26 |
),
|
| 27 |
-
tools=[GoogleCalendarTools(credentials_path="./credentials.json",token_path="./token.json")
|
|
|
|
| 28 |
show_tool_calls=True,
|
| 29 |
instructions=[
|
| 30 |
f"""
|
|
@@ -64,6 +111,8 @@ async def sms_reply(Body: str = Form(...), From: str = Form(...)):
|
|
| 64 |
- Include the reason for the appointment in the event description.
|
| 65 |
- Always ask for user confirmation before booking an appointment.
|
| 66 |
- Always ask for user confirmation before creating an event.
|
|
|
|
|
|
|
| 67 |
|
| 68 |
Be polite, upbeat, and positive at all times.
|
| 69 |
|
|
|
|
| 7 |
from agno.tools.googlecalendar import GoogleCalendarTools
|
| 8 |
import datetime
|
| 9 |
from zoneinfo import ZoneInfo
|
| 10 |
+
import gspread
|
| 11 |
+
from google.oauth2.service_account import Credentials
|
| 12 |
+
from agno.tools import tool
|
| 13 |
|
| 14 |
load_dotenv()
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
|
| 18 |
+
SERVICE_ACCOUNT_FILE = 'hybrid-subject-456511-t8-2eb6032818f8.json'
|
| 19 |
+
SCOPES = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']
|
| 20 |
+
credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
|
| 21 |
+
gc = gspread.authorize(credentials)
|
| 22 |
+
spreadsheet = gc.open_by_url('https://docs.google.com/spreadsheets/d/1LfeH3lueDz7686bSzhNT_4bq75U0cmsLlcx4PaxEZE4/edit')
|
| 23 |
+
worksheet = spreadsheet.get_worksheet(0)
|
| 24 |
+
|
| 25 |
+
def log_before_call(fc):
|
| 26 |
+
"""Pre-hook: Log arguments before calling the tool"""
|
| 27 |
+
print(f"π Adding appointment with details: {fc.arguments}")
|
| 28 |
+
|
| 29 |
+
def log_after_call(fc):
|
| 30 |
+
"""Post-hook: Log result after calling the tool"""
|
| 31 |
+
print(f"β
Appointment tool completed with result: {fc.result}")
|
| 32 |
+
|
| 33 |
+
@tool(
|
| 34 |
+
name="add_patient_appointment",
|
| 35 |
+
description="Add a patient's appointment to the worksheet",
|
| 36 |
+
show_result=True,
|
| 37 |
+
stop_after_tool_call=True,
|
| 38 |
+
pre_hook=log_before_call,
|
| 39 |
+
post_hook=log_after_call,
|
| 40 |
+
cache_results=False # usually better off for dynamic actions like this
|
| 41 |
+
)
|
| 42 |
+
def add_appointment(name: str, reason: str, datetime_str: str, contact: str) -> str:
|
| 43 |
+
"""
|
| 44 |
+
Add a new appointment entry.
|
| 45 |
+
|
| 46 |
+
Args:
|
| 47 |
+
name (str): Patient's name
|
| 48 |
+
reason (str): Reason for appointment
|
| 49 |
+
datetime_str (str): Date and time range (e.g., "11/7/2025 - 2 PM to 4 PM")
|
| 50 |
+
contact (str): Contact information (email or phone)
|
| 51 |
+
|
| 52 |
+
Returns:
|
| 53 |
+
str: Confirmation message
|
| 54 |
+
"""
|
| 55 |
+
new_row = [name, reason, datetime_str, contact]
|
| 56 |
+
try:
|
| 57 |
+
worksheet.append_row(new_row)
|
| 58 |
+
return f"ποΈ Appointment added for {name} on {datetime_str} for '{reason}'. Contact: {contact}"
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return f"β Error adding appointment: {str(e)}"
|
| 61 |
+
|
| 62 |
@app.post("/sms")
|
| 63 |
async def sms_reply(Body: str = Form(...), From: str = Form(...)):
|
| 64 |
|
|
|
|
| 70 |
id="gemini-2.5-pro-exp-03-25",
|
| 71 |
api_key=os.getenv("gemini_key")
|
| 72 |
),
|
| 73 |
+
tools=[GoogleCalendarTools(credentials_path="./credentials.json",token_path="./token.json"),
|
| 74 |
+
add_appointment],
|
| 75 |
show_tool_calls=True,
|
| 76 |
instructions=[
|
| 77 |
f"""
|
|
|
|
| 111 |
- Include the reason for the appointment in the event description.
|
| 112 |
- Always ask for user confirmation before booking an appointment.
|
| 113 |
- Always ask for user confirmation before creating an event.
|
| 114 |
+
- Always add Entry in Google Sheet for the appointment using the add_appointment function afte scheduling and event but don't mentione it in the response.
|
| 115 |
+
- Always generate conciess and clear short response.
|
| 116 |
|
| 117 |
Be polite, upbeat, and positive at all times.
|
| 118 |
|