Voicemail Transcription Using Java

Overview

This guide shows how to transcribe voicemail and send the transcription via SMS.

Prerequisites

To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. You must have a voice- and SMS-enabled Plivo phone number to receive calls and send SMS messages; you can rent numbers from the Numbers page of the Plivo console, or by using the Numbers API. If this is your first time using Plivo APIs, follow our instructions to set up a Java development environment and a web server and safely expose that server to the internet.

Create a Spring application to implement voicemail transcription

Create a Java class called VoicemailApplication and paste into it this code.

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
package com.example.voicemail;

import com.plivo.api.Plivo;
import com.plivo.api.exceptions.PlivoRestException;
import com.plivo.api.exceptions.PlivoValidationException;
import com.plivo.api.exceptions.PlivoXmlException;
import com.plivo.api.models.message.Message;
import com.plivo.api.models.message.MessageCreateResponse;
import com.plivo.api.xml.Record;
import com.plivo.api.xml.Response;
import com.plivo.api.xml.Speak;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


@RestController
@SpringBootApplication
public class VoicemailApplication {

	public static void main(String[] args) {
		SpringApplication.run(VoicemailApplication.class, args);
	}
	
	@PostMapping(value = "/voicemail", produces = {"text/xml"})
	public String voiceMail(HttpServletRequest request) throws PlivoXmlException, PlivoValidationException {
	
		String hostName = request.getRequestURL().toString();
		Response response = new Response()
				.children(
						new Speak("Please leave a message. Press the star key when you're done"),
						new Record(hostName + "action-url")
								.transcriptionType("auto")
								.transcriptionUrl(hostName + "transcription-url")
								.finishOnKey("*")
								.maxLength(20));
		response.children(new Speak("Recording not received"));
		return response.toXmlString();
	}
	
	@PostMapping("voicemail/transcription-url")
	@ResponseStatus(code = HttpStatus.OK)
	public String TranscriptionBody(String call_uuid, String duration, String recording_id, String transcription, String transcription_charge, String transcription_rate) {
		System.out.println("callUuid:"+ call_uuid + "\n" + "duration:"+duration + "\n" + "recordingId:"+recording_id +"\n"+ "transcription:"+transcription + "\n" + "transcriptionCharge:"+transcription_charge + "\n" + "transcription_rate:"+transcription_rate + "\n");
		Plivo.init("<auth_id>","<auth_token>");
		try {
			MessageCreateResponse response = Message.creator("<sender_id>", "<destination_number>",
					"You have a new transcription: "+transcription)
					.create();
			System.out.println(response);
		}
		catch (PlivoRestException | IOException e)
			{
				e.printStackTrace();
			}
		return "OK";
	}
	
	@PostMapping("voicemail/action-url")
	@ResponseStatus(code = HttpStatus.OK)
	public String ActionBody(String BillRate,String CallStatus,String CallUUID,String CallerName,String Digits,String Direction,String Event,String From,String ParentAuthID,String RecordFile,String RecordUrl,String RecordingDuration,String RecordingDurationMs,String RecordingEndMs,String RecordingID,String RecordingStartMs,String SessionStart,String To) {
		System.out.println("billRate:"+ BillRate+"\n"+"callStatus:"+ CallStatus+"\n"+"callUUID:"+ CallUUID+"\n"+"callerName:"+ CallerName+"\n"+"digits:"+ Digits+"\n"+"direction:"+ Direction+"\n"+"event:"+ Event+"\n"+"From:"+ From+"\n"+"parentAuthID:"+"\n"+ParentAuthID+"\n"+"recordFile:"+ RecordFile+"\n"+"recordUrl:"+ RecordUrl+"\n"+"recordingDuration:"+ RecordingDuration+"\n"+"recordingDurationMs:"+ RecordingDurationMs+"\n"+"recordingEndMs:"+ RecordingEndMs+"\n"+"recordingID:"+ RecordingID+"\n"+"recordingStartMs:"+ RecordingStartMs+"\n"+"sessionStart:"+ SessionStart+"\n"+"To:"+ To);
		return "OK";
	}

}

Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234).

Note: We recommend that you store your credentials in the auth_id and auth_token environment variables, to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and Plivo will automatically fetch the values from the environment variables. You can use System.getenv() to store environment variables and retrieve them when initializing the client.

Save the project and run it. You should see your basic server application in action at http://localhost:8080/voicemail/.

Set up ngrok to expose your local server to the internet.

Create a Plivo application for voicemail transcription

Associate the Spring application you created with Plivo by creating a Plivo application. Visit Voice > Applications in the Plivo console and click on Add New Application, or use Plivo’s Application API.

Give your application a name — we called ours Voicemail-Transcription. Enter the server URL you want to use (for example https://<yourdomain>.com/voicemail/) in the Answer URL field and set the method to POST. Click Create Application to save your application.

Plivo Create Application Voicemail Transcription

Assign a Plivo number to your application

Navigate to the Numbers page and select the phone number you want to use for this application.

From the Application Type drop-down, select XML Application.

From the Plivo Application drop-down, select Voicemail-Transcription (the name we gave the application).

Click Update Number to save.

Assign Voicemail Application

Test

Make a call to your Plivo number and leave yourself a voicemail message. You should receive a text message with the transcription.

Note: If you’re using a Plivo Trial account, you can send SMS messages only to phone numbers that have been verified with Plivo. You can verify (sandbox) a number by going to the console’s Phone Numbers > Sandbox Numbers page.