Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
alvagi
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
alvagi
Commits
3309cfa1
Commit
3309cfa1
authored
Jul 06, 2015
by
antirek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
remove some lines
parent
fb764f1a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
49 deletions
+56
-49
app.js
example/app.js
+7
-5
command.js
lib/command.js
+1
-0
context.js
lib/context.js
+48
-44
No files found.
example/app.js
View file @
3309cfa1
var
AGIServer
=
require
(
'
./../lib/index
'
);
var
handler
=
function
(
context
)
{
context
.
onEvent
(
'
variables
'
)
context
.
onEvent
(
'
variables
'
)
.
then
(
function
(
vars
)
{
return
context
.
streamFile
(
'
beep
'
);
console
.
log
(
'
vars
'
,
vars
);
return
context
.
streamFile
(
'
beep
'
,
'
#
'
);
})
.
then
(
function
(
result
)
{
return
context
.
setVariable
(
'
RECOGNITION_RESULT
'
,
'
I
\'
m your father, Luc
'
);
})
.
then
(
function
(
result
)
{
return
context
.
end
();
});
})
.
fail
(
console
.
log
);
};
var
agi
=
new
AGIServer
(
handler
);
agi
.
start
(
3000
);
\ No newline at end of file
var
agi
=
new
AGIServer
(
handler
,
{
debug
:
true
});
agi
.
start
(
3007
);
\ No newline at end of file
lib/command.js
View file @
3309cfa1
...
...
@@ -330,6 +330,7 @@ module.exports = [
}
},
{
default
:
'
#
'
,
prepare
:
function
(
value
)
{
return
'
"
'
+
value
+
'
"
'
;
}
...
...
lib/context.js
View file @
3309cfa1
...
...
@@ -4,63 +4,53 @@ var state = require('./state');
var
Q
=
require
(
'
q
'
);
var
commands
=
require
(
'
./command
'
);
//base context
var
Context
=
function
(
stream
,
debug
)
{
EventEmitter
.
call
(
this
);
this
.
debug
=
debug
;
this
.
stream
=
new
Readable
();
//this.stream = stream;
this
.
stream
.
setEncoding
(
'
utf8
'
);
this
.
stream
.
wrap
(
stream
);
this
.
state
=
state
.
init
;
this
.
msg
=
""
;
this
.
variables
=
{};
this
.
pending
=
null
;
var
self
=
this
;
this
.
stream
.
on
(
'
readable
'
,
function
()
{
//always keep the 'leftover' part of the message
//always keep the 'leftover' part of the message
self
.
msg
=
self
.
read
();
});
//this.msg = this.read();
this
.
variables
=
{};
this
.
pending
=
null
;
this
.
stream
.
on
(
'
error
'
,
this
.
emit
.
bind
(
this
,
'
error
'
));
this
.
stream
.
on
(
'
close
'
,
this
.
emit
.
bind
(
this
,
'
close
'
));
};
require
(
'
util
'
).
inherits
(
Context
,
EventEmitter
);
Context
.
prototype
.
read
=
function
()
{
var
buffer
=
this
.
stream
.
read
();
Context
.
prototype
.
read
=
function
()
{
var
buffer
=
this
.
stream
.
read
();
if
(
!
buffer
)
return
this
.
msg
;
this
.
msg
+=
buffer
;
//.toString('utf8');
this
.
msg
+=
buffer
;
if
(
this
.
state
===
state
.
init
)
{
if
(
this
.
msg
.
indexOf
(
'
\n\n
'
)
<
0
)
return
this
.
msg
;
//we don't have whole message
if
(
this
.
msg
.
indexOf
(
'
\n\n
'
)
<
0
)
return
this
.
msg
;
//we don't have whole message
this
.
readVariables
(
this
.
msg
);
}
else
if
(
this
.
state
===
state
.
waiting
)
{
if
(
this
.
msg
.
indexOf
(
'
\n
'
)
<
0
)
return
this
.
msg
;
//we don't have whole message
if
(
this
.
msg
.
indexOf
(
'
\n
'
)
<
0
)
return
this
.
msg
;
//we don't have whole message
this
.
readResponse
(
this
.
msg
);
}
return
""
;
return
''
;
};
Context
.
prototype
.
readVariables
=
function
(
msg
)
{
var
lines
=
msg
.
split
(
'
\n
'
);
/*
for(var i = 0; i < lines.length; i++) {
var line = lines[i];
var split = line.split(':')
var name = split[0];
var value = split[1];
this.variables[name] = (value||'').trim();
}*/
var
lines
=
msg
.
split
(
'
\n
'
);
lines
.
map
(
function
(
line
)
{
var
split
=
line
.
split
(
'
:
'
);
...
...
@@ -71,33 +61,30 @@ Context.prototype.readVariables = function (msg) {
this
.
emit
(
'
variables
'
,
this
.
variables
);
this
.
setState
(
state
.
waiting
);
//return "";
};
Context
.
prototype
.
readResponse
=
function
(
msg
)
{
var
lines
=
msg
.
split
(
'
\n
'
);
/*
for(var i = 0; i < lines.length; i++) {
this.readResponseLine(lines[i]);
}
*/
lines
.
map
(
function
(
line
)
{
this
.
readResponseLine
(
line
);
},
this
);
//return "";
};
Context
.
prototype
.
readResponseLine
=
function
(
line
)
{
if
(
!
line
)
return
;
var
parsed
=
/^
(\d{3})(?:
result=
)(
.*
)
/
.
exec
(
line
);
//var parsed = /^(\d{3})(?: result=)(.*)/.exec(line);
var
parsed
=
/^
(\d{3})(?:
result=
)([^
(
]
*
)(?:\((
.*
)\))?
/
.
exec
(
line
);
if
(
!
parsed
)
{
return
this
.
emit
(
'
hangup
'
);
}
var
response
=
{
code
:
parseInt
(
parsed
[
1
]),
result
:
parsed
[
2
]
result
:
parsed
[
2
]
.
trim
(),
};
//our last command had a pending callback
...
...
@@ -109,11 +96,11 @@ Context.prototype.readResponseLine = function (line) {
this
.
emit
(
'
response
'
,
response
);
}
Context
.
prototype
.
setState
=
function
(
state
)
{
Context
.
prototype
.
setState
=
function
(
state
)
{
this
.
state
=
state
;
};
Context
.
prototype
.
send
=
function
(
msg
,
cb
)
{
Context
.
prototype
.
send
=
function
(
msg
,
cb
)
{
this
.
pending
=
cb
;
this
.
stream
.
write
(
msg
);
};
...
...
@@ -146,16 +133,14 @@ Context.prototype.onEvent = function (event) {
return
defer
.
promise
;
};
Context
.
prototype
.
dial
=
function
(
num
,
timeout
,
params
)
{
return
this
.
exec
(
'
Dial
'
,
num
+
'
,
'
+
timeout
+
'
,
'
+
params
);
};
//additional agi commands
commands
.
map
(
function
(
command
)
{
var
str
=
''
;
Context
.
prototype
[
command
.
name
]
=
function
()
{
if
(
command
.
params
>
0
)
{
var
args
=
[].
slice
.
call
(
arguments
,
0
,
command
.
params
);
str
=
command
.
command
+
"
"
+
prepareArgs
(
args
,
command
.
paramRules
).
join
(
"
"
);
str
=
command
.
command
+
"
"
+
prepareArgs
(
args
,
command
.
paramRules
,
command
.
params
).
join
(
"
"
);
}
else
{
str
=
command
.
command
;
}
...
...
@@ -163,16 +148,35 @@ commands.map(function (command) {
};
});
var
prepareArgs
=
function
(
args
,
argsRules
)
{
var
q
;
if
(
argsRules
)
{
q
=
args
.
map
(
function
(
arg
,
i
)
{
return
(
argsRules
[
i
])
?
argsRules
[
i
].
prepare
(
arg
)
:
arg
;
var
prepareArgs
=
function
(
args
,
argsRules
,
count
)
{
var
q
,
argsP
=
[];
if
(
argsRules
&&
count
)
{
args
=
args
.
map
(
function
(
arg
){
return
arg
.
toString
();
});
for
(
var
i
=
0
;
i
<
count
;
i
++
)
{
argsP
[
i
]
=
(
args
[
i
])
?
args
[
i
]
:
((
argsRules
[
i
]
&&
argsRules
[
i
].
default
)
?
argsRules
[
i
].
default
:
''
);
}
q
=
argsP
.
map
(
function
(
arg
,
i
)
{
return
(
argsRules
[
i
]
&&
argsRules
[
i
].
prepare
)
?
argsRules
[
i
].
prepare
(
arg
)
:
arg
;
});
}
else
{
q
=
args
;
}
return
q
;
}
};
//sugar commands
Context
.
prototype
.
dial
=
function
(
target
,
timeout
,
params
)
{
return
this
.
exec
(
'
Dial
'
,
target
+
'
,
'
+
timeout
+
'
,
'
+
params
);
};
module
.
exports
=
Context
;
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment