import os
import asyncio
from dotenv import load_dotenv
from getpatter import Patter, Twilio, OpenAIRealtime, Tool, Guardrail
load_dotenv()
phone = Patter(
carrier=Twilio(), # TWILIO_* from env
phone_number=os.environ["PHONE_NUMBER"],
webhook_url=os.environ["WEBHOOK_URL"],
)
async def schedule_appointment(args: dict, ctx: dict) -> dict:
# Your reservation system here.
return {"confirmation": "ACME-123", "date": args["date"], "time": args["time"]}
agent = phone.agent(
engine=OpenAIRealtime(voice="nova"), # OPENAI_API_KEY from env
system_prompt="""You are a friendly receptionist for Acme Corp.
Help callers schedule appointments, answer general questions, and transfer to the right department.
Transfer billing questions to +15550001111.
Transfer technical support to +15550002222.""",
first_message="Thank you for calling Acme Corp! How can I help you today?",
tools=[
Tool(
name="schedule_appointment",
description="Schedule an appointment for the caller.",
parameters={
"type": "object",
"properties": {
"name": {"type": "string", "description": "Caller's full name"},
"date": {"type": "string", "description": "Preferred date (YYYY-MM-DD)"},
"time": {"type": "string", "description": "Preferred time (HH:MM)"},
"reason": {"type": "string", "description": "Reason for appointment"},
},
"required": ["name", "date", "time"],
},
handler=schedule_appointment,
),
],
guardrails=[
Guardrail(
name="No legal advice",
blocked_terms=["lawsuit", "sue", "legal action"],
replacement="I'm not able to provide legal guidance. Let me transfer you to our legal department.",
),
],
)
async def on_call_start(event):
print(f"[CALL START] {event['direction']} call {event['call_id']}")
print(f" From: {event['caller']} -> To: {event['callee']}")
async def on_call_end(event):
print(f"[CALL END] {event['call_id']}")
for entry in event["transcript"]:
print(f" [{entry['role']}]: {entry['text']}")
async def on_transcript(event):
print(f" [{event['role']}]: {event['text']}")
async def main():
await phone.serve(
agent,
port=8000,
recording=True,
on_call_start=on_call_start,
on_call_end=on_call_end,
on_transcript=on_transcript,
voicemail_message="Hi, this is Acme Corp. Please call us back at your earliest convenience.",
)
asyncio.run(main())