Voicemail Transcription Using Go

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 Go development environment and a web server and safely expose that server to the internet.

Create a Go server to implement voicemail transcription

Create a file called voicemail.go 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
package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/plivo/plivo-go"
	"github.com/plivo/plivo-go/v7/xml"
)

func main() {
	r := gin.Default()
	r.POST("/voicemail", func(c *gin.Context) {
		c.Header("Content-Type", "application/xml")
		response := xml.ResponseElement{
			Contents: []interface{}{
				new(xml.SpeakElement).
					AddSpeak("Please leave a message. Press the star key when you're done"),
				new(xml.RecordElement).
					SetTranscriptionType("auto").
					SetTranscriptionUrl("https://" + c.Request.Host + "/transcription-url").
					SetAction("https://" + c.Request.Host + "/action-url").
					SetFinishOnKey("*").
					SetMaxLength(20),

				new(xml.SpeakElement).
					AddSpeak("Recording not received"),
			},
		}
		c.String(200, response.String())
	})
	r.POST("/transcription-url", func(c *gin.Context) {
		c.Header("Content-Type", "application/xml")
		c.MultipartForm()
		for key, value := range c.Request.PostForm {
			log.Printf("%v = %v \n", key, value)
		}
		client, err := plivo.NewClient("<auth_id>", "<auth_token>", &plivo.ClientOptions{})
		if err != nil {
			fmt.Print("Error", err.Error())
			return
		}
		response, err := client.Messages.Create(
			plivo.MessageCreateParams{
				Src:  "<sender_id>",
				Dst:  "<destination_number>",
				Text: strings.Join(c.Request.PostForm["transcription"], "You have a new transcription"),
			},
		)
		if err != nil {
			fmt.Print("Error", err.Error())
			return
		}
		fmt.Printf("Response: %#v\n", response)
	})
	r.POST("/action-url", func(c *gin.Context) {
		c.Header("Content-Type", "application/xml")
		c.MultipartForm()
		for key, value := range c.Request.PostForm {
			log.Printf("%v = %v \n", key, value)
		}
	})
	r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

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 os.Setenv and os.Getenv functions to store environment variables and fetch them when initializing the client.

Save the file and run it.

$ go run voicemail.go

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 Go 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.