#!/usr/bin/php -q verbose("No TTS text provided", 3); exit; } $text = trim($argv[1]); $agi->verbose("Incoming TTS: $text", 3); /* 2) BUILD GOOGLE URL */ $encoded = urlencode($text); $tts_url = "https://translate.google.com/translate_tts?ie=UTF-8&q={$encoded}&tl=en&client=tw-ob"; /* 3) CACHE DIRECTORY */ $cache_dir = "/var/lib/asterisk/sounds/tts_cache"; if (!is_dir($cache_dir)) { mkdir($cache_dir, 0755, true); } /* 4) BUILD HASHED CACHE FILENAME */ $hash = md5($text); $mp3_file = "$cache_dir/$hash.mp3"; $agi->verbose("Cache filename: $mp3_file", 3); /* 5) CHECK CACHE FIRST */ if (file_exists($mp3_file) && filesize($mp3_file) > 200) { $agi->verbose("Cache hit! Playing existing file...", 3); $agi->exec("MP3Player", $mp3_file); exit; } /* 6) NOT CACHED — DOWNLOAD FROM GOOGLE */ $agi->verbose("Cache miss — downloading from Google...", 3); $cmd = "wget -q -U Mozilla -O '$mp3_file' '$tts_url'"; exec($cmd); if (!file_exists($mp3_file) || filesize($mp3_file) < 200) { $agi->verbose("TTS download failed", 3); unlink($mp3_file); exit; } $agi->verbose("Download complete — playing MP3", 3); /* 7) PLAY NEW FILE */ $agi->exec("MP3Player", $mp3_file); /* 8) Done — file stays cached for future calls */ ?>