-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathari-second.js
85 lines (71 loc) · 2.99 KB
/
ari-second.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const AriClient = require('ari-client');
const WebSocket = require('ws');
// ARI connection parameters
const ariUrl = 'http://192.168.0.181:8088';
const ariUser = 'asterisk';
const ariPassword = 'secret';
// Connect to Asterisk ARI
AriClient.connect(ariUrl, ariUser, ariPassword)
.then(client => {
console.log('Connected to ARI');
client.on('StasisStart', event => {
const channel = event.channel;
console.log(`Channel ${channel.id} entered Stasis application`);
// Start recording the call
const recordingName = `recording_${channel.id}`;
client.channels.record({
channelId: channel.id,
name: recordingName,
format: 'wav',
ifExists: 'overwrite',
beep: true
}).then(recording => {
console.log(`Recording started: ${recording.name}`);
}).catch(err => {
console.error('Error starting recording:', err);
});
// Connect to WebSocket server
const ws = new WebSocket('ws://localhost:8080');
ws.on('open', () => {
console.log('Connected to WebSocket server');
// Send call details to WebSocket server
ws.send(JSON.stringify({
event: 'call',
channel: channel.id,
caller: channel.caller.number,
callee: channel.dialplan.exten
}));
// Answer the call
client.channels.answer({ channelId: channel.id })
.catch(err => console.error('Error answering call:', err));
});
ws.on('message', message => {
console.log('Received from WebSocket server:', message);
// Handle messages from WebSocket server
});
ws.on('close', () => {
console.log('WebSocket connection closed');
});
ws.on('error', err => {
console.error('WebSocket error:', err);
});
// Event handler for when the recording is finished
client.on('RecordingFinished', recording => {
if (recording.name === recordingName) {
console.log(`Recording finished: ${recording.name}`);
// Send the recorded file details via WebSocket
ws.send(JSON.stringify({
event: 'recording_finished',
recording: {
name: recording.name,
format: recording.format,
duration: recording.duration
}
}));
}
});
});
client.start('my_stasis_app')
.catch(err => console.error('Error starting Stasis application:', err));
})
.catch(err => console.error('Error connecting to ARI:', err));