Modern call centers are moving beyond rigid menu-based IVRs toward AI-powered, dynamic conversational flows. This chapter provides real-world examples of IVR scripts that combine TTS + NLP + Telephony, ready for developers and integrators.
The examples in this chapter demonstrate:
Scenario: Customer wants to check their order status.
Flow:
CheckOrderStatus
.Key Features:
Twilio + Python Example:
from flask import Flask, request, Response
from twilio.twiml.voice_response import VoiceResponse
app = Flask(__name__)
@app.route("/voice", methods=["POST"])
def voice():
resp = VoiceResponse()
resp.say("Welcome to ShopEasy. How can I assist you today?", voice="Polly.Joanna")
# Here you would integrate NLP and backend logic
return Response(str(resp), mimetype="application/xml")
if __name__ == "__main__":
app.run(port=5000)
Scenario: Patient wants to schedule an appointment.
Flow:
BookAppointment
, entity → DoctorName=Smith
.Key Points:
Features:
Scenario: Customer calls to pay an outstanding invoice.
Flow:
MakePayment
Notes:
Security Features:
Scenario: Customer needs help with a technical issue.
Flow:
TechnicalSupport
, entity → IssueType=Internet
Features:
Scenario: Customer wants to check account balance.
Flow:
CheckBalance
Security Features:
❌ “Press 1 for billing, press 2 for support…”
✅ “How can I help you today?”
def classify_intent(utterance: str) -> Dict:
"""Classify customer intent from utterance"""
utterance_lower = utterance.lower()
if any(word in utterance_lower for word in ["track", "order", "status"]):
return {"intent": "CheckOrderStatus", "confidence": 0.95}
elif any(word in utterance_lower for word in ["book", "appointment", "schedule"]):
return {"intent": "BookAppointment", "confidence": 0.92}
elif any(word in utterance_lower for word in ["pay", "payment", "bill"]):
return {"intent": "MakePayment", "confidence": 0.89}
else:
return {"intent": "Unknown", "confidence": 0.45}
def extract_entities(utterance: str) -> Dict:
"""Extract entities from customer utterance"""
entities = {}
# Extract order numbers
order_pattern = r'\b(\d{5,})\b'
orders = re.findall(order_pattern, utterance)
if orders:
entities["order_number"] = orders[0]
# Extract doctor names
doctor_pattern = r'Dr\.\s+(\w+)'
doctors = re.findall(doctor_pattern, utterance)
if doctors:
entities["doctor_name"] = doctors[0]
# Extract amounts
amount_pattern = r'\$(\d+(?:\.\d{2})?)'
amounts = re.findall(amount_pattern, utterance)
if amounts:
entities["amount"] = float(amounts[0])
return entities
def generate_ssml_response(text: str, add_pauses: bool = True) -> str:
"""Generate SSML with natural pacing"""
ssml = text
if add_pauses:
# Add pauses for natural pacing
ssml = re.sub(r'([.!?])\s+', r'\1 <break time="300ms"/> ', ssml)
# Add pauses before important information
ssml = re.sub(r'(\$[\d,]+\.?\d*)', r'<break time="400ms"/> \1', ssml)
return f'<speak>{ssml}</speak>'
from flask import Flask, request
from twilio.twiml.voice_response import VoiceResponse
app = Flask(__name__)
@app.route("/webhook", methods=["POST"])
def handle_call():
resp = VoiceResponse()
# Get customer input
speech_result = request.values.get('SpeechResult', '')
# Process with NLP
intent = classify_intent(speech_result)
if intent["intent"] == "CheckOrderStatus":
resp.say("Please provide your order number.", voice="Polly.Joanna")
resp.gather(input="speech", action="/process_order", method="POST")
else:
resp.say("I didn't understand. Please try again.", voice="Polly.Joanna")
resp.gather(input="speech", action="/webhook", method="POST")
return str(resp)
{
"Type": "GetCustomerInput",
"Parameters": {
"Text": "Welcome to our service. How can I help you today?",
"TimeoutSeconds": 10,
"MaxDigits": 0,
"TextToSpeechParameters": {
"VoiceId": "Joanna",
"Engine": "neural"
}
},
"NextAction": "ProcessIntent"
}
[main-menu]
exten => s,1,Answer()
exten => s,n,Wait(1)
exten => s,n,Playback(welcome)
exten => s,n,Read(customer_input,beep,3)
exten => s,n,Set(intent=${SHELL(python3 /path/to/nlp.py ${customer_input})})
exten => s,n,GotoIf($[${intent}="order"]?order-tracking:main-menu)
exten => s,n,Hangup()
[order-tracking]
exten => s,1,Playback(please-provide-order)
exten => s,n,Read(order_number,beep,5)
exten => s,n,Set(order_info=${SHELL(python3 /path/to/order_lookup.py ${order_number})})
exten => s,n,Playback(order-info)
exten => s,n,Hangup()
def handle_low_confidence(intent: Dict, utterance: str) -> str:
"""Handle cases where intent confidence is low"""
if intent["confidence"] < 0.7:
return f"I think you said '{utterance}', but I'm not completely sure. " \
f"Could you please clarify what you need help with?"
return None
def escalate_to_human(reason: str) -> str:
"""Escalate call to human agent"""
return f"I understand this is important. Let me connect you with a " \
f"specialist who can better assist you. Please hold."
def retry_prompt(attempt: int, max_attempts: int = 3) -> str:
"""Generate retry prompt with increasing clarity"""
if attempt == 1:
return "I didn't catch that. Could you please repeat?"
elif attempt == 2:
return "I'm still having trouble understanding. You can say things like " \
"'check my order', 'make a payment', or 'speak to an agent'."
else:
return "Let me connect you with a human agent who can help."
def test_intent_classification():
"""Test intent classification accuracy"""
test_cases = [
("I want to track my order", "CheckOrderStatus"),
("I need to pay my bill", "MakePayment"),
("Book an appointment", "BookAppointment")
]
for utterance, expected_intent in test_cases:
result = classify_intent(utterance)
assert result["intent"] == expected_intent
✅ This closes Chapter 5.
Chapter 6 will cover advanced voice AI features including emotion detection, speaker identification, and multilingual support for global call centers.