Situation
When using Thirdlane with Lumenvox, there is the need to update the grammar file each time an extension is added/modified/deleted. On top of that, the generation of the first grammar file takes some time.
Solution
1) Create a script that will:
- Generate the entire grammar file based on the voicemail.conf file
- Update the grammar file based on the voicemail.conf file when an extension is updated
- Delete the proper entry from the grammar file when an extension is deleted
- Add the proper entry to the grammar file based on the voicemail.conf file when an extension is created
2) Trigger this script from Thirdlane with the corresponding parameters.
Usage
Add
|
1 |
buildgrammar.php add 1102 |
Adds extension 1102 to directory.gram IF IT EXISTS in voicemail.conf
Delete
|
1 |
buildgrammar.php delete 1102 |
Deletes extension 1102 from directory.gram IF IT EXISTS in directory.gram
Update
|
1 |
buildgrammar.php update 1102 |
Updates extension 1102 in directory.gram IF IT EXISTS in directory.gram ONLY IF the name (ie: $jasonshane ) changed.
Generate all
|
1 |
buildgrammar.php generate all |
Generates the entire directory.gram file based on the voicemail.conf file.
Configuration
You need to set the location for voicemail.conf and directory.gram
|
1 2 3 |
/********************* PATHS ***************************/
$voicemailConfFilePath = "/usr/local/www/data-dist/test/tests/lumenvoxgrammar/voicemail.conf";
$grammarFilePath = "/usr/local/www/data-dist/test/tests/lumenvoxgrammar/directory.gram"; |
Code
buildgrammar.php script
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
#!/usr/local/bin/php -q
<?php
/* EXAMPLE COMMANDS
buildgrammar.php add 1102 -> adds extension 1102 to directory.gram IF IT EXISTS in voicemail.conf
buildgrammar.php delete 1102 -> deletes extension 1102 from directory.gram IF IT EXISTS in directory.gram
buildgrammar.php update 1102 -> updates extension 1102 in directory.gram IF IT EXISTS in directory.gram ONLY IF the name (ie: $jasonshane ) changed.
buildgrammar.php generate all -> generates the entire directory.gram file based on the voicemail.conf file
*/
/********************* PATHS ***************************/
$voicemailConfFilePath = "/usr/local/www/data-dist/test/tests/lumenvoxgrammar/voicemail.conf";
$grammarFilePath = "/usr/local/www/data-dist/test/tests/lumenvoxgrammar/directory.gram";
/********************* FILE HEADER *********************/
$fheader =
"#ABNF 1.0;
language en-US;
mode voice;
tag-format <semantics/1.0.2006>;
root \$company_directory;
";
/******************** FUNCTIONS ************************/
function sendError($errorMessage){
echo $errorMessage;
}
function validate($type,$element){
switch ($type){
case 'mode':
if($element == 'add' || $element == 'delete' || $element == 'update' || $element == 'generate'){ $result['tf'] = true; } else { $result['tf'] = false; $result['msg'] = 'invalid_action'; };
break;
case 'number':
if(is_numeric($element) || $element == 'all'){ $result['tf'] = true; } else { $result['tf'] = false; $result['msg'] = 'invalid_number'; };
break;
}
return $result;
}
function file2array($filepath){
if(is_file($filepath)){
$lines = file($filepath);
return $lines;
} else {
return false;
}
}
function write2file($filepath, $lines){
if(is_file($filepath)){
$Handle = fopen($filepath, 'w');
fwrite($Handle, $lines);
fclose($Handle);
}
}
function buildgrammarAll($voicemailConfFilePath,$grammarFilePath,$fheader){
$vmFileArray = file2array($voicemailConfFilePath);
$startWrite = false;
$grammarLines = '';
$grammarDirectory = "\$company_directory = (";
//build $grammarLines and $grammarDirectory
$lineCounter = 0;
$vmFullName = '';
foreach ($vmFileArray as $line){
if( substr (trim($line),0,1) == '[' ){
if( trim($line) == '[default]'){ $startWrite = true;} else { $startWrite = false; }
}
//Set values for $vmAccount and $vmFullName
if( $startWrite && trim($line) != '' ){
$tempDataArray = explode('=>',$line);
$vmAccount = trim($tempDataArray[0]);
$tempDataArray = explode(',',trim($tempDataArray[1]));
$vmFullName = trim($tempDataArray[1]);
}
if(trim($vmFullName) != ''){
$thisname = explode(' ',$vmFullName);
if(count($thisname)>1)
{ $thisnameString = $thisname[0].' ['.$thisname[1].']'; }
else { $thisnameString = $thisname[0]; }
$thisline = "$".strtolower(str_replace(' ','',$vmFullName)). " = (".$thisnameString.") {out=\"".$vmAccount."\"};\n";
$grammarLines .= $thisline;
//$jasonshane = (Jason [Shane]) {out="1411"};
$grammarDirectory .= "$".strtolower(str_replace(' ','',$vmFullName))."|";
//$company_directory = ($jasonshane|$lukeroman|$ketanpatel|$jasonthacker|$kiranpatel|$hasushah|$bartmehta|$receptionist|$operator|$janthornton) {out = rules.latest()};
}
}//endLoop
//Build final string and write to file
$grammarDirectory = substr_replace($grammarDirectory ,"",-1).") {out = rules.latest()};";
$grammarFileFullText = $fheader . $grammarLines . $grammarDirectory;
write2file($grammarFilePath,$grammarFileFullText);
}
function addToGrammar($voicemailConfFilePath,$grammarFilePath,$fheader,$mode,$number){
//read grammar file to grammar array
$grammarFileArray = file2array($grammarFilePath);
//read voicemail.conf file to array
$vmFileArray = file2array($voicemailConfFilePath);
//look for extension in voicemail.conf and get fullname
$vmFullName = '';
$extensionFound = false;
$noFullName = true;
foreach($vmFileArray as $line){
if(!($extensionFound)){
$tempDataArray = explode('=>',$line);
$vmAccount = trim($tempDataArray[0]);
if($vmAccount == $number){
$extensionFound = true;
$tempDataArray = explode(',',trim($tempDataArray[1]));
$vmFullName = trim($tempDataArray[1]);
if(trim($vmFullName) != ''){
$noFullName = false;
}
};
}
}
//If extension and fullname was found then..
if($extensionFound && !($noFullName)){
//build grammar entry with fullname and extension
//$jasonshane = (Jason [Shane]) {out="1411"};
$thisname = explode(' ',$vmFullName);
if(count($thisname)>1){ $thisnameString = $thisname[0].' ['.$thisname[1].']'; }
else { $thisnameString = $thisname[0]; }
$thisline = "$".strtolower(str_replace(' ','',$vmFullName)). " = (".$thisnameString.") {out=\"".$vmAccount."\"};\n";
//$company_directory
//add a new line to the array and copy $company_directory entry to it
$grammarFileArray[] = $grammarFileArray[(count($grammarFileArray)-1)];
//then replace line (n-1) for the new added grammar line
$grammarFileArray[(count($grammarFileArray)-2)] = $thisline;
//finally add the new name to the grammar directory
$grammarFileArray[(count($grammarFileArray)-1)] = str_replace(") {out = rules.latest()};","|$".strtolower(str_replace(' ','',$vmFullName)).") {out = rules.latest()};", $grammarFileArray[(count($grammarFileArray)-1)]);
//write lines to file
foreach($grammarFileArray as $line){
$grammarLines .= $line;
//echo $line;
}
//echo $grammarLines;
write2file($grammarFilePath,$grammarLines);
}
}
function deleteFromGrammar($voicemailConfFilePath,$grammarFilePath,$fheader,$mode,$number){
//read grammar
//loop and build new array without the one that needs to be deleted
//write ALL with new array
//read grammar file to grammar array
$grammarFileArray = file2array($grammarFilePath);
foreach($grammarFileArray as $line){
if( (substr($line,0,1) == '$')){
if( ( stripos($line, "{out=\"".$number."\"}" ) === false ) ){
//add line
$newGrammarFile[] = $line;
} else {
//get $namelastname to remove from directory
$tempData = explode("=",$line);
$theFullName = trim($tempData[0]);
}
}
}
if(count($newGrammarFile)>2 && $theFullName != ''){
//remove from directory
$newGrammarFile[count($newGrammarFile)-1] = str_replace($theFullName,'',$newGrammarFile[count($newGrammarFile)-1]);
//fix string
$newGrammarFile[count($newGrammarFile)-1] = str_replace('||','|',$newGrammarFile[count($newGrammarFile)-1]);
$newGrammarFile[count($newGrammarFile)-1] = str_replace('|)',')',$newGrammarFile[count($newGrammarFile)-1]);
$newGrammarFile[count($newGrammarFile)-1] = str_replace('(|','(',$newGrammarFile[count($newGrammarFile)-1]);
//new file ready, then...
//build string to write
$grammarLines = $fheader;
foreach($newGrammarFile as $line){
$grammarLines .= $line;
//echo $line;
}
//write to file
write2file($grammarFilePath,$grammarLines);
}
}
function updateGrammar($voicemailConfFilePath,$grammarFilePath,$fheader,$mode,$number){
//read grammar
//read voicemail.conf
//look for name in voicemail.conf
//build $fullname
//look for extension in grammar
//IF $fullname found in grammar does not match, then update
//read grammar file to grammar array
$grammarFileArray = file2array($grammarFilePath);
//get fullname from grammar and array line number
$lineCounter = 0;
$lineFound = false;
foreach($grammarFileArray as $line){
if( !(stripos($line, "{out=\"".$number."\"}" )) === false ){
$lineFound = true;
$lineFoundAt = $lineCounter;
//get $namelastname
$tempData = explode("=",$line);
$theFullName = trim($tempData[0]);
//echo $line;
}
$lineCounter ++;
}
//IF it was found, then look in voicemail.conf for the new fullname
// compare vm.fullname with grammar.fullname
// IF != , then update grammar
if($lineFound){
//read voicemail.conf file to array
$vmFileArray = file2array($voicemailConfFilePath);
//look for extension in voicemail.conf and get fullname
$vmFullName = '';
$extensionFound = false;
$noFullName = true;
foreach($vmFileArray as $line){
if(!($extensionFound)){
$tempDataArray = explode('=>',$line);
$vmAccount = trim($tempDataArray[0]);
if($vmAccount == $number){
$extensionFound = true;
$tempDataArray = explode(',',trim($tempDataArray[1]));
$vmFullName = trim($tempDataArray[1]);
if(trim($vmFullName) != ''){
$noFullName = false;
}
};
}
}
//John Doe
$thisname = explode(' ',$vmFullName);
if(count($thisname)>1) {
$thisnameString = $thisname[0].' ['.$thisname[1].']';
} else {
$thisnameString = $thisname[0];
}
$thisline = "$".strtolower(str_replace(' ','',$vmFullName)). " = (".$thisnameString.") {out=\"".$vmAccount."\"};\n";
//$johndoe
$vmFullName = "$".strtolower(str_replace(' ','',trim($vmFullName)));
//compare vm.fullname with grammar.fullname
//update both, the grammar line and the gramar directory entry
if( $vmFullName != $theFullName ){
//Update grammar line
$grammarFileArray[$lineFoundAt] = $thisline;
//Update directory - replace the grammar entry for the vm entry
$directoryLine = $grammarFileArray[count($grammarFileArray)-1];
$directoryLine = str_replace($theFullName, $vmFullName, $directoryLine);
$grammarFileArray[count($grammarFileArray)-1] = $directoryLine;
//build string to write
$grammarLines = $fheader;
foreach($grammarFileArray as $line){
$grammarLines .= $line;
//echo $line;
}
//write to file
write2file($grammarFilePath,$grammarLines);
//echo $grammarLines;
}
/*
echo "\nVMFN :".$vmFullName;
echo "\ngramFL:".$theFullName."\n";
echo $grammarFileArray[$lineFoundAt];
*/
}
if(count($newGrammarFile)>2 && $theFullName != '' && $cocoa == 'loloa'){
//remove from directory
$newGrammarFile[count($newGrammarFile)-1] = str_replace($theFullName,'',$newGrammarFile[count($newGrammarFile)-1]);
//fix string
$newGrammarFile[count($newGrammarFile)-1] = str_replace('||','|',$newGrammarFile[count($newGrammarFile)-1]);
$newGrammarFile[count($newGrammarFile)-1] = str_replace('|)',')',$newGrammarFile[count($newGrammarFile)-1]);
$newGrammarFile[count($newGrammarFile)-1] = str_replace('(|','(',$newGrammarFile[count($newGrammarFile)-1]);
//new file ready
}
//updates extension 1102 in directory.gram IF IT EXISTS in directory.gram ONLY IF the name (ie: $jasonshane ) changed
//echo 'updateGrammar'.'|'.$voicemailConfFilePath.'|'.$grammarFilePath.'|'.$fheader;
}
function buildgrammar($voicemailConfFilePath,$grammarFilePath,$fheader,$mode,$number){
switch ($mode){
case 'generate':
if($number == 'all'){
buildgrammarAll($voicemailConfFilePath,$grammarFilePath,$fheader);
} else {
sendError('generate_all_only');
}
break;
case 'add':
if($number != 'all'){
addToGrammar($voicemailConfFilePath,$grammarFilePath,$fheader,$mode,$number);
} else {
sendError('add_numbers_only');
}
break;
case 'delete':
if($number != 'all'){
deleteFromGrammar($voicemailConfFilePath,$grammarFilePath,$fheader,$mode,$number);
} else {
sendError('delete_numbers_only');
}
break;
case 'update':
if($number != 'all'){
updateGrammar($voicemailConfFilePath,$grammarFilePath,$fheader,$mode,$number);
} else {
sendError('update_numbers_only');
}
break;
}
}
/*******************************************************/
/*******************************************************
* MAIN
******************************************************/
/* avoid dialplan errors */
if($argc != 3){
sendError("not_enough_parameters");
//ends here
} else {
/*load parameters*/
$mode = $argv[1];
$number = $argv[2]; /* can be an extesion number or the "all" word IF mode = generate*/
/*validate parameters*/
$counter = 1;
$isValid['tf'] = true;
$isValid['msg'] = '';
$isValidMode = validate('mode',$mode);
$isValidNumber = validate('number',$number);
if(!($isValidMode['tf'])){
$counter ++;
$isValid['tf'] = false;
$isValid['msg'] = $isValidMode['msg'];
}
if(!($isValidNumber['tf'])){
$isValid['tf'] = false;
if($counter > 1){ $isValid['msg'] .= " | ".$isValidNumber['msg']; } else { $isValid['msg'] = $isValidNumber['msg']; }
}
/*Apply action if validation succeded*/
if($isValid['tf']){
buildgrammar($voicemailConfFilePath,$grammarFilePath,$fheader,$mode,$number);
} else {
sendError($isValid['msg']);
}
}
?> |
Example Configuration Files
directory.gram
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#ABNF 1.0;
language en-US;
mode voice;
tag-format <semantics/1.0.2006>;
root $company_directory;
$johndoe = (John [Doe]) {out="1411"};
$lukeroman = (Luke [Roman]) {out="1400"};
$company_directory = ($johndoe |$janedoe) {out = rules.latest()}; |
voicemail.conf
|
1 2 3 4 5 6 7 8 9 10 11 12 |
;
; Voicemail Configuration
;
...
...
...
[default]
1411 => 1411,John Doe,john.doe@demo.com,,delete=no|attach=yes
1411 => 1411,Jane Doe,jane.doe@demo.com,,delete=no|attach=yes
...
...
... |
