-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
64 lines (54 loc) · 1.68 KB
/
controller.js
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
import { VoiceCallbackWebhooks } from '@sinch/sdk-core';
import {
handleAnsweredCallEvent,
handleDisconnectedCallEvent,
handleIncomingCallEvent,
handleNotifyEvent,
handlePromptInputEvent,
} from './serverBusinessLogic.js';
export const voiceController = (app) => {
const applicationKey = process.env.SINCH_APPLICATION_KEY;
const applicationSecret = process.env.SINCH_APPLICATION_SECRET;
const voiceCallbackWebhooks = new VoiceCallbackWebhooks({
applicationKey,
applicationSecret,
});
app.post('/VoiceEvent', async (req, res) => {
// Ensure the authentication is valid before processing the request
const validAuth = voiceCallbackWebhooks.validateAuthenticationHeader(
req.headers,
req.rawBody,
'/VoiceEvent',
'POST',
);
// Authentication header failed
if (!validAuth) {
return res.status(401).json();
}
// Parse the request payload
const event = voiceCallbackWebhooks.parseEvent(req.body);
// Let the business layer process the request
let response;
switch (event.event) {
case 'ice':
response = handleIncomingCallEvent(event);
break;
case 'ace':
response = handleAnsweredCallEvent(event);
break;
case 'pie':
response = handlePromptInputEvent(event);
break;
case 'dice':
response = handleDisconnectedCallEvent(event);
break;
case 'notify':
response = handleNotifyEvent(event);
break;
default:
res.status(500).json({ error: 'Unsupported event type' });
}
console.log(`JSON response:\n${JSON.stringify(response, null, 2)}`);
return res.status(200).json(response);
});
};