Voicemail

From SoftIVR

Jump to: navigation, search

Voicemail

This article shows how to build a simple voicemail application, which we'll then extend in a few interesting ways.

Our first iteration just asks the user to leave a message after the tone, beeps, records the message and e-mails it to the recipient. The code is as follows:

answer();
say("Please leave your message after the tone.");
playTone(400, 0.5);
onHangup('sendMail("support@softivr.com", "Voicemail", "You have a voicemail from " + call.callerID, "vm.wav");');
recordFile("vm.wav");

The onHangup function specifies a bit of Javascript to be executed when the caller hangs up. In this instance, it's a call to SoftIVR's sendMail function, which e-mails the recorded message to (in this case) support@softivr.com.

There are a few problems with this script as it stands. The first is that it cannot cope with more than one caller at a time: they'll both try to record 'vm.wav', and it's likely that at least one of the messages will get lost. Each call has a unique identifier associated with it, which is available in call.UID, so we can add that to the filename and the problem's resolved. Our code now looks like this:

answer();
say("Please leave your message after the tone.");
playTone(400, 0.5);
onHangup('sendMail("support@softivr.com", "Voicemail", "You have a voicemail from " + call.callerID, "vm" + call.UID + ".wav");');
recordFile("vm" + call.UID + ".wav");

The next problem is that our disc will start to fill up with these files, as each call results in a new file being created. So we can add a call to the deleteFile function to delete the file after it's been e-mailed; for readability, we'll also separate that hangup processing out in to a separate subroutine.

// Hangup processing - e-mail voicemail and delete it
function sm() {
  sendMail("support@softivr.com", "Voicemail", "You have a voicemail from " + call.callerID, "vm" + call.UID + ".wav");
  deleteFile("vm" + call.UID + ".wav");
}

// Main voicemail service
answer();
say("Please leave your message after the tone.");
playTone(400, 0.5);
onHangup('sm();');
recordFile("vm" + call.UID + ".wav");

Now let's do something clever. We're going to use Amazon's Mechanical Turk service to transcribe the voicemail, and send the text to the recipient as an SMS message. You'll need some credit in your account to use these services, but the good news is that we've already done all of the hard work of integrating with them for you.

SoftIVR's transcribe command uses Mechanical Turk (a system which allows humans to be paid piecerate for small jobs which are better carried out by humans than by computer) to turn a recorded file in to text. It may take a few minutes for someone to pick the job up, so it's not immediate - in fact, there's no guarantee that it will get done at all. We make sure that we offer enough money for each job to make it attractive for someone to do, and we have a target of < 5 mins for a transcription task.

Adding this to our code produces our final (for now) voicemail service:

// Hangup processing - e-mail voicemail and delete the file; transcribe it and send as SMS
function sm() {
  sendMail("support@softivr.com", "Voicemail", "You have a voicemail from " + call.DDI, "vm" + call.UID + ".wav");
  if (transcribe("voicemail", "vm" + call.UID + ".wav", "msg")) {
    sendSMS("+447773000000", "From " + msg.name + " " + msg.sender + " " + msg.transcription);
  }
  deleteFile("vm" + call.UID + ".wav");
}

// Main voicemail service
answer();
say("Please leave your message after the tone.");
playTone(400, 0.5);
onHangup('sm();');
recordFile("vm" + call.UID + ".wav");

- that's the essentials of SpinVOX in a dozen lines of code.

Personal tools