-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
384 lines (325 loc) · 10.2 KB
/
index.js
File metadata and controls
384 lines (325 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const app = express();
const PORT = process.env.PORT || 3000;
// Security middleware
app.use(helmet());
// CORS configuration
app.use(cors({
origin: process.env.ALLOWED_ORIGINS ? process.env.ALLOWED_ORIGINS.split(',') : '*',
methods: ['POST', 'GET', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-AVR-WEBHOOK-SECRET']
}));
// Rate limiting to prevent abuse
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: process.env.RATE_LIMIT_MAX || 100, // limit each IP to 100 requests per windowMs
message: {
error: 'Too many requests from this IP, please try again later.',
retryAfter: '15 minutes'
},
standardHeaders: true,
legacyHeaders: false,
});
app.use('/events', limiter);
// Body parsing middleware
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
/**
* Simple webhook secret verification middleware
* Verifies that the X-AVR-WEBHOOK-SECRET header matches the WEBHOOK_SECRET environment variable
*/
function verifyWebhookSecret(req, res, next) {
const webhookSecret = process.env.WEBHOOK_SECRET;
// Skip verification if no secret is configured
if (!webhookSecret) {
console.warn('[WARNING] WEBHOOK_SECRET not configured, skipping secret verification');
return next();
}
const providedSecret = req.headers['x-avr-webhook-secret'];
if (!providedSecret) {
console.warn(`[SECURITY] Missing X-AVR-WEBHOOK-SECRET header from IP: ${req.ip}`);
return res.status(401).json({
error: 'Unauthorized',
message: 'Missing X-AVR-WEBHOOK-SECRET header',
timestamp: new Date().toISOString()
});
}
if (providedSecret !== webhookSecret) {
console.warn(`[SECURITY] Invalid webhook secret from IP: ${req.ip}`);
return res.status(401).json({
error: 'Unauthorized',
message: 'Invalid webhook secret',
timestamp: new Date().toISOString()
});
}
console.log(`[SECURITY] Valid webhook secret verified for IP: ${req.ip}`);
next();
}
// Request logging middleware
app.use((req, res, next) => {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] ${req.method} ${req.path} - IP: ${req.ip}`);
next();
});
/**
* Health check endpoint
* GET /health
*/
app.get('/health', (req, res) => {
res.status(200).json({
status: 'healthy',
timestamp: new Date().toISOString(),
service: 'avr-webhook',
version: process.env.npm_package_version || '1.0.0'
});
});
/**
* Webhook event handler
* POST /events
* Handles incoming webhook events from avr-core
*/
app.post('/events', verifyWebhookSecret, async (req, res) => {
try {
const { uuid, type, timestamp, payload } = req.body;
// Validate required fields
if (!uuid || !type || !timestamp) {
return res.status(400).json({
error: 'Missing required fields',
required: ['uuid', 'type', 'timestamp'],
received: Object.keys(req.body)
});
}
// Validate event type
const validEventTypes = [
'call_initiated',
'call_started',
'call_ended',
'interruption',
'transcription',
'dtmf_digit',
'error'
];
if (!validEventTypes.includes(type)) {
console.warn(`[WARNING] Unknown event type received: ${type}`);
}
// Log the incoming event
console.log(`[EVENT] ${type.toUpperCase()} - UUID: ${uuid} - Timestamp: ${timestamp}`);
if (payload) {
console.log(`[PAYLOAD] ${JSON.stringify(payload, null, 2)}`);
}
// Process the event based on type
await processWebhookEvent(uuid, type, timestamp, payload);
// Send acknowledgment response
res.status(200).json({
success: true,
message: 'Event processed successfully',
eventId: uuid,
processedAt: new Date().toISOString()
});
} catch (error) {
console.error('[ERROR] Failed to process webhook event:', error);
res.status(500).json({
error: 'Internal server error',
message: 'Failed to process webhook event',
timestamp: new Date().toISOString()
});
}
});
/**
* Process webhook event based on type
* @param {string} uuid - Unique identifier for the event
* @param {string} type - Type of event
* @param {string} timestamp - Event timestamp
* @param {Object} payload - Event payload data
*/
async function processWebhookEvent(uuid, type, timestamp, payload) {
try {
switch (type) {
case 'call_initiated':
await handleCallInitiated(uuid, payload);
break;
case 'call_started':
await handleCallStarted(uuid, payload);
break;
case 'call_ended':
await handleCallEnded(uuid, payload);
break;
case 'transcription':
await handleTranscription(uuid, payload);
break;
case 'interruption':
await handleInterruption(uuid, payload);
break;
case 'dtmf_digit':
await handleDtmfDigit(uuid, payload);
break;
case 'error':
await handleError(uuid, payload);
break;
default:
console.log(`[INFO] Unhandled event type: ${type}`);
}
} catch (error) {
console.error(`[ERROR] Failed to process event ${type} for UUID ${uuid}:`, error);
throw error;
}
}
/**
* Handle call initiated events
* @param {string} uuid - Event UUID
* @param {Object} payload - Event payload
*/
async function handleCallInitiated(uuid, payload) {
console.log(`[CALL_INITIATED] Call initiated - UUID: ${uuid}`);
// Example: Store call information in database
// await database.calls.create({
// uuid,
// startTime: new Date(),
// callerId: payload?.callerId,
// destination: payload?.destination,
// status: 'active'
// });
// Example: Send notification to monitoring system
// await notificationService.send('call_initiated', { uuid, payload });
// Example: Return STS URL with custom parameters
return res.json({
sts_url: `ws://localhost:6030?uuid=${uuid}&my_custom_param=my_custom_value`,
});
}
/**
* Handle call started events
* @param {string} uuid - Event UUID
* @param {Object} payload - Event payload
*/
async function handleCallStarted(uuid, payload) {
console.log(`[CALL_STARTED] Call initiated - UUID: ${uuid}`);
// Example: Store call information in database
// await database.calls.create({
// uuid,
// startTime: new Date(),
// callerId: payload?.callerId,
// destination: payload?.destination,
// status: 'active'
// });
// Example: Send notification to monitoring system
// await notificationService.send('call_started', { uuid, payload });
}
/**
* Handle call ended events
* @param {string} uuid - Event UUID
* @param {Object} payload - Event payload
*/
async function handleCallEnded(uuid, payload) {
console.log(`[CALL_ENDED] Call terminated - UUID: ${uuid}`);
// Example: Update call record in database
// await database.calls.update(uuid, {
// endTime: new Date(),
// duration: payload?.duration,
// status: 'completed',
// reason: payload?.reason
// });
// Example: Generate call summary
// await generateCallSummary(uuid, payload);
}
/**
* Handle interruption events
/**
* Handle DTMF digit events
/**
* Handle DTMF digit events
* @param {string} uuid - Event UUID
* @param {Object} payload - Event payload
*/
async function handleDtmfDigit(uuid, payload) {
console.log(`[DTMF_DIGIT] DTMF digit: ${payload?.digit} - UUID: ${uuid}`);
}
/**
* Handle interruption events
/**
* Handle interruption events
* @param {string} uuid - Event UUID
* @param {Object} payload - Event payload
*/
async function handleInterruption(uuid, payload) {
console.log(`[INTERRUPTION] Interruption - UUID: ${uuid}`);
}
/**
* @param {string} uuid - Event UUID
* @param {Object} payload - Event payload
*/
async function handleInterruption(uuid, payload) {
console.log(`[INTERRUPTION] Interruption - UUID: ${uuid}`);
}
/**
* Handle transcription events
* @param {string} uuid - Event UUID
* @param {Object} payload - Event payload
*/
async function handleTranscription(uuid, payload) {
console.log(`[TRANSCRIPTION] Text: "${payload?.text}" - UUID: ${uuid}`);
// Example: Store transcription in database
// await database.transcriptions.create({
// uuid,
// text: payload?.text,
// confidence: payload?.confidence,
// timestamp: new Date()
// });
// Example: Process transcription for intent analysis
// await intentAnalysis.process(payload?.text, uuid);
}
/**
* Handle error events
* @param {string} uuid - Event UUID
* @param {Object} payload - Event payload
*/
async function handleError(uuid, payload) {
console.error(`[ERROR] ${payload?.message || 'Unknown error'} - UUID: ${uuid}`);
// Example: Log error to monitoring system
// await errorReporting.log({
// uuid,
// error: payload?.error,
// message: payload?.message,
// stack: payload?.stack,
// timestamp: new Date()
// });
// Example: Send alert to administrators
// await alertService.send('error_occurred', { uuid, payload });
}
// Global error handler
app.use((error, req, res, next) => {
console.error('[GLOBAL_ERROR]', error);
res.status(500).json({
error: 'Internal server error',
message: process.env.NODE_ENV === 'development' ? error.message : 'Something went wrong',
timestamp: new Date().toISOString()
});
});
// 404 handler
app.use('*', (req, res) => {
res.status(404).json({
error: 'Not found',
message: `Route ${req.method} ${req.originalUrl} not found`,
timestamp: new Date().toISOString()
});
});
// Start server
app.listen(PORT, () => {
console.log(`🚀 AVR Webhook Service running on port ${PORT}`);
console.log(`📡 Health check: http://localhost:${PORT}/health`);
console.log(`🎯 Webhook endpoint: http://localhost:${PORT}/events`);
console.log(`🌍 Environment: ${process.env.NODE_ENV || 'development'}`);
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('SIGTERM received, shutting down gracefully');
process.exit(0);
});
process.on('SIGINT', () => {
console.log('SIGINT received, shutting down gracefully');
process.exit(0);
});
module.exports = app;