asterisk / node-ari-client

Node.js client for ARI. This library is best effort with limited support.
Other
259 stars 102 forks source link

Can i use originate for outbound call using sip trunk #150

Open unique1o1 opened 3 years ago

unique1o1 commented 3 years ago

I'm new to asterisk and I'm having trouble grasping all the nuances.

I have successfully made calls to inbound extension using the originate function and played some audio files. But now I need to call real-world phone numbers. I have already set up sip trunk in my asterisk config but I'm having an issue calling through sip/trunk.

   await channel.originate({
        context: "from-internal",
        endpoint: "SIP/TRUNK_ABC/XXXXXX",
        // app: "channel-dump",
        extension: "test",
        callerId: "asterisk",
      });

Running the following code just gives me an allocation failed error.

Can anyone point me in the right direction? Thanks

dioris-moreno commented 3 years ago

Use a function like this (outboundCalll) to make outbound calls. Take into consideration the following:

  1. Using PJSIP the endpoint format is PJSIP/1235551234@outbound, where "outbound" is the peer you want to use from pjsip.conf. What version of Asterisk are you using?
  2. Listen to StasisStart event on the client instance, it will be triggered when the call is answered.
  3. Use appArgs to identify your outbound calls entering the application. If you are using snoop channels, for example, StasisStart event will be also triggered.

` import Ari from 'ari-client';

const outboundCalll = async () => { try { const url = 'http://myasterisk.com:8088/ari'; const username = 'asterisk'; const password = 'asterisk';

    const client = await Ari.connect(url, username, password);
    console.log(`Connected to ${url}`);

    client.on('StasisStart', async (event, channel) => {
        console.log('call answered:', JSON.stringify(event));

        // Ignore other channels entering the application (e.g. snoop channels).
        if (event.args[0] !== 'dialed') return;

        channel.once('ChannelHangupRequest', () => {
            console.log('ChannelHangupRequest');
        });

        // Your code here
    });

    const endpoint = 'PJSIP/1235551234@outbound';
    const app = 'channel-dump';

    client.start(app, async err => {
        if (err) return console.log('application start error:', err);

        console.log('application started!');

        try {
            const outChannel = client.Channel();

            outChannel.on('ChannelDestroyed', (event, channel) => {
                console.log('channel destroyed', channel.id);
            });

            await outChannel.originate({
                endpoint,
                app,
                appArgs: 'dialed',
            });

            console.log('channel created:', outChannel.id);
        } catch (error) {
            console.log('error while creating channel:', error);
        }
    });
} catch (err) {
    console.log(err);
}

}; `