Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/antirek/ding-dong
Browse files Browse the repository at this point in the history
Conflicts:
	package.json
  • Loading branch information
antirek committed Mar 19, 2015
2 parents 0122e71 + 8ec64e4 commit 0d62b7f
Show file tree
Hide file tree
Showing 4 changed files with 387 additions and 29 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Create AGI server with ding-dong. Use with Asterisk for fast telephony apps. [Fork of node-agi](http://github.com/brianc/node-agi)

## install
## Install

```
npm install ding-dong [--save]
Expand Down Expand Up @@ -60,10 +60,13 @@ context.hangup(function(err, res) {
```


## Projects
Projects
========

[Voicer](http://github.com/antirek/voicer) - AGI yandex voice recognizer for Asterisk

[agi-number-archer](http://github.com/antirek/agi-number-archer) - AGI server for find region code of phone number (Russia)

## Links

[Asterisk AGI](https://wiki.asterisk.org/wiki/display/AST/Asterisk+13+AGI+Commands)
167 changes: 149 additions & 18 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ var Readable = require('readable-stream');
var EventEmitter = require('events').EventEmitter;
var state = require('./state');

var Context = function(stream) {
var Context = function (stream) {
EventEmitter.call(this);
this.stream = new Readable();
this.stream.wrap(stream);
this.state = state.init;
this.msg = "";
var self = this;
this.stream.on('readable', function() {
this.stream.on('readable', function () {
//always keep the 'leftover' part of the message
self.msg = self.read();
});
Expand All @@ -36,7 +36,7 @@ Context.prototype.read = function() {
return "";
};

Context.prototype.readVariables = function(msg) {
Context.prototype.readVariables = function (msg) {
var lines = msg.split('\n');
for(var i = 0; i < lines.length; i++) {
var line = lines[i];
Expand All @@ -50,17 +50,19 @@ Context.prototype.readVariables = function(msg) {
return "";
};

Context.prototype.readResponse = function(msg) {
Context.prototype.readResponse = function (msg) {
var lines = msg.split('\n');
for(var i = 0; i < lines.length; i++) {
this.readResponseLine(lines[i]);
}
return "";
};

Context.prototype.readResponseLine = function(line) {
Context.prototype.readResponseLine = function (line) {
if(!line) return;

var parsed = /^(\d{3})(?: result=)(.*)/.exec(line);

if(!parsed) {
return this.emit('hangup');
}
Expand Down Expand Up @@ -92,7 +94,7 @@ Context.prototype.exec = function() {
var last = args.pop();
if(typeof last !== 'function') {
args.push(last);
last = function() { }
last = function () { }
}
this.send('EXEC ' + args.join(' ') + '\n', last);
};
Expand All @@ -101,29 +103,142 @@ Context.prototype.dial = function (num, timeout, params, cb) {
this.exec('Dial', num + ',' + timeout + ',' + params, cb);
};

Context.prototype.databaseDel = function (family, key, cb) {
this.send('DATABASE DEL ' + family + ' ' + key + '\n', cb || function () { });
};

Context.prototype.databaseDelTree = function (family, keytree, cb) {
this.send('DATABASE DELTREE ' + family + ' ' + keytree + '\n', cb || function () { });
};

Context.prototype.databaseGet = function (family, key, cb) {
this.send('DATABASE GET ' + family + ' ' + key + '\n', cb || function () { });
};

Context.prototype.databasePut = function (family, key, value, cb) {
this.send('DATABASE PUT ' + family + ' ' + key + ' ' + value + '\n', cb || function () { });
};

Context.prototype.speechCreate = function (engine, cb) {
this.send('SPEECH CREATE ' + engine + '\n', cb || function () { });
};

Context.prototype.speechDestroy = function (cb) {
this.send('SPEECH DESTROY\n', cb || function () { });
};

Context.prototype.speechActivateGrammar = function (name, cb) {
this.send('SPEECH ACTIVATE GRAMMAR ' + name + '\n', cb || function () { });
};

Context.prototype.speechDeactivateGrammar = function (name, cb) {
this.send('SPEECH DEACTIVATE GRAMMAR ' + name + '\n', cb || function () { });
};

Context.prototype.speechLoadGrammar = function (name, path, cb) {
this.send('SPEECH LOAD GRAMMAR ' + name + ' ' + path + '\n', cb || function () { });
};

Context.prototype.speechUnloadGrammar = function (name, cb) {
this.send('SPEECH UNLOAD GRAMMAR ' + name + '\n', cb || function () { });
};

Context.prototype.speechSet = function (name, value, cb) {
this.send('SPEECH SET ' + name + ' ' + value + '\n', cb || function () { });
};

Context.prototype.speechRecognize = function (prompt, timeout, offset, cb) {
this.send('SPEECH RECOGNIZE ' + prompt + ' ' + timeout + ' ' + offset + '\n', cb || function () { });
};

Context.prototype.getVariable = function (name, cb) {
this.send('GET VARIABLE ' + name + '\n', cb || function() { });
this.send('GET VARIABLE ' + name + '\n', cb || function () { });
};

Context.prototype.getFullVariable = function (variable, channel, cb) {
this.send('GET FULL VARIABLE ' + variable + ' ' + channel + '\n', cb || function() { });
this.send('GET FULL VARIABLE ' + variable + ' ' + channel + '\n', cb || function () { });
};

Context.prototype.getData = function (file, timeout, maxdigits, cb) {
this.send('GET DATA ' + file + ' ' + timeout + ' ' + maxdigits + '\n', cb || function () { });
};

Context.prototype.getOption = function (file, escape_digits, timeout, cb) {
this.send('GET OPTION ' + file + ' "' + escape_digits + '" ' + timeout + '\n', cb || function () { });
};

Context.prototype.receiveChar = function (timeout, cb) {
this.send('RECEIVE CHAR ' + timeout + '\n', cb || function () { });
};

Context.prototype.receiveText = function (timeout, cb) {
this.send('RECEIVE TEXT ' + timeout + '\n', cb || function () { });
};

Context.prototype.setAutoHangup = function (seconds, cb) {
this.send('SET AUTOHANGUP ' + seconds + '\n', cb || function () { });
};

Context.prototype.setCallerID = function (number, cb) {
this.send('SET CALLERID ' + number + '\n', cb || function () { });
};

Context.prototype.setContext = function (context, cb) {
this.send('SET CONTEXT ' + context + '\n', cb || function () { });
};

Context.prototype.setExtension = function (extension, cb) {
this.send('SET EXTENSION ' + extension + '\n', cb || function () { });
};

Context.prototype.setPriority = function (priority, cb) {
this.send('SET PRIORITY ' + priority + '\n', cb || function () { });
};

Context.prototype.setMusic = function (musicclass, cb) {
this.send('SET MUSIC ' + musicclass + '\n', cb || function () { });
};

Context.prototype.setVariable = function (name, value, cb) {
this.send('SET VARIABLE ' + name + ' ' + value + '\n', cb || function() { });
this.send('SET VARIABLE ' + name + ' ' + value + '\n', cb || function () { });
};

Context.prototype.sendImage = function (image, cb) {
this.send('SEND IMAGE ' + image + '\n', cb || function() { });
};

Context.prototype.sendText = function (text, cb) {
this.send('SEND TEXT "' + text + '"\n', cb || function() { });
};

Context.prototype.channelStatus = function (name, cb) {
this.send('CHANNEL STATUS ' + name + '\n', cb || function() { });
};

Context.prototype.answer = function (cb) {
this.send('ANSWER' + '\n', cb || function() { });
this.send('ANSWER\n', cb || function () { });
};

Context.prototype.verbose = function (message, level, cb) {
this.send('VERBOSE "' + message + '" ' + level + '\n', cb || function () { });
};

Context.prototype.tddMode = function (value, cb) {
this.send('TDD MODE ' + value + '\n', cb || function () { });
};

Context.prototype.noop = function (cb) {
this.send('NOOP\n', cb || function () { });
};

Context.prototype.gosub = function (context, extension, priority, option, cb) {
var str = [context, extension, priority, option].join(' ');
this.send('GOSUB ' + str + '\n', cb || function () { });
};

Context.prototype.recordFile = function (filename, format, escape_digits, timeout, offset, beep, silence, cb) {
var str = [
'"'+filename+'"',
'"' + filename + '"',
format,
escape_digits,
parseInt(timeout)*1000,
Expand All @@ -134,27 +249,43 @@ Context.prototype.recordFile = function (filename, format, escape_digits, timeou
this.send('RECORD FILE ' + str + '\n', cb || function() { });
};

Context.prototype.sayNumber = function(number, escape_digits, cb) {
Context.prototype.sayNumber = function (number, escape_digits, cb) {
this.send('SAY NUMBER ' + number + ' "' + escape_digits + '"' + '\n', cb || function() { });
};

Context.prototype.sayDigits = function(digits, escape_digits, cb) {
Context.prototype.sayAlpha = function (number, escape_digits, cb) {
this.send('SAY ALPHA ' + number + ' "' + escape_digits + '"' + '\n', cb || function() { });
};

Context.prototype.sayDate = function (seconds, escape_digits, cb) { //seconds since 1.01.1970
this.send('SAY DATE ' + seconds + ' "' + escape_digits + '"' + '\n', cb || function() { });
};

Context.prototype.sayTime = function (seconds, escape_digits, cb) { //seconds since 1.01.1970
this.send('SAY TIME ' + seconds + ' "' + escape_digits + '"' + '\n', cb || function() { });
};

Context.prototype.sayDateTime = function (seconds, escape_digits, format, timezone, cb) { //seconds since 1.01.1970
this.send('SAY DATETIME ' + seconds + ' "' + escape_digits + '" ' + format + ' ' + timezone + '\n', cb || function() { });
};

Context.prototype.sayDigits = function (digits, escape_digits, cb) {
this.send('SAY DIGITS ' + digits + ' "' + escape_digits + '"' + '\n', cb || function() { });
};

Context.prototype.sayPhonetic = function(string, escape_digits, cb) {
Context.prototype.sayPhonetic = function (string, escape_digits, cb) {
this.send('SAY PHONETIC ' + string + ' "' + escape_digits + '"' + '\n', cb || function() { });
};

Context.prototype.streamFile = function(filename, acceptDigits, cb) {
Context.prototype.streamFile = function (filename, acceptDigits, cb) {
if(typeof acceptDigits === 'function') {
cb = acceptDigits;
acceptDigits = "1234567890#*";
}
this.send('STREAM FILE "' + filename + '" "' + acceptDigits + '"\n', cb);
};

Context.prototype.waitForDigit = function(timeout, cb) {
Context.prototype.waitForDigit = function (timeout, cb) {
if(typeof timeout === 'function') {
cb = timeout;
//default to 2 second timeout
Expand All @@ -163,11 +294,11 @@ Context.prototype.waitForDigit = function(timeout, cb) {
this.send('WAIT FOR DIGIT ' + timeout + '\n', cb);
};

Context.prototype.hangup = function(cb) {
Context.prototype.hangup = function (cb) {
this.send('HANGUP\n', cb);
};

Context.prototype.end = function() {
Context.prototype.end = function () {
this.stream.end();
};

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"author": "Sergey Dmitriev <[email protected]>",
"name": "ding-dong",
"description": "AGI (Asterisk Gateway Interface) for writing dialplan scripts",
"version": "0.0.7",
"description": "Write AGI-server quickly! (AGI - Asterisk Gateway Interface)",
"version": "0.0.9",
"repository": {
"type": "git",
"url": "git://github.com/antirek/ding-dong.git"
Expand Down
Loading

0 comments on commit 0d62b7f

Please sign in to comment.