DEV Community

Ekim
Ekim

Posted on

Asterisk Manager with JavaScript

Weekly sharing

Hi everyone, I am Ekim, a fresh Bootcamp graduate and an IT helper (I don't dare to call myself a programmer yet). Every Friday, I will share some of the work that I've done over the last week in a bid to get feedbacks from you guys and record my journey to become a programmer.

Previously

Brief intro of Asterisk Manager

Introduction

This time, we will go through how we could control the asterisk through our codes. Below, I will use JavaScript to illustrate how you could fiddle with asterisk without the CLI (command-line interface). And as the previous sharing, my pjsip.conf and extensions.conf align with my first sharing.

Set up your environment

  • Install the package we need
npm install asterisk-ami-client
Enter fullscreen mode Exit fullscreen mode

Coding section

  • Create a main.js file and copy these
    const AmiClient = require("asterisk-ami-client");
    let client = new AmiClient();

    client
      .connect("<YOUR USERNAME>", "<YOUR PASSWORD>", { host: "127.0.0.1", port: 5038 })     // connect to your AMI remotely
      .then(() => {

        client
            .on('connect', () => console.log('connect'))                // show connection logs in terminal
            .on('event', event => console.log(event))                   // show AMI event logs in terminal
            .on('response', response => console.log(response))          // show response logs in terminal
            .on('disconnect', () => console.log('disconnect'))          // show disconnection logs in terminal
            .on('reconnection', () => console.log('reconnection'))      // show reconnection logs in terminal
            .on('internalError', error => console.log(error))           // show AMI error logs in terminal
            .action({                                                   // manager action   
                Action:"Originate",                                     // Originate call
                Channel:"PJSIP/7000",                                   // calling from endpoint 7000
                Exten:"7100",                                           // expected to be received by endpoint 7100
                Context:"interaction",
                Priority:"1",
            })

    })
    .catch(error => console.log(error));

Enter fullscreen mode Exit fullscreen mode
  • please replace "<YOUR USERNAME>" and "<YOUR PASSWORD>" with your AMI login username and password.

  • Then, it's time to run the code

    node main.js            # make sure you execute the command in the correct path that has the newly created main.js
Enter fullscreen mode Exit fullscreen mode
  • You should now be able to originate a call without using the CLI. And in your terminal, your could see all the states and processes of asterisk when you connect to the AMI and originate the call. You could use them to do different kinds of things.

  • For example, think about converting voice messages in the voicemail into texts, and then send those texts to a telegram group. Wouldn't it be amazing?

Conclusion

  • It is a short and simple sharing, but you could do various things based on it. I tend to keep things easy and avoid people getting overwhelmed by the asterisk thing. I hope you enjoy my reading so far. In the meantime, stay healthy and stay tuned for more content !!!

Top comments (0)