Guest User

Untitled

a guest
Jun 19th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. // Include required modules.
  2. var net = require('net');
  3. var sys = require('sys');
  4.  
  5. // Create a TCP server and listen on FastAGI port.
  6. var server = net.createServer();
  7. server.listen(4573, '127.0.0.7');
  8.  
  9. // Add a listener for new connections.
  10. server.addListener('connection', fastAGI);
  11.  
  12. // An array to hold AGI variables submitted from Asterisk.
  13. var agiVars = new Array();
  14.  
  15. // An array to hold the set of commands we want to send back to Asterisk.
  16. var commands = new Array();
  17.  
  18. // Return value provided by Asterisk after AGI commands are executed.
  19. var returnValue;
  20.  
  21. /******************************************************************************
  22. * Asterisk AGI Commands.
  23. ******************************************************************************
  24. */
  25.  
  26. // Method to stream an audio file.
  27. function streamFile(file, escapeDigits, offset) {
  28. var command = "STREAM FILE " + file + " \"" + escapeDigits + "\"" ;
  29. if(typeof(offset) != 'undefined') { command += " " + offset; }
  30. command += "\n";
  31. commands.push(command);
  32. }
  33.  
  34. // Method to say a number.
  35. function sayNumber(number, escapeDigits) {
  36. var command = "SAY NUMBER " + number + " \"" + escapeDigits + "\"" + "\n" ;
  37. commands.push(command);
  38. }
  39.  
  40. // Method to tell Asterisk to hangup the channel.
  41. function hangup(channel) {
  42. var command = "HANGUP";
  43. if(typeof(channel) != 'undefined') { command += " " + channel; }
  44. command += "\n";
  45. commands.push(command);
  46. }
  47.  
  48. /******************************************************************************
  49. * Helper methods.
  50. ******************************************************************************
  51. */
  52.  
  53. // Method to access AGI variables submitted from Asterisk.
  54. function getagiVars(data) {
  55. var values = data.toString().split("\n");
  56. for(i=1; i < values.length; i++) {
  57. var temp = values[i].split(":");
  58. agiVars[temp[0]] = temp[1];
  59. }
  60. }
  61.  
  62. // Method to extract the return value from Asterisk response.
  63. function getReturnValue(data) {
  64. returnValue = data.substr((data.indexOf("=")+1),1);
  65. }
  66.  
  67. // Method to prepare the response before sending commands to Asterisk
  68. // Call before stream.write().
  69. function prepareResponse() {
  70. commands.reverse();
  71. }
  72.  
  73. // Method to render commands one at a time from the commands array.
  74. function renderCommands() {
  75. return commands.pop();
  76. }
  77.  
  78. // Prototype method to create size property for agiVars array.
  79. Array.prototype.size = function () {
  80. var size = this.length ? --this.length : -1;
  81. for (var item in this) {
  82. size++;
  83. }
  84. return size;
  85. }
  86.  
  87. // Method to execute AGI logic.
  88. function fastAGI(stream) {
  89.  
  90. stream.setEncoding('utf8');
  91.  
  92. stream.addListener('connect', function() {
  93. sys.puts("Got a connection from Asterisk!");
  94. });
  95.  
  96. stream.addListener('data', function(data) {
  97.  
  98. // When Asterisk starts the AGI script, it will pass channel variables.
  99. if(!agiVars.size()) {
  100.  
  101. // Populate agiVars array.
  102. getagiVars(data);
  103.  
  104. // Write some debug output.
  105. sys.puts("Getting a call from: " + agiVars["agi_calleridname"]);
  106.  
  107. // Set up the commands to send back to Asterisk, populate the commands array.
  108. streamFile("hello-world", "#");
  109. streamFile("tt-monkeys", "#");
  110. streamFile("goodbye", "#");
  111. hangup();
  112.  
  113. // Prepare the response (just reverses the commands array).
  114. prepareResponse();
  115.  
  116. // Start sending commands.
  117. stream.write(renderCommands());
  118.  
  119. }
  120.  
  121. // With subnsequent responses, Asterisk will send a response code with return value.
  122. else {
  123.  
  124. // Check the return value from Asterisk.
  125. getReturnValue(data);
  126.  
  127. // After we tell Asterisk to streak audio files, we get a 0 return.
  128. if(returnValue == 0) {
  129. stream.write(renderCommands()); // Send the next command.
  130. }
  131. // After we tell Asterisk to hangup, we get a 1 return.
  132. else {
  133. stream.end();
  134. }
  135.  
  136. }
  137.  
  138. });
  139.  
  140. // We don't see this event until we call stream.end().
  141. stream.addListener('end', function() {
  142. sys.puts("Goodbye Asterisk.");
  143. agiVars = new Array();
  144. commands = new Array();
  145. });
  146.  
  147. stream.addListener('error', function() {
  148. stream.end();
  149. });
  150.  
  151. }
Add Comment
Please, Sign In to add comment