Asterisk Manager with JavaScript
Tags: asterisk, beginners, ami, javascript
Published: 2021-05-21 03:35:42

## 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
<a href="https://dev.to/ekim34351855/brief-intro-of-asterisk-manager-1n65">Brief intro of Asterisk Manager</a>


### 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 <a href="https://dev.to/ekim34351855/asterisk-basic-set-up-4ple">my first sharing</a>. 



### Set up your environment

- Install the package we need

``` bash
npm install asterisk-ami-client
```



### Coding section

- Create a ```main.js``` file and copy these 

```js
    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));
    
```

- please replace `"<YOUR USERNAME>"`  and `"<YOUR PASSWORD>"` with your AMI login username and password. 



- Then, it's time to run the code

```bash
    node main.js			# make sure you execute the command in the correct path that has the newly created main.js
```

- 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 !!!