import os
import asyncio
from dotenv import load_dotenv
from patter import Patter
load_dotenv()
phone = Patter(
twilio_sid=os.environ["TWILIO_SID"],
twilio_token=os.environ["TWILIO_TOKEN"],
openai_key=os.environ["OPENAI_KEY"],
phone_number=os.environ["PHONE_NUMBER"],
webhook_url=os.environ["WEBHOOK_URL"],
)
agent = phone.agent(
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.""",
voice="nova",
first_message="Thank you for calling Acme Corp! How can I help you today?",
tools=[
{
"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"],
},
"webhook_url": "https://api.example.com/appointments",
},
],
guardrails=[
Patter.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())