├── .editorconfig ├── .gitignore ├── .tern-project ├── LICENSE ├── README.md ├── examples └── test-agi.js ├── index.js ├── lib ├── agi-channel.js ├── agi-server.js └── async-agi-server.js └── package.json /.editorconfig: -------------------------------------------------------------------------------- 1 | [*.js] 2 | indent_style = space 3 | indent_size = 2 4 | end_of_line = lf 5 | charset = utf-8 6 | trim_trailing_whitespace = true 7 | insert_final_newline = true 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/* 2 | *.tgz 3 | package/* 4 | .tern-port 5 | play 6 | -------------------------------------------------------------------------------- /.tern-project: -------------------------------------------------------------------------------- 1 | { 2 | "libs": [ 3 | ], 4 | "plugins": { 5 | "complete_strings": {}, 6 | "node": {}, 7 | "doc_comment": { 8 | "fullDocs": true 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 TCN 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Getting Started 2 | 3 | ### 1. Usage 4 | 5 | ``` bash 6 | npm install --save agi-node 7 | ``` 8 | 9 | ``` javascript 10 | 11 | var AGIServer = require('agi-node').AGIServer; 12 | //var AsyncAGIServer = require('agi-node').AsyncAGIServer; 13 | //var conn = new require('asterisk-manager')(5038, 'localhost', 'asterisk', 'astpass', true); 14 | 15 | function fakeCallback(param, callback) { 16 | setTimeout(function () { 17 | callback(null, param); 18 | }, 2000); 19 | } 20 | 21 | function testScript(channel) { 22 | console.log('Script got call %s -> %s', channel.request.callerid, channel.request.extension); 23 | 24 | var answerReply = channel.answer(); 25 | console.log('ANSWER', answerReply); 26 | 27 | console.log('CHANNEL STATUS', channel.channelStatus()); 28 | console.log('GET UNIQUEID', channel.getVariable('UNIQUEID')); 29 | console.log('GET JUNK', channel.getVariable('JUNK')); 30 | 31 | console.log('beeping in 2 seconds'); 32 | channel.streamFile(fakeCallback.sync(null, 'beep')); 33 | 34 | 35 | console.log('PLAYBACK', channel.streamFile('conf-adminmenu')); 36 | console.log('PLAYBACK', channel.streamFile('conf-adminmenu')); 37 | } 38 | 39 | /* Async AGI Server */ 40 | // conn is an asterisk-manager connection 41 | //var server = new AsyncAGIServer(script, conn); 42 | 43 | /* AGI Server */ 44 | var server = new AGIServer(testScript, 4573); 45 | 46 | ``` 47 | 48 | 49 | * `AGIServer` constructor receives two parameters: a `mapper` and a `port`. If the mapper is a function it will execute that as an AGI script. If it is an object it maps script names from AGI URL to generator functions. Example: `{hello: helloScript}` will map `agi://agi_host/hello` to `helloScript`. The `port` is the AGI standard listening port (default `4573`) 50 | * `AsyncAGIServer` receives two parameters: a `mapper` and an AMI connection (established through `asterisk-manager`) 51 | * The script should exit at the very end. That means that all async operations (e.g. database lookups) need to be done using `sync` library. This is a dependency of `agi-node` and the script is executed inside a fiber, so callbacks can be put in "sync" mode (see `fakeCallback` example above and `sync` library itself at https://www.npmjs.com/package/sync). 52 | 53 | ### 2. Channel API 54 | 55 | #### 2.1 Channel.request 56 | 57 | This property is an object that maps all the AGI initialization variable without the *agi_* prefix (e.g. `agi_calleridname` becomes `channel.request.calleridname`). Here's a list of all of these variables: 58 | 59 | 60 | * `agi_request` - The filename of your script 61 | * `agi_channel` - The originating channel (your phone) 62 | * `agi_language` - The language code (e.g. "en") 63 | * `agi_type` - The originating channel type (e.g. "SIP" or "ZAP") 64 | * `agi_uniqueid` - A unique ID for the call 65 | * `agi_version` - The version of Asterisk (since Asterisk 1.6) 66 | * `agi_callerid` - The caller ID number (or "unknown") 67 | * `agi_calleridname` - The caller ID name (or "unknown") 68 | * `agi_callingpres` - The presentation for the callerid in a ZAP channel 69 | * `agi_callingani2` - The number which is defined in ANI2 see Asterisk Detailed Variable List (only for PRI Channels) 70 | * `agi_callington` - The type of number used in PRI Channels see Asterisk Detailed Variable List 71 | * `agi_callingtns` - An optional 4 digit number (Transit Network Selector) used in PRI Channels see Asterisk Detailed Variable List 72 | * `agi_dnid` - The dialed number id (or "unknown") 73 | * `agi_rdnis` - The referring DNIS number (or "unknown") 74 | * `agi_context` - Origin context in extensions.conf 75 | * `agi_extension` - The called number 76 | * `agi_priority` - The priority it was executed as in the dial plan 77 | * `agi_enhanced` - The flag value is 1.0 if started as an EAGI script, 0.0 otherwise 78 | * `agi_accountcode` - Account code of the origin channel 79 | * `agi_threadid` - Thread ID of the AGI script (since Asterisk 1.6) 80 | 81 | #### 2.2 Channel methods 82 | 83 | 84 | * `answer()` 85 | 86 | Answers the channel 87 | 88 | * `channelStatus()` 89 | 90 | Returns the current channel status (see http://www.voip-info.org/wiki/view/channel+status) 91 | 92 | * `continueAt(context, extension, priority)` 93 | 94 | Sets the point in the dialplan to continue the call after the AGI script is done. `extension` is optional and, if missing, is set the current channel extension. `priority` is optional and, if missing, is set to `1`. 95 | 96 | * `exec(applicationName, applicationParameters)` 97 | 98 | Executes the requested dialplan application with parameters 99 | 100 | * `getData(file, timeout, maxDigits)` 101 | 102 | Reads DTMF input from user. Plays the `file` prompt. Times out at `timeout` milliseconds and allows 103 | up to `maxDigits` to be read. It can be ended with `#`. 104 | 105 | * `getVariable(variableName)` 106 | 107 | Reads the specified variable value on the current channel. Returns `null` if variable does not exist. 108 | 109 | * `noop()` 110 | 111 | Does nothing. 112 | 113 | * `recordFile(file, format, escapeDigits, timeout, silenceSeconds, beep)` 114 | 115 | Records the current channel in `file` using `format`. Recording can be stopped using one 116 | of the `escapeDigits` or after `silenceSeconds`. If `beep` is true, a beep sound is played 117 | before recording is started. 118 | 119 | * `setContext(context)` 120 | 121 | Sets the context to continue at after leaving the script. 122 | 123 | * `setExtension(extension)` 124 | 125 | Sets the extension to continue at after leaving the script. 126 | 127 | * `setPriority(priority)` 128 | 129 | Sets the priority to continue at after leaving the script. 130 | 131 | * `setVariable(variable, value)` 132 | 133 | Sets the specified `variable` to the desired `value`. 134 | 135 | * `streamFile(file, escapeDigits)` 136 | 137 | Plays the specified `file`. Playback can be stopped using one of the (optional) `escapeDigits`. 138 | 139 | * `hangup()` 140 | 141 | Hangs up the current channel. 142 | 143 | 144 | ## LICENSE 145 | 146 | 147 | Copyright (c) 2015 Alexandru Pirvulescu 148 | 149 | 150 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 151 | 152 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 153 | 154 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 155 | -------------------------------------------------------------------------------- /examples/test-agi.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /* eslint no-var: 0 */ 4 | var AGIServer = require('../index').AGIServer; 5 | 6 | function fakeCallback(param, callback) { 7 | setTimeout(function () { 8 | callback(null, param); 9 | }, 2000); 10 | } 11 | 12 | function testScript(channel) { 13 | try { 14 | console.log('Script got call %s -> %s', channel.request.callerid, channel.request.extension); 15 | channel.exec('ANSWER'); 16 | channel.streamFile('beep'); 17 | channel.hangup(); 18 | 19 | } catch (ex) { 20 | console.log('Error in script', ex); 21 | } 22 | 23 | // var answerReply = channel.answer(); 24 | // console.log('ANSWER', answerReply); 25 | 26 | // console.log('CHANNEL STATUS', channel.channelStatus()); 27 | // console.log('GET UNIQUEID', channel.getVariable('UNIQUEID')); 28 | // console.log('GET JUNK', channel.getVariable('JUNK')); 29 | 30 | // console.log('beeping in 2 seconds'); 31 | // channel.streamFile(fakeCallback.sync(null, 'beep')); 32 | 33 | 34 | // console.log('PLAYBACK', channel.streamFile('conf-adminmenu')); 35 | // console.log('PLAYBACK', channel.streamFile('conf-adminmenu')); 36 | 37 | // console.log('GET DATA:', channel.getData('beep')); 38 | 39 | // console.log('EXEC: ', 40 | // channel.exec('Playback', 'beep')); 41 | // console.log('STREAM: ', 42 | // channel.streamFile('conf-adminmenu', '5')); 43 | // console.log('STREAM2: ', 44 | // channel.streamFile('beep', '5')); 45 | 46 | 47 | // console.log('CHANNEL STATUS', 48 | // channel.channelStatus()); 49 | // console.log('HANGUP: ', 50 | // yield channel.hangup()); 51 | } 52 | 53 | var server = new AGIServer(testScript, 4573); 54 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* eslint no-var: 0*/ 2 | /* global unescape */ 3 | 4 | /* eslint no-var: 0 */ 5 | 6 | var AGIChannel = require('./lib/agi-channel'); 7 | var AsyncAGIServer = require('./lib/async-agi-server'); 8 | var AGIServer = require('./lib/agi-server'); 9 | 10 | module.exports = { 11 | AsyncAGIServer: AsyncAGIServer, 12 | AGIServer: AGIServer, 13 | AGIChannel: AGIChannel 14 | }; 15 | -------------------------------------------------------------------------------- /lib/agi-channel.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /* eslint no-var: 0 */ 4 | // var sync = require('sync'); 5 | var syncho = require('syncho'); 6 | var util = require('util'); 7 | var events = require('events'); 8 | var sprintf = require('sprintf-js').sprintf; 9 | 10 | var AGIReply = function(line) { 11 | this.rawReply = line.trim(); 12 | this.code = parseInt(this.rawReply); 13 | this.attributes = {}; 14 | 15 | var self = this; 16 | 17 | var items = this.rawReply.split(' '); 18 | 19 | items.forEach(function(item) { 20 | if (item.indexOf('=') > 0) { 21 | var subItems = item.split('='); 22 | 23 | self.attributes[subItems[0]] = subItems[1]; 24 | } 25 | }); 26 | 27 | var m = this.rawReply.match(/\((.*)\)/); 28 | 29 | if (m) { 30 | this.extra = m[1]; 31 | } 32 | }; 33 | 34 | 35 | var AGIChannel = function(request, mapper) { 36 | events.EventEmitter.call(this); 37 | 38 | var self = this; 39 | 40 | self.request = request; 41 | self.cmdId = 0; 42 | 43 | if (typeof mapper == 'function') { 44 | mapper = { 45 | default: mapper 46 | }; 47 | } else if (typeof mapper != 'object') { 48 | self.emit('error', 'Invalid mapper'); 49 | return; 50 | } 51 | 52 | // locate the script 53 | var script; 54 | 55 | if (request.network_script) { 56 | script = mapper[request.network_script]; 57 | } 58 | 59 | if (!script) { 60 | script = mapper.default; 61 | } 62 | 63 | if (!script) { 64 | self.emit('error', 'Could not find requested script'); 65 | return; 66 | } 67 | 68 | process.nextTick(function() { 69 | syncho(function() { 70 | try { 71 | script(self); 72 | } catch (ex) { 73 | // console.log('Exception in script', ex, ex.stack); 74 | self.emit('error', ex); 75 | } 76 | self.emit('done'); // script has finished 77 | }); 78 | }); 79 | }; 80 | 81 | util.inherits(AGIChannel, events.EventEmitter); 82 | 83 | AGIChannel.prototype.handleReply = function(reply) { 84 | if (this.callback) { 85 | if (reply == 'hangup') { 86 | this.callback('hangup'); 87 | } else { 88 | this.callback(null, new AGIReply(reply)); 89 | } 90 | } 91 | }; 92 | 93 | AGIChannel.prototype._sendRequest = function(request, callback) { 94 | this.callback = callback; 95 | this.cmdId = this.cmdId + 1; 96 | this.emit('request', request, this.cmdId); 97 | }; 98 | 99 | AGIChannel.prototype.sendRequest = function(request) { 100 | return this._sendRequest.sync(this, request); 101 | }; 102 | 103 | 104 | // external API 105 | AGIChannel.prototype.answer = function() { 106 | var result = this.sendRequest('ANSWER'); 107 | 108 | return parseInt(result.attributes.result || -1); 109 | }; 110 | 111 | AGIChannel.prototype.channelStatus = function(channelName) { 112 | channelName = channelName || ''; 113 | 114 | var result = this.sendRequest(sprintf('CHANNEL STATUS %s', channelName)); 115 | 116 | return parseInt(result.attributes.result || -1); 117 | }; 118 | 119 | AGIChannel.prototype.exec = function(app, params) { 120 | 121 | if (params == undefined) { 122 | params = ''; 123 | } 124 | 125 | return this.sendRequest(sprintf('EXEC %s %s', app, params)); 126 | }; 127 | 128 | AGIChannel.prototype.getData = function(file, timeout, maxDigits) { 129 | timeout = (timeout == undefined) ? '' : timeout; 130 | maxDigits = (maxDigits == undefined) ? '' : maxDigits; 131 | 132 | var result = this.sendRequest(sprintf('GET DATA "%s" %s %s', file, timeout, 133 | maxDigits)); 134 | 135 | return result.attributes.result; 136 | }; 137 | 138 | AGIChannel.prototype.getFullVariable = function(variable, channel) { 139 | channel = (channel == undefined) ? '' : channel; 140 | 141 | var result = this.sendRequest(sprintf('GET FULL VARIABLE %s %s', variable, channel)); 142 | 143 | if (result.extra) { 144 | return result.extra; 145 | } else { 146 | return null; 147 | } 148 | }; 149 | 150 | AGIChannel.prototype.getOption = function(file, escapeDigits, timeout) { 151 | escapeDigits = (escapeDigits == undefined) ? '' : escapeDigits; 152 | timeout = (timeout == undefined) ? '' : timeout; 153 | 154 | return this.sendRequest(sprintf('GET OPTION "%s" %s" %s', file, escapeDigits, timeout)); 155 | }; 156 | 157 | AGIChannel.prototype.getVariable = function(variable) { 158 | var result = this.sendRequest(sprintf('GET VARIABLE "%s"', variable)); 159 | 160 | if (result.extra) { 161 | return result.extra; 162 | } else { 163 | return null; 164 | } 165 | }; 166 | 167 | 168 | AGIChannel.prototype.noop = function() { 169 | return this.sendRequest('NOOP'); 170 | }; 171 | 172 | AGIChannel.prototype.recordFile = function(file, format, escapeDigits, timeout, silenceSeconds, 173 | beep) { 174 | format = format || 'wav'; 175 | escapeDigits = escapeDigits || ''; 176 | timeout = (timeout == undefined) ? -1 : timeout; 177 | silenceSeconds = (silenceSeconds == undefined) ? '' : 's=' + silenceSeconds; 178 | beep = (beep) ? 'BEEP' : ''; 179 | 180 | 181 | return this.sendRequest(sprintf('RECORD FILE "%s" "%s" "%s" %s %s %s', 182 | file, format, escapeDigits, timeout, beep, silenceSeconds)); 183 | }; 184 | 185 | 186 | AGIChannel.prototype.streamFile = function(file, escapeDigits) { 187 | escapeDigits = escapeDigits || ''; 188 | 189 | return this.sendRequest(sprintf('STREAM FILE "%s" "%s"', file, escapeDigits)); 190 | }; 191 | 192 | AGIChannel.prototype.hangup = function() { 193 | return this.sendRequest('HANGUP'); 194 | }; 195 | 196 | 197 | AGIChannel.prototype.setContext = function(context) { 198 | return this.sendRequest(sprintf('SET CONTEXT %s', context)); 199 | }; 200 | 201 | AGIChannel.prototype.setExtension = function(extension) { 202 | return this.sendRequest(sprintf('SET EXTENSION %s', extension)); 203 | }; 204 | 205 | AGIChannel.prototype.setPriority = function(priority) { 206 | return this.sendRequest(sprintf('SET PRIORITY %s', priority)); 207 | }; 208 | 209 | AGIChannel.prototype.setVariable = function(variable, value) { 210 | return this.sendRequest(sprintf('SET VARIABLE %s %s', variable, value)); 211 | }; 212 | 213 | AGIChannel.prototype.continueAt = function(context, extension, priority) { 214 | extension = extension || this.request.extension; 215 | priority = priority || 1; 216 | 217 | this.setContext(context); 218 | this.setExtension(extension); 219 | this.setPriority(priority); 220 | 221 | return; 222 | }; 223 | 224 | AGIChannel.parseBuffer = function(buffer) { 225 | var request = {}; 226 | 227 | buffer.split('\n').forEach(function(line) { 228 | var items = line.split(/:\s?/); 229 | 230 | if (items.length == 2) { 231 | var name = items[0].trim(); 232 | 233 | if (name.indexOf('agi_') == 0) { 234 | name = name.substring(4); 235 | } 236 | var value = items[1].trim(); 237 | 238 | request[name] = value; 239 | } 240 | }); 241 | 242 | return request; 243 | }; 244 | 245 | 246 | module.exports = AGIChannel; 247 | -------------------------------------------------------------------------------- /lib/agi-server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /* eslint no-var: 0 */ 4 | 5 | var util = require('util'); 6 | var events = require('events'); 7 | var AGIChannel = require('./agi-channel'); 8 | var net = require('net'); 9 | 10 | // Embedded connection handler 11 | var AGIConnection = function(mapper, conn) { 12 | this.conn = conn; 13 | this.mapper = mapper; 14 | this.buffer = ''; 15 | 16 | var self = this; 17 | 18 | conn.on('data', this.handleData.bind(this)); 19 | conn.on('end', function() { 20 | if (self.handler) { 21 | self.handler('hangup'); 22 | } 23 | self.conn.destroy(); 24 | }); 25 | }; 26 | 27 | AGIConnection.prototype.handleData = function(data) { 28 | var self = this; 29 | 30 | data = data.toString(); 31 | 32 | if (data.indexOf('HANGUP') == 0) { 33 | if (self.handler) { 34 | self.handler('hangup'); 35 | } 36 | return; 37 | } 38 | 39 | if (self.handler) { 40 | self.handler(data.trim()); 41 | } else { 42 | this.buffer += data; 43 | if (this.buffer.indexOf('\n\n') >= 0) { 44 | // environment is sent 45 | var request = AGIChannel.parseBuffer(this.buffer); 46 | var channel = new AGIChannel(request, this.mapper); 47 | 48 | this.handler = channel.handleReply.bind(channel); 49 | 50 | channel.on('request', function(req) { 51 | self.conn.write(req + '\n'); 52 | }); 53 | 54 | channel.on('done', function() { 55 | self.conn.destroy(); 56 | self.conn = null; 57 | }); 58 | 59 | channel.on('error', function() { 60 | self.conn.destroy(); 61 | self.conn = null; 62 | }); 63 | } 64 | } 65 | }; 66 | 67 | 68 | var AGIServer = function(mapper, listenPort) { 69 | this.listenPort = listenPort || 4573; 70 | this.mapper = mapper; 71 | 72 | this.tcpServer = net.createServer(this.handleConnection.bind(this)); 73 | 74 | var self = this; 75 | 76 | process.nextTick(function() { 77 | self.tcpServer.listen(self.listenPort, function() { 78 | self.emit('ready'); 79 | }); 80 | }); 81 | }; 82 | 83 | util.inherits(AGIServer, events.EventEmitter); 84 | 85 | AGIServer.prototype.handleConnection = function(conn) { 86 | return new AGIConnection(this.mapper, conn); 87 | }; 88 | 89 | module.exports = AGIServer; 90 | -------------------------------------------------------------------------------- /lib/async-agi-server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /* eslint no-var: 0, no-console: 0 */ 4 | /* global unescape */ 5 | 6 | var events = require('events'); 7 | var util = require('util'); 8 | var AGIChannel = require('./agi-channel'); 9 | 10 | var AsyncAGIServer = function(mapper, amiConnection) { 11 | events.EventEmitter.call(this); 12 | 13 | var self = this; 14 | 15 | self.amiConnection = amiConnection; 16 | self.mapper = mapper; 17 | self.channels = {}; 18 | 19 | amiConnection.on('asyncagi', self.handleEvent.bind(self)); 20 | amiConnection.on('hangup', self.handleHangup.bind(self)); 21 | }; 22 | 23 | util.inherits(AsyncAGIServer, events.EventEmitter); 24 | 25 | AsyncAGIServer.prototype.handleHangup = function(hangup) { 26 | var handler = this.channels[hangup.channel]; 27 | 28 | if (handler) { 29 | handler('hangup'); 30 | delete this.channels[hangup.channel]; 31 | } 32 | }; 33 | 34 | AsyncAGIServer.prototype.handleEvent = function(event) { 35 | var channelName = event.channel; 36 | var handler; 37 | 38 | var self = this; 39 | 40 | if (event.event != 'AsyncAGI') { 41 | return; 42 | } 43 | 44 | var channel; 45 | 46 | if (event.subevent == 'Start') { 47 | // this is a start event 48 | // decode request 49 | var request = AGIChannel.parseBuffer(unescape(event.env)); 50 | 51 | channel = new AGIChannel(request, self.mapper); 52 | self.channels[channelName] = channel.handleReply.bind(channel); 53 | 54 | channel.on('request', function(req, cmdId) { 55 | var action = { 56 | action: 'agi', 57 | commandId: cmdId, 58 | command: req, 59 | channel: channelName 60 | }; 61 | 62 | self.amiConnection.action(action); 63 | }); 64 | 65 | channel.on('error', function(e) { 66 | console.log('Got error from script', e); 67 | self.amiConnection.action({ 68 | action: 'hangup', 69 | channel: channelName 70 | }); 71 | }); 72 | 73 | channel.on('done', function() { 74 | delete self.channels[channelName]; 75 | self.amiConnection.action({ 76 | action: 'agi', 77 | command: 'ASYNCAGI BREAK', 78 | channel: channelName 79 | }); 80 | 81 | }); 82 | } else if (event.subevent == 'Exec') { 83 | handler = self.channels[channelName]; 84 | if (handler) { 85 | handler(unescape(event.result)); 86 | } 87 | } 88 | }; 89 | 90 | module.exports = AsyncAGIServer; 91 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "agi-node", 3 | "version": "0.0.10", 4 | "description": "Asterisk AGI Server", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "publishConfig": { 10 | "registry": "https://registry.npmjs.org" 11 | }, 12 | "author": "Alexandru Pirvulescu ", 13 | "dependencies": { 14 | "asterisk-manager": "^0.1.15", 15 | "fibers": "^1.0.5", 16 | "sprintf-js": "^1.0.2", 17 | "syncho": "^0.2.3" 18 | }, 19 | "directories": { 20 | "example": "examples" 21 | }, 22 | "devDependencies": {}, 23 | "repository": { 24 | "type": "git", 25 | "url": "https://github.com/tcncloud/agi-node.git" 26 | }, 27 | "keywords": [ 28 | "agi", 29 | "node", 30 | "asterisk" 31 | ], 32 | "license": "MIT" 33 | } 34 | --------------------------------------------------------------------------------