📜 ⬆️ ⬇️

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 to connections on the TCP port, by default it is 5038. The client program can connect to AMI, send commands to Asterisk, receive a response about the status of command execution.
In this post, we will look at using AMI using the example of solving a specific task: configure Asterisk to generate calls based on a given url, in which the call parameters should be set.

Configure Asterisk AMI



The first thing you need to do is enable AMI and start a user with which the client program will be authenticated:
')
/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, perform the reload:

 asterisk -rx "module reload manager" 


Now we need to create an internal number which, in fact, we will connect with the called subscriber:

/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 number, you will need to re-read the Asterisk configuration:
 asterisk -rx "sip reload" 


This completes the Asterisk setup.



Callback script



Now we are going to create a script in PHP:

callback.php
 <?php # --- define globals --- $strhost = "192.168.0.10"; $strport = "5038"; $timeout = "10"; $num=$_REQUEST['num']; $cid=$_REQUEST['cid']; $c=$_REQUEST['c']; $p=$_REQUEST['p']; $errno=0 ; $errstr=0 ; $sconn = fsockopen ($strhost, $strport, &$errno, &$errstr, $timeout) or die("Connection to $strhost:$strport failed"); if (!$sconn) { echo "$errstr ($errno)<br>\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); } ?> 


You can now verify call generation using the following URL:
 http://domain.com/callback.php?p=1&c=OUT_EXT2&cid=3200&num=84951234567 


The result we see 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)). Usually transmitted as a result of pressing the Enter key and indicates the completion of the transfer 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 arrive at the SIP / 3200 subscriber, as soon as he answers the call, the call will be redirected to the number 84951234567.

Callerid: $ cid
Caller ID to be set for the outgoing channel.

Timeout: 15000
Specify the time to wait for the answer to the call - 15 seconds

Async: yes
Asynchronous call formation allows you to create one or more calls without waiting for an immediate answer.

Source: https://habr.com/ru/post/161521/


All Articles