Advertisement
Maikl5700

Asteriks node

Dec 6th, 2022
1,193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * port:  port server
  3.  * host: host server
  4.  * username: username for authentication
  5.  * password: username's password for authentication
  6.  * events: this parameter determines whether events are emited.
  7.  **/
  8.  
  9. import { HOST, PASSWORD, PORT, USERNAME } from "./config";
  10.  
  11. const ami = require('asterisk-manager')(PORT, HOST, USERNAME, PASSWORD, true);
  12.  
  13. // In case of any connectiviy problems we got you coverd.
  14. ami.keepConnected();
  15.  
  16. let gettingQueueStatuResponse = false;
  17.  
  18. function getQueueStatus() {
  19.     gettingQueueStatuResponse = true;
  20.     ami.action({
  21.         'action': 'queues',
  22.         // 'channel':'SIP/myphone',
  23.         // 'context':'default',
  24.         // 'exten': '',
  25.         // 'priority': '1',
  26.         // 'variable':{
  27.         //   'name1':'value1',
  28.         //   'name2':'value2'
  29.         // }
  30.     }, (err, res) => {
  31.         console.log(err);
  32.         console.log(res);
  33.     });
  34. }
  35.  
  36. const queueMembers = [] as any[];
  37.  
  38. async function main() {
  39.     // Listen for any/all AMI events.
  40.     ami.on('managerevent', (event) => {
  41.         console.log('managerevent', event.event);
  42.  
  43.         if (event.event.includes('Agent')) {
  44.             console.log(event);
  45.         }    
  46.  
  47.         if (event.event === 'QueueParams') {
  48.             gettingQueueStatuResponse = true;
  49.         }
  50.  
  51.         if (event.event === 'QueueMember' && gettingQueueStatuResponse) {
  52.             return queueMembers.push(event);
  53.         }
  54.        
  55.         if (event.event === 'QueueStatusComplete') {
  56.             gettingQueueStatuResponse = false;
  57.             return console.log(queueMembers);
  58.         }
  59.     });
  60.  
  61.  
  62.     // Listen for specific AMI events. A list of event names can be found at
  63.     // https://wiki.asterisk.org/wiki/display/AST/Asterisk+11+AMI+Events
  64.     ami.on('hangup', (event) => {
  65.         console.log('hangup', event);
  66.     });
  67.  
  68.     ami.on('confbridgejoin', (event) => {
  69.         console.log('confbridgejoin', event);
  70.     });
  71.  
  72.     // Listen for Action responses.
  73.     ami.on('response', (event) => {
  74.         console.log('response', event);
  75.     });
  76.  
  77.     await new Promise(resolve => setTimeout(resolve, 2000));
  78.     // getQueueStatus();
  79.  
  80.  
  81.     ami.action({
  82.         'action': 'queuestatus',
  83.         // 'channel':'SIP/myphone',
  84.         // 'context':'default',
  85.         // 'exten': '',
  86.         // 'priority': '1',
  87.         // 'variable':{
  88.         //   'name1':'value1',
  89.         //   'name2':'value2'
  90.         // }
  91.     }, (err, res) => {
  92.         console.log(err);
  93.         console.log(res);
  94.     });
  95.  
  96.     // await new Promise(resolve => setTimeout(resolve, 2000));
  97.  
  98.     // ami.action({
  99.     //     'action':'queuestatus',
  100.     //     // 'channel':'SIP/myphone',
  101.     //     // 'context':'default',
  102.     //     // 'exten': '',
  103.     //     // 'priority': '1',
  104.     //     // 'variable':{
  105.     //     //   'name1':'value1',
  106.     //     //   'name2':'value2'
  107.     //     // }
  108.     // }, (err, res) => {
  109.     //     console.log(err);
  110.     //     console.log(res);
  111.     // });
  112.  
  113.     return;
  114. }
  115. main();
  116.  
  117. // Perform an AMI Action. A list of actions can be found at
  118. // https://wiki.asterisk.org/wiki/display/AST/Asterisk+11+AMI+Actions
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement