Proof of concept: Automatically transfer Google Voice voicemail to Asterisk voicemail

 

Important
This article is a guest post. We may not be able to answer questions about this article.

The following should be considered proof-of-concept material. Feel free to use and/or modify it at your own risk.

The problem: You have Google Voice calls coming into an Asterisk server but even though you take care to answer calls when they arrive, every so often some hiccup causes voicemails to be left in Google Voice’s voicemail, and no one checks those. The solution: Transfer those voicemails to Asterisk’s voicemail box for that user.

The following assumes that there is already an active voicemail box established for the target user.

Prerequisites: php-xml (do yum install php-xml on a Centos-based system). Also, go to the Google-Voice-PHP-API page on GitHub and grab the file GoogleVoice.php – be sure to get the Raw version of the file. It is suggested that you place it your root directory, and make it executable. Note that if you have downloaded this file in the past, you should check to make sure you have the most recent version, since older versions available prior to June, 2013 did not include the ability to download voicemails.

Then in the same directory create a new file. Call it anything you want but give it a .php extension:

<?php

include('/root/GoogleVoice.php');

// NOTE: If you get errors from GoogleVoice.php, try installing the php-xml package
// You must change the name and password on the following line. NOTE: Full email address required.
$gv = new GoogleVoice('gv_user@gmail.com', 'gv_account_password');
// On the next line insert the extension number of the target voice mail box.  Must have voicemail enabled!
$vmbox = '1111';
// On the next line specify an email address for delivery of new voicemail notification.  Use $email=FALSE; if you don't want this.
// Note that this will not send the voicemail as an attachment so if you want that, do it from within the Google Voice account.
$email="username@gmail.com";

// Don't change anything below unless you know what you are doing!

// Set path to user's mailbox
$dir = '/var/spool/asterisk/voicemail/default/'.$vmbox.'/INBOX/msg';

// Set flag that determines if message waiting indications will be updated
$mwi = FALSE;

// Get data on all unread Google Voice voicemail messages into array.
$voice_mails = $gv->getUnreadVoicemail();
$msgIDs = array();
$keys = array_keys($voice_mails);

// process each available message
foreach($voice_mails as $v) {
	//	echo 'Message id: '.$v->id.' from '.$v->phoneNumber.' on '.$v->displayStartDateTime.': '.$v->messageText."<br><br>\n";

	// Downloads individual voicemail
	// Uses getVoiceMailMP3 function added to GoogleVoice.php - WILL FAIL with unmodified GoogleVoice.php 
	$mp3 = $gv->getVoicemailMP3($v->id);

	// construct temporary filenames used
	$mp3file = $v->id.".mp3";
	$wavfile = $v->id.".wav";

	// write temporary mp3 file to disk
	$fh = fopen($mp3file, 'w') or die("can't open file");
	fwrite($fh, $mp3);
	fclose($fh);

	// convert mp3 to temporary wav file
	system("mpg123 -q -w mp3towavtmp.wav ".$mp3file);

	// convert wav file to format required by Asterisk
	system("sox mp3towavtmp.wav -r 8000 -c 1 ".$wavfile." vol 3 dB");

	// If caller's number starts with +1 strip it
	$number=$v->phoneNumber;
	if (substr($number, 0, 2) == '+1') {
		$number=substr($number, 2);
	}

	// Calculate approximate duration of file from file size
	$duration=(int) (filesize($wavfile)/16000);

	// Construct Message Information file for Asterisk - following must be one long continuous line!
	$txt=";\n; Message Information file\n;\n[message]\norigmailbox=$vmbox\ncontext=macro-vm\nmacrocontext=ext-local\nexten=s-NOANSWER\nrdnis=unknown\npriority=2\ncallerchan=\ncallerid=<$number>\norigdate=$v->displayStartDateTime\norigtime=".substr($v->startTime,0,10)."\ncategory=\nflag=\nduration=$duration\n";

	// Find next unused voicemail file number in Asterisk voicemail directory
	$exists = TRUE;
	$i = 0;
	while ($exists) {
		$file=$dir.substr("000$i",-4).'.txt';    
		$exists = file_exists($file);
		$i++;
	}
	$i--;

	// $file will contain path and base filename of voicemail files, without extension
	$file = $dir.substr("000$i",-4);

	// Write Message Information file for this voicemail
	$fh = fopen($file.".txt", 'w') or die("can't open file");
	fwrite($fh, $txt);
	fclose($fh);

	// Change owner and group of file to asterisk
	chown($file.'.txt', "asterisk");
	chgrp($file.'.txt', "asterisk");

	// Move wav file to voicemail directory with proper filename
	rename($wavfile, $file.".wav");

	// Change owner	and group of file to asterisk
	chown($file.'.wav', "asterisk");
	chgrp($file.'.wav', "asterisk");

	// Mark message read in Google Voice so we don't read it again
	if(!in_array($v->id, $msgIDs)) {
		// Mark the message as read in your Google Voice Voicemail.
		$gv->markMessageRead($v->id);
		$msgIDs[] = $v->id;
	}

	// If email address specified construct and send email to user
	// Wanted option to attach voicemail but couldn't figure out how to do it correctly. uuencode does NOT work.
	if ($email) {
		// Construct email text
		$txt="There is a new voicemail in mailbox $vmbox that was imported from Google Voice's voicemail system:\n\n\tFrom:\t$number\n\tLength:\t$duration seconds\n\tDate:\t$v->displayStartDateTime\n";
		if ($v->messageText) {
			$txt=$txt."\tTranscription:\t$v->messageText\n";
		}
		$txt=$txt."\nDial *98 to access your voicemail by phone.\n";
		$i++;
		`echo "$txt" | mail -s "New message $i in mailbox $vmbox" "$email"`;
	}

	// Remove temporary files
	unlink($mp3file);
	unlink('mp3towavtmp.wav');

	// Set flag to indicate message waiting indication needs to be reset
	$mwi = TRUE;

}

// If messages were processed tell Asterisk to reset message waiting indications
// This may not be the best way to do this but only way I know
if ($mwi) {
	`/usr/sbin/asterisk -rx "voicemail reload"`;
}

?>

After you have changed the information at the top of the above code so it is correct for your installation, save it to a file with the extension .php and don’t forget to make it executable. Remember, this is proof-of-concept code, and we do not guarantee that it is ready for use in a production environment. It could probably benefit from additional error checking and possibly other enhancements.

You may want to call the above file on a schedule, perhaps every 15 minutes or every hour, depending on how urgently you want to receive Google Voice voicemails. I would not do it too often because you never know if Google would feel you are abusing their system with very frequent accesses. Note that when you call the script you’ll need to call it using php, for example:

php scriptname.php

and if that doesn’t work try adding the -f option after “php” but before the script name. Also, if you are running multiple copies of this script from a bash script (because you are collecting voicemail for more than one Google Voice user) then use a sleep statement between executions (such as sleep 30), otherwise you may see errors such as this:

PHP Fatal error: Uncaught exception ‘Exception’ with message ‘Could not log in to Google Voice with username: user@gmail.com’ in /root/GoogleVoice.php:67

Note that when this inserts voicemails into the Asterisk voicemail directory, it is not quite the same as when using a user leaves a voicemail. The known differences are:

  • When a user leaves a voicemail, the audio is saved in two files, a larger one with a .wav extension and a smaller one with a .WAV extension. The larger one is standard .wav format but the smaller seems to be some sort of GSM file, and I cannot figure out how to get SOX to create a compatible file, so I just don’t bother with it. It doesn’t seem to be needed anyway.
  • While this will let you send a notification e-mail to the message recipient, it does not let you attach the mp3 or wav file to the email. Again, don’t know how, but remember you can configure Google Voice to send a notification (with out without audio file attachment) when someone leaves a voicemail.

I hope someone will expand upon this to create something a bit more robust.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

Recent Comments

Archives

Categories

Meta

GiottoPress by Enrique Chavez