Asterisk Translator

Required packges (CentOS)

yum install perl-Crypt-SSLeay perl-libwww-perl.noarch sox perl-JSON perl-JSON-XS flac perl-IO-Socket-SSL esound-devel libtool-ltdl
wget http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/mpg123-1.9.1-1.el5.rf.i386.rpm
rpm -i --force mpg123-1.9.1-1.el5.rf.i386.rpm

For Cisco 7970 Authentication, must be set in xml.

/var/www/html/xmlservices/auth/authenticate.php
<?php
echo "AUTHORIZED";
/etc/asterisk/extensions_custom.conf
[g-speech]
;Speech recognition demo also using googletts.agi for text to speech synthesis:
exten => i,1,Answer()
exten => i,n(start),agi(googletts.agi,"Say something in English, when done press the pound key.",en)
exten => i,n(record),agi(speech-recog.agi,en)
;;exten => i,n,Verbose(1,Script returned: ${confidence} , ${utterance})
;;exten => i,n(playback),agi(googletts.agi,"The text you just said was...",en)
exten => i,n,agi(googletranslate.agi,"${utterance}",pt-BR)
;;exten => i,n,Verbose(1,Translated text: ${gtranslation})
exten => i,n,agi(googletts.agi,"${gtranslation}",pt-BR)
exten => i,n,agi(transcript.agi,"${gtranslation}",pt-BR)
exten => i,n,agi(pushtocisco.agi, 10.1.1.2, "http://10.1.1.1/xmlservices/translator/?lang=pt-BR")
exten => i,n,goto(i,start)

XML service for cisco 7970. Reads transcript file and makes an xml.

/var/www/html/xmlservices/translator/index.php
<?php
$lang = $_REQUEST['lang'];
header("Content-type: text/xml");
header("Connection: close");
header("Expires: 1");
//header("Refresh: 2");
$file_data = file_get_contents($lang.'.txt');
echo '<?xml version="1.0" encoding="utf-8" ?>';
?>
<CiscoIPPhoneText>
<Title>Live Translation (<?php echo $lang;?>)</Title>
<Text><?php echo $file_data; ?></Text>
<Prompt></Prompt>
</CiscoIPPhoneText>

Create the transcript

/var/lib/asterisk/agi-bin/transcript.agi
#!/usr/bin/php
<?php
$myFile = "/var/www/html/xmlservices/translator/" . $argv[2] . ".txt";
$file_data = date('H:i:s') . " - " .  $argv[1] . "\n";
$file_data .= file_get_contents($myFile);
file_put_contents($myFile, $file_data);

Push translate URL to cisco 7970

/var/lib/asterisk/agi-bin/pushtocisco.agi
#!/usr/bin/php -q
<?php
 
function push2phone($ip, $uri, $uid, $pwd)
{
        $ip = trim($ip);
        $uri = trim($uri);
 
        $auth = base64_encode($uid.":".$pwd);
        $xml = "<CiscoIPPhoneExecute><ExecuteItem Priority=\"0\"URL=\"".$uri."\"/></CiscoIPPhoneExecute>";
        $xml = "XML=".urlencode($xml);
 
        $post = "POST /CGI/Execute HTTP/1.0\r\n";
        $post .= "Host: $ip\r\n";
        $post .= "Authorization: Basic $auth\r\n";
        $post .= "Connection: close\r\n";
        $post .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $post .= "Content-Length: ".strlen($xml)."\r\n\r\n";
 
        $response = '';
 
        $fp = fsockopen ($ip, 80, $errno, $errstr, 30);
        if (!$fp) {
                //error_log("$errstr ($errno)<br>\n");
        } else {
                fputs($fp, $post.$xml);
                flush();
                while (!feof($fp)) {
                        $response .= fgets($fp, 128);
                        flush();
                }
        }
 
        //error_log($response);
}
 
$uid = "test";
$pwd = "test";
push2phone($argv[1], $argv[2], $uid, $pwd);