forked from voodoohome/node-red-contrib-asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.js
111 lines (82 loc) · 2.69 KB
/
module.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
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
/**
* This is a test
**/
module.exports = function(RED) {
var aio = require('asterisk.io'),
agi = null;
function AsteriskControllerNode(config) {
console.log("AsteriskControllerNode", config);
RED.nodes.createNode(this, config);
var callbacks = {};
this.registerHandler = function(code, callback) {
console.log(code,callback);
callbacks[code] = callback;
};
console.log("start service");
agi = aio.agi(14000); // port and host
// if host is missing then
// '0.0.0.0' is used as host
agi.on('error', function(err) {
throw err;
});
agi.on('listening', function() {
console.log('listening on port 14000');
});
agi.on('close', function() {
console.log('close');
});
agi.on('connection', function(agiHandler) {
console.log('connection', agiHandler);
agiHandler.on('hangup', function() {
// hangup handler
});
agiHandler.on('error', function(err) {
throw err;
});
agiHandler.on('close', function() {
// closing handler
});
// answer the channel
agiHandler.command('Answer', function(code, result, data) {
agiHandler.command('GET DATA "beep"', function(code, result, data) {
console.log(code, result, data);
if (code == 200 && result && result in callbacks) {
callbacks[result](result,agiHandler);
agiHandler.command('SAY DIGITS "' + result + '" "0"', function(code, result, data) {
console.log(code, result, data);
agiHandler.command('HangUp', function() {
// hangup the channel, this will raise hangup and close event
});
});
} else {
console.log("No match found");
agiHandler.command('HangUp', function() {
// hangup the channel, this will raise hangup and close event
});
}
});
});
});
this.on('close', function() {
agi.close();
});
console.log("service started");
}
RED.nodes.registerType("asterisk-controller", AsteriskControllerNode);
function AsteriskInputNode(config) {
console.log("AsteriskInputNode", config);
RED.nodes.createNode(this, config);
var node = this;
var ctrl = RED.nodes.getNode(config.controller);
this.on("input", function(msg) {
if (msg != null) {
console.log(msg);
};
});
ctrl.registerHandler(config.code,function(code, handler) {
console.log("getting called");
node.send({"topic": 'incoming', "payload": {"caller": handler.agi_callerid, "code":code}});
});
}
RED.nodes.registerType("asterisk-in", AsteriskInputNode);
}