📜 ⬆️ ⬇️

Extending Asterisk with PHP

Everyone has heard about the new generation PBX whose name is Asterisk. It so happened that I became interested in this system and even managed to make a couple of commercial projects.

In this article I want to tell a little about the integration of asterisks with the php programming language. In this case, we will use the class phpagi .

Under the cat, I will give examples of using several methods of this class that helped me.
')

First of all, download the latest version of phpagi and connect it to our project, as well as edit the /etc/asterisk/manager.conf file

; ; Asterisk Call Management support ; [general] enabled = yes ;  asterisk manager interface (AMI) port = 5038 bindaddr = 127.0.0.1 ;       webenabled = no ; Each user has a section labeled with the username ; so this is the section for the user named "mark" [user] ;     secret = qwerty ;  deny=0.0.0.0/0.0.0.0 ;     permit=127.0.0.1/255.255.255.0 read = system,call,log,verbose,command,agent,user,originate ;      write = system,call,log,verbose,command,agent,user,originate 


There is a phpagi.conf file in the archive with phpagi, it needs to be copied to / etc / asterisk and it is natural to fix the login and password.
Now we can safely connect to the AMI from the php script, like this:

 include('phpagi.php'); $manager = new AGI_AsteriskManager(); $manager->connect(); //    phpagi.conf     , , . 


First of all, I would like to tell you about writing a simple asterisk event monitor in php.
I think this is the most useful feature of the phpagi class.

This is how I got an event monitor:
 function dump_events($ecode,$data,$server,$port) { $date_now = date('Ym-d'); $time_now = date('H:i:s'); echo "$time_now : received event '$ecode' from $server:$port\n"; print_r($data); } include('phpagi.php'); $manager = new AGI_AsteriskManager(); $manager->connect(); $manager->add_event_handler('*', 'dump_events'); //       //   AMI    //    $manager->wait_response(); //   ,      //         sleep() $manager->disconnect(); 


Using this handler, you can perform any actions depending on the event received, for example, check the balance on the sim card inserted into the huawei modem and connected via chan_dongle.
I will give an example of my implementation using the Command method:
The first script catches the newussd event.
 function donglenewussd($ecode, $data) { if($model = Trunk::model()->find('value = :value', array( ':value' => $data['Device']))){ if(!empty($data['MessageLine0'])){ $balance = explode(' ', $data['MessageLine0']); switch($model->carrier){ case '0': break; case '1': $model->balance = $balance[0]; $model->save(); echo $balance[0]."\n"; break; case '2': $model->balance = $balance[2]; $model->save(); echo $balance[2]."\n"; break; case '3': preg_match('/[+-]?\d+\.?\d*/', $balance[1], $match); $model->balance = $match[0]; $model->save(); echo $match[0]."\n"; break; } } } } $manager = new AGI_AsteriskManager(); $manager->connect(); $manager->add_event_handler('donglenewussd', 'donglenewussd'); $manager->wait_response(); $manager->disconnect(); 


This script receives a donglenewussd event in which we receive a response from the operator based on which we enter into the database information about the state of balance.
The following script will crown, say once an hour, send ussd a request to check the balance.

  $manager = new AGI_AsteriskManager(); $manager->connect(); $trunks =Trunk::model()->findAll(); foreach($trunks as $trunk){ switch($trunk->carrier){ case '1': $manager->Command('dongle ussd '.$trunk->value.' *101#'); break; case '2': $manager->Command('dongle ussd '.$trunk->value.' *111#'); break; case '3': $manager->Command('dongle ussd '.$trunk->value.' *111#'); break; } } $manager->disconnect(); 


As you can see, I use the yii framework for my projects, I have a model in which modem settings are stored (system name, operator, balance, status, etc.)

This example works with Ukrainian operators (MTS, Kyivstar and Life)

And for dessert, I want to tell you about the Originate method. Are you still using call files? Then we go to you.

A very useful function that initiates a call using AMI, and not the old old-fashioned way by copying the call file to the / var / spool / asterisk / outgoing directory

All parameters passed to the function are almost the same as the parameters of the call file:
 $manager->Originate( '  ,  SIP/1001', '  ', ' ', '  ', '    ,  playback', ' ,    ', '', '       ', '  ', 'account -  ,   ', '    (       )', 'actionid -    ' ); 


Well, what to do with this function, I think you think up yourself, and if you combine it with the event manager, you can also get a report on the performance of the outgoing call.

I hope my article will be useful to someone, since I did not find a mention of phpagi on Habré, and in general I found it difficult to find at least some examples of use other than those that go with the library.

If anyone has other methods of working with this library, I will be very glad to read about them in comments.

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


All Articles