|
| 1 | +#!/usr/bin/php -q |
| 2 | +<?php |
| 3 | +/* |
| 4 | + |
| 5 | + * For FreePBX |
| 6 | + */ |
| 7 | +ob_implicit_flush(false); |
| 8 | +set_time_limit(0); |
| 9 | +error_reporting(0); |
| 10 | + |
| 11 | +$VersionPath = dirname(__FILE__); |
| 12 | +$LibPath = $VersionPath.'/lib'; |
| 13 | + |
| 14 | +$voskServer = 'ws://127.0.0.1:2700'; |
| 15 | +$FD3 = 'php://fd/3'; |
| 16 | +require_once '/etc/freepbx.conf'; // Configure DB and other things |
| 17 | +require_once '/var/lib/asterisk/agi-bin/phpagi.php'; // Load php-agi in FreePBX |
| 18 | + |
| 19 | +$module_name = 'eagiVosk'; |
| 20 | + |
| 21 | +$dbh = \FreePBX::Database(); // Connect to database |
| 22 | +$outGoingPath = \FreePBX::Config()->get("ASTSPOOLDIR")."/outgoing"; // Recieve FreePBX settings |
| 23 | +$monitorPath = \FreePBX::Config()->get("ASTSPOOLDIR")."/monitor"; // Call recordings storage |
| 24 | + |
| 25 | +// Keywords for call directions |
| 26 | +$word_arr = array( |
| 27 | + 'tech' => 'ext-queues,8001,1', // queue 8001 |
| 28 | + 'accountant' => 'from-did-direct,102,1', // accountant 202 |
| 29 | + 'operator' => 'ext-queues,800,1', // quue 800 |
| 30 | +); |
| 31 | + |
| 32 | +$myagi = new AGI(); |
| 33 | +$myagi->answer(); |
| 34 | + |
| 35 | +// Loads websocket library |
| 36 | +require_once("/var/www/html/tts/vosk/scripts/websocket-php-1.3.1/vendor/autoload.php"); |
| 37 | +use WebSocket\Client; |
| 38 | +$client = new Client($voskServer, array('timeout' => 20,'fragment_size'=> 8192)); |
| 39 | +$client->send('{ "config" : { "sample_rate" : 8000 } }', 'text'); |
| 40 | + |
| 41 | +// Play hello sound |
| 42 | +$myagi->exec('PLAYBACK',array('/var/lib/asterisk/sounds/ru/hello-world')); |
| 43 | + |
| 44 | +$myText = array(); |
| 45 | +$totalText = ''; |
| 46 | +$mydata = fopen($FD3, "rb"); // opens file descriptor 3 |
| 47 | +$previous_text = ''; |
| 48 | +while(!feof($mydata)) { |
| 49 | + $data = fread($mydata,8192); |
| 50 | + $client->send($data, 'binary'); |
| 51 | + $receive = $client->receive(); |
| 52 | + |
| 53 | + $result = json_decode($receive, true); |
| 54 | + if(!empty($result['partial'])) { |
| 55 | + $myagi->verbose($result['partial'],3); |
| 56 | + if($previous_text != $result['partial']){ |
| 57 | + $previous_text = $result['partial']; |
| 58 | + foreach ($word_arr as $word => $destination){ |
| 59 | + if (preg_match('/'.$word.'/u', $previous_text , $matches) ){ |
| 60 | + list($context,$ext,$priority) = explode(',',$destination); |
| 61 | + $myagi->exec_dial('Local',$ext.'@'.$context); // calls required direction |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | +} |
| 70 | +$client->send("{\"eof\" : 1}"); |
| 71 | +$receive = $client->receive(); |
| 72 | +fclose($mydata); |
| 73 | + |
| 74 | +$myagi->hangup(); |
| 75 | +?> |
0 commit comments