Voice-AI-Systems-Guide

Chapter 5: Modern IVR Script Examples

5.1 Introduction

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:


5.2 Example 1 – E-commerce Order Tracking

Scenario: Customer wants to check their order status.

Flow:

  1. Greeting → “Welcome to ShopEasy. How can I assist you today?”
  2. Customer → “I want to track my order.”
  3. NLP identifies intent CheckOrderStatus.
  4. AI asks for the order number → “Please provide your order number.”
  5. Customer → “55421.”
  6. Backend query retrieves order info.
  7. TTS response → “Order 55421 was shipped yesterday and will arrive tomorrow.”
  8. Closing → “Is there anything else I can help you with?”

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)

5.3 Example 2 – Appointment Booking (Healthcare)

Scenario: Patient wants to schedule an appointment.

Flow:

  1. Greeting → “Hello, this is CityCare. How can I help you today?”
  2. Customer → “I want to book an appointment with Dr. Smith.”
  3. NLP intent → BookAppointment, entity → DoctorName=Smith.
  4. AI checks schedule → “Dr. Smith is available Thursday at 10 AM. Does that work?”
  5. Customer confirms → TTS → “Your appointment with Dr. Smith is confirmed for Thursday at 10 AM.”

Key Points:

Features:


5.4 Example 3 – Payment Collection

Scenario: Customer calls to pay an outstanding invoice.

Flow:

  1. Greeting → “Welcome to FinBank automated service.”
  2. Customer → “I want to pay my bill.”
  3. NLP intent → MakePayment
  4. AI → “Please provide your account number.”
  5. Customer provides info → Backend verifies balance
  6. TTS → “Your payment of $120 has been successfully processed.”
  7. Closing → “Thank you for using FinBank. Have a great day!”

Notes:

Security Features:


5.5 Example 4 – Technical Support

Scenario: Customer needs help with a technical issue.

Flow:

  1. Greeting → “Welcome to TechSupport. How can I help you today?”
  2. Customer → “My internet is not working.”
  3. NLP intent → TechnicalSupport, entity → IssueType=Internet
  4. AI → “I understand you’re having internet issues. Let me help you troubleshoot.”
  5. AI guides through diagnostic steps
  6. If resolved → “Great! Your internet should be working now.”
  7. If not resolved → “Let me connect you with a technician.”

Features:


5.6 Example 5 – Banking Balance Inquiry

Scenario: Customer wants to check account balance.

Flow:

  1. Greeting → “Welcome to SecureBank. How can I help you today?”
  2. Customer → “I want to check my balance.”
  3. NLP intent → CheckBalance
  4. AI → “For security, I’ll need to verify your identity. What’s your account number?”
  5. Customer provides account number
  6. AI → “Did you say account number 1-2-3-4-5-6-7-8?”
  7. Customer confirms
  8. AI → “Your current balance is $2,456.78.”
  9. Closing → “Is there anything else I can help you with?”

Security Features:


5.7 Best Practices Illustrated in Scripts

1. Use Natural Language

❌ “Press 1 for billing, press 2 for support…”
✅ “How can I help you today?”

2. Confirm Key Data

3. Short & Clear Prompts

4. Error Handling

5. Personalization

6. Multilingual Support


5.8 Technical Implementation Patterns

A. Intent Recognition Pattern

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}

B. Entity Extraction Pattern

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

C. SSML Response Pattern

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>'

5.9 Platform-Specific Implementations

A. Twilio Implementation

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)

B. Amazon Connect Implementation

{
  "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"
}

C. Asterisk Implementation

[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()

5.10 Error Handling and Fallbacks

A. Low Confidence Handling

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

B. Escalation Pattern

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."

C. Retry Pattern

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."

5.11 Performance Optimization

A. Response Time Optimization

B. Accuracy Improvement

C. Scalability Considerations


5.12 Testing and Quality Assurance

A. Unit Testing

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

B. Integration Testing

C. User Acceptance Testing


5.13 Summary


🛠️ Practical Examples

📚 Next Steps

✅ This closes Chapter 5.

Chapter 6 will cover advanced voice AI features including emotion detection, speaker identification, and multilingual support for global call centers.