| 1 | import express from 'express'; |
| 2 | import crypto from 'crypto'; |
| 3 | |
| 4 | const app = express(); |
| 5 | app.use(express.json()); |
| 6 | |
| 7 | // Webhook secret verification |
| 8 | function verifyWebhookSignature(payload: string, signature: string) { |
| 9 | const expectedSignature = crypto |
| 10 | .createHmac('sha256', process.env.WEBHOOK_SECRET!) |
| 11 | .update(payload) |
| 12 | .digest('hex'); |
| 13 | return crypto.timingSafeEqual( |
| 14 | Buffer.from(signature), |
| 15 | Buffer.from(expectedSignature) |
| 16 | ); |
| 17 | } |
| 18 | |
| 19 | // Support escalation logic |
| 20 | function determineSupportDestination(request: any) { |
| 21 | const { functionCall, call, customer } = request; |
| 22 | const { issue_category, complexity_level, customer_context, escalation_reason } = functionCall.parameters; |
| 23 | |
| 24 | // Simulate customer tier lookup |
| 25 | const customerData = lookupCustomerTier(customer.number); |
| 26 | |
| 27 | // Enterprise customer escalation |
| 28 | if (customerData?.tier === 'enterprise' || complexity_level === 'critical') { |
| 29 | return { |
| 30 | type: "number", |
| 31 | number: "+1-555-ENTERPRISE-SUPPORT", |
| 32 | message: "Connecting you to our enterprise support specialist.", |
| 33 | transferPlan: { |
| 34 | mode: "warm-transfer-say-summary", |
| 35 | summaryPlan: { |
| 36 | enabled: true, |
| 37 | messages: [ |
| 38 | { |
| 39 | role: "system", |
| 40 | content: "Provide a summary for the enterprise support specialist." |
| 41 | }, |
| 42 | { |
| 43 | role: "user", |
| 44 | content: `Enterprise customer with ${issue_category} issue. Complexity: ${complexity_level}. Reason: ${escalation_reason}. Context: ${customer_context}` |
| 45 | } |
| 46 | ] |
| 47 | } |
| 48 | } |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | // Advanced technical issues |
| 53 | if (issue_category === 'technical' && (complexity_level === 'advanced' || complexity_level === 'intermediate')) { |
| 54 | return { |
| 55 | type: "number", |
| 56 | number: "+1-555-TECH-SPECIALISTS", |
| 57 | message: "Transferring you to our technical support specialists.", |
| 58 | transferPlan: { |
| 59 | mode: "warm-transfer-say-message", |
| 60 | message: `Technical ${complexity_level} issue. Customer context: ${customer_context}. Escalation reason: ${escalation_reason}` |
| 61 | } |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | // Billing and account specialists |
| 66 | if (issue_category === 'billing' || issue_category === 'account') { |
| 67 | return { |
| 68 | type: "number", |
| 69 | number: "+1-555-BILLING-TEAM", |
| 70 | message: "Connecting you with our billing and account specialists.", |
| 71 | transferPlan: { |
| 72 | mode: "warm-transfer-say-message", |
| 73 | message: `${issue_category} issue, complexity ${complexity_level}. Context: ${customer_context}` |
| 74 | } |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | // Product and feature questions |
| 79 | if (issue_category === 'product') { |
| 80 | return { |
| 81 | type: "number", |
| 82 | number: "+1-555-PRODUCT-SUPPORT", |
| 83 | message: "Transferring you to our product specialists.", |
| 84 | transferPlan: { |
| 85 | mode: "warm-transfer-say-message", |
| 86 | message: `Product ${complexity_level} inquiry. Context: ${customer_context}` |
| 87 | } |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | // Default to general support |
| 92 | return { |
| 93 | type: "number", |
| 94 | number: "+1-555-GENERAL-SUPPORT", |
| 95 | message: "Connecting you with our support team.", |
| 96 | transferPlan: { |
| 97 | mode: "warm-transfer-say-message", |
| 98 | message: `General ${issue_category} support needed. Level: ${complexity_level}` |
| 99 | } |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | // Simulate customer tier lookup |
| 104 | function lookupCustomerTier(phoneNumber: string) { |
| 105 | // In production, integrate with your actual CRM |
| 106 | const mockCustomerData = { |
| 107 | "+1234567890": { tier: "enterprise", account: "TechCorp Enterprise" }, |
| 108 | "+0987654321": { tier: "standard", account: "Basic Plan" }, |
| 109 | "+1111111111": { tier: "premium", account: "Premium Support" } |
| 110 | }; |
| 111 | return mockCustomerData[phoneNumber]; |
| 112 | } |
| 113 | |
| 114 | // Support escalation webhook |
| 115 | app.post('/webhook/escalation', (req, res) => { |
| 116 | try { |
| 117 | const signature = req.headers['x-vapi-signature'] as string; |
| 118 | const payload = JSON.stringify(req.body); |
| 119 | |
| 120 | // Verify webhook signature |
| 121 | if (!verifyWebhookSignature(payload, signature)) { |
| 122 | return res.status(401).json({ error: 'Invalid signature' }); |
| 123 | } |
| 124 | |
| 125 | const request = req.body; |
| 126 | |
| 127 | // Only handle transfer destination requests |
| 128 | if (request.type !== 'transfer-destination-request') { |
| 129 | return res.status(200).json({ received: true }); |
| 130 | } |
| 131 | |
| 132 | // Determine destination based on escalation context |
| 133 | const destination = determineSupportDestination(request); |
| 134 | |
| 135 | res.json({ destination }); |
| 136 | } catch (error) { |
| 137 | console.error('Escalation webhook error:', error); |
| 138 | res.status(500).json({ |
| 139 | error: 'Unable to determine escalation destination. Please try again.' |
| 140 | }); |
| 141 | } |
| 142 | }); |
| 143 | |
| 144 | app.listen(3000, () => { |
| 145 | console.log('Support escalation server running on port 3000'); |
| 146 | }); |