sameera
sameera

Reputation: 31

Asterisk AMI AGI Notification PHP Script

I am new to Asterisk. My requirement is when I receive a call I need to identify caller id and pop up that id when answer the call. I have some knowledge in AMI and AGI. I want to know how can I do this using php script.

Any example or something that i can used to do this, please attach here.

Upvotes: 1

Views: 3052

Answers (3)

Enrico Pasqualotto
Enrico Pasqualotto

Reputation: 3

If a popup on a browser is ok for you I suggest to use websocket to notify a webpage about a new call (with all the parameters). You can do it by listening on AMI events and send data to browsers with nodejs, with this you can avoid polling to Asterisk server to check if a call is answered.

Take a look on this code: https://www.backloop.biz/en/products/asterisk-call-notifier-en

Upvotes: 0

Matt Jordan
Matt Jordan

Reputation: 2884

A lot of this depends on the version of Asterisk you're using. I'd recommend using Asterisk 13, as it is both an LTS and has nicer AMI events than other versions.

There are two approaches you could take here. The first is to use AMI, which will spill events back to you over a TCP socket. The second is to use AGI, specifically FastAGI, which will give your remote application control of a channel. In that application, you can then extract the Caller ID yourself.

If you choose to use AMI, you should generally listen for two events: Newchannel - which is raised when a channel is created - and Newcallerid - which is raised when the party identification of the channel is changed. That should cover the vast majority of the time the party identification changes, and should give you both the Caller ID of the channel, as well as the Connected Line information, which is the party identification of the party that channel is talking to.

You shouldn't need the Link subevent in the Bridge event (which doesn't exist in 13, and is replaced by the BridgeEnter/BridgeLeave events) - which is what I think Arheops was referring to. Link is only raised when a channel is 'linked' to another channel in a bridge, and has no bearing on Caller ID. Party identification can change in that situation, but that would be conveyed in the NewcallerId event, so it's pointless listening for it.

Alternatively, you can use FastAGI. If you go down that route, you'll want to have in your dialplan something that calls your FastAGI server:

exten => _X.,1,NoOp()
 same => n,AGI(agi://127.0.0.1)
 same => n,...

In your script - which can be written in a large variety of languages, given the number of AGI libraries are available - you can then extract the CallerID using the get variable command and the CALLERID function. The following is an example in node.js:

return agi.getFullVariable('${CALLERID(NUMBER)}').then(function (number) {
    callerId.number = number;

    return agi.getFullVariable('${CALLERID(NAME)}');
}).then(function (name) {
    callerId.name = name;

As an aside, none of this is "expert" level Asterisk manipulation. It just requires some programming and a basic understanding of Asterisk APIs. Good luck!

Upvotes: 3

arheops
arheops

Reputation: 15259

If you new to asterisk, you can use already developed soft like asternic fop/fop2. It allow do splash with callerid on incoming call and i am sure it will be simpler to got that working.

If you still want do it via asterisk, you should watch for "link" event. However there will be no callerid in that event, so you have also look for "NewChannel" events and for "set" extensions with CALLERID(num)=something, which will alter callerid.

You can connect events using channel uniqueid.

This task is NOT trivial and require be expert in php.

Also you can change asterisk dialplan to trigger UserEvent action with info you need on call answer, after that just collect UserEvents. That require high expertise in asterisk dialplan and low in php.

Upvotes: 0

Related Questions