Skip to content

Commit 38e1edd

Browse files
committed
Add php eagi
1 parent 9f1ae2d commit 38e1edd

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

client-samples/asterisk/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,9 @@ same = n,Hangup()
7979
For more advanced chatbot, database interoperation and other callflow
8080
adjustments modify eagi.py according to your needs. You can also use
8181
PHP/Perl, see other examples in this package.
82+
83+
84+
## EAGI with PHP
85+
86+
For EAGI with PHP on FreePBS see another example script eagi_phpagi.php.
87+
Thanks to Mihail Belobrov <[email protected]> for contribution.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)