Asterisk: callback using AMI
Asterisk Manager Interface (AMI) is a software interface that allows external programs to both manage and control the Asterisk system. AMI listens for connections on the TCP port, by default it is 5038. The client program can connect to AMI, send commands to Asterisk, and receive a response about the status of command execution.
In this post, we will consider using AMI as an example of solving a certain problem: configure Asterisk to generate calls by a given url, in which call parameters should be set.
The first thing to do is enable AMI and have a user with which the client program authenticates:
/etc/asterisk/manager.conf
/etc/asterisk/manager.conf
To apply the changes, reload:
Now we need to create an extension number which, in fact, will be connected to the called party:
/etc/asterisk/sip.conf
After creating the extension, you will need to re-read the Asterisk configuration:
This completes the setup of Asterisk.
Now let's move on to creating a script in PHP:
callback.php
Now you can check the generation of the call using the following URL:
We see the result in the console:
Here are some comments that may be useful to practitioners:
\ r \ n - carriage return with line feed (Carriage Return + Line Feed (CR + LF)). It is usually transmitted by pressing the Enter key and indicates the completion of the command.
Events: off
In this case, we disable sending events to this AMI interface connection. Basically, its value is always off
Channel: SIP / $ cid
The name of the channel to which the call is addressed. In our case, the call will first be sent to the SIP / 3200 subscriber, as soon as he picks up the phone, the call will be redirected to the number 84951234567.
Callerid: $ cid
Caller ID, which must be set for the outgoing channel.
Timeout: 15000
We indicate the waiting time for answering a call - 15 seconds
Async: yes
Asynchronous generation of calls allows you to create one or more calls without waiting for an immediate answer.
In this post, we will consider using AMI as an example of solving a certain problem: configure Asterisk to generate calls by a given url, in which call parameters should be set.
Configuring Asterisk AMI
The first thing to do is enable AMI and have a user with which the client program authenticates:
/etc/asterisk/manager.conf
[general]
enabled = yes
port = 5038
bindaddr = 0.0.0.0
/etc/asterisk/manager.conf
[c2call]
secret=FrUyHn6FSaX
deny=0.0.0.0/0.0.0.0
permit=192.168.0.0/255.255.0.0
read=system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate
write=system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate
To apply the changes, reload:
asterisk -rx "module reload manager"
Now we need to create an extension number which, in fact, will be connected to the called party:
/etc/asterisk/sip.conf
[3200]
deny=0.0.0.0/0.0.0.0
permit=192.168.0.0/255.255.0.0
secret=3200
dtmfmode=rfc2833
canreinvite=no
context=OUT_IN1
host=dynamic
type=friend
nat=yes
port=5060
qualify=yes
callcounter=yes
faxdetect=no
After creating the extension, you will need to re-read the Asterisk configuration:
asterisk -rx "sip reload"
This completes the setup of Asterisk.
Callback script
Now let's move on to creating a script in PHP:
callback.php
\n"; }
else {
fputs ($sconn, "Action: login\r\n");
fputs ($sconn, "Username: c2call\r\n");
fputs ($sconn, "Secret: FrUyHn6FSaX\r\n");
fputs ($sconn, "Events: off\r\n\r\n");
usleep(500);
fputs ($sconn, "Action: Originate\r\n");
fputs ($sconn, "Channel: SIP/$cid\r\n");
fputs ($sconn, "Callerid: $cid\r\n");
fputs ($sconn, "Timeout: 15000\r\n");
fputs ($sconn, "Context: $c\r\n");
fputs ($sconn, "Exten: $num\r\n");
fputs ($sconn, "Priority: $p\r\n\r\n");
fputs ($sconn, "Async: yes\r\n\r\n" );
fputs ($sconn, "Action: Logoff\r\n\r\n");
usleep (500);
fclose ($sconn);
}
?>
Now you can check the generation of the call using the following URL:
http://domain.com/callback.php?p=1&c=OUT_EXT2&cid=3200&num=84951234567
We see the result in the console:
*CLI> == Manager 'c2call' logged on from 192.168.0.11
== Using SIP RTP CoS mark 5
-- Executing [84951234567@OUT_EXT2:1] Dial("SIP/3200-0000000a", "SIP/84951234567@TRK1") in new stack
== Manager 'c2call' logged off from 192.168.0.11
== Using SIP RTP CoS mark 5
-- Called SIP/84951234567@TRK1
-- SIP/TRK1-0000000b is ringing
== Spawn extension (OUT_EXT2, 84951234567, 1) exited non-zero on 'SIP/3200-0000000a'
Here are some comments that may be useful to practitioners:
\ r \ n - carriage return with line feed (Carriage Return + Line Feed (CR + LF)). It is usually transmitted by pressing the Enter key and indicates the completion of the command.
Events: off
In this case, we disable sending events to this AMI interface connection. Basically, its value is always off
Channel: SIP / $ cid
The name of the channel to which the call is addressed. In our case, the call will first be sent to the SIP / 3200 subscriber, as soon as he picks up the phone, the call will be redirected to the number 84951234567.
Callerid: $ cid
Caller ID, which must be set for the outgoing channel.
Timeout: 15000
We indicate the waiting time for answering a call - 15 seconds
Async: yes
Asynchronous generation of calls allows you to create one or more calls without waiting for an immediate answer.