Skip to main content

Sending a Recording Transcription via SMS

Overview

This code snippet uses transcription status callbacks in order to send a transcription of the created recording via SMS. When a recording is created with transcription enabled and the transcription callback pointing to this script, a SignalWire client object will be created and used to send the message to the end destination cell phone.

You can learn more about transcription status callbacks, all of the possible parameters you can learn, and how to set them up in our status callback mega guide!

Full code example: Transcription to SMS
@app.route("/message", methods=["POST"])
def message():
call_sid = request.form.get('CallSid')
transcription_text = request.form.get('TranscriptionText')
from_number = request.form.get('From')

client = signalwire_client("ProjectID", "AuthToken", signalwire_space_url = 'YOURSPACE.signalwire.com')

m = client.messages.create(
body='You have received a voicemail from the number ' + from_number +
'. The voicemail transcription is as follows: "' + transcription_text +
'" and the Call SID is ' + call_sid,
from_='+1xxxxxxxxxx',
to='+1xxxxxxxxxx'
)
return transcription_text

Python

This script is short and simple. There is only one route in this application which we will call /message.

What do I need to run this code?

You will need the Flask framework and the SignalWire Python SDK downloaded.

How to Run Snippet?

To run the application, execute export FLASK_APP=your_file_name.py then run flask run.

Code Walkthrough

We need to use request.form.get('ParameterName') in order to gather the CallSid, TranscriptionText, and From number parameters and store them in their own variables. If you want to include more parameters either to print to console or include in the message, you can gather them using the same format here.

We then create a SignalWire client object with our project details and authentication. All that's left there is to create a message object and send all of the necessary information within the Body with the To number being the end destination number and the From number being a SignalWire number.

@app.route("/message", methods=["POST"])
def message():
# gather necessary paramters and store them in an accessible variable
call_sid = request.form.get('CallSid')
transcription_text = request.form.get('TranscriptionText')
from_number = request.form.get('From')

# create a client object connected to our account & project
client = signalwire_client("ProjectID", "AuthToken", signalwire_space_url = 'YOURSPACE.signalwire.com')

# create a text message and the text with necessary parameters
m = client.messages.create(
body='You have received a voicemail from the number ' + from_number +
'. The voicemail transcription is as follows: "' + transcription_text +
'" and the Call SID is ' + call_sid,
from_='+1xxxxxxxxxx',
to='+1xxxxxxxxxx'
)
return transcription_text

Node.js

What do I need to run this code?

We will need the following libraries (click their names to get instructions on how to install them):

How to Run Snippet?

If you save this code snippet in a file called recordingTranscriptionViaSMS.js, for example, you then need to run:
node recordingTranscriptionViaSMS.js.

Code Walkthrough

Load the necessary libraries

const express = require("express")
const { Messaging } = require('@signalwire/realtime-api')

Instantiate Express

In this section we create and launch Express, to then have it listen for requests on port 3000.

var app = express();
app.use(express.urlencoded());

app.listen(3000, () => {
console.log("Server running on port 3000");
});

Set your SignalWire Credentials

In order for us to connect to SignalWire later on in the code using the Rest Client we first need to make sure we update project_id and access_token.

let project_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX";
let access_token = "PTxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

Instantiate the SignalWire Client

Since we're just going to be sending messages, there is no need to specify the Relay context.

const client = new Messaging.Client({
project: project_id,
token: access_token,
})

Expose the /message endpoint

Here we expose the /message endpoint, that will solely respond to POST requests, and outline the actions to take when one comes in.

We gather the call_sid, transcription_text, and from_number from the request body. We then create a message with the body making use of the data gathered from the request, using FROM and TO numbers of our liking.

app.post("/message", (req, res, next) => {
console.log(req.body)
let call_sid = req.body.CallSid
let transcription_text = req.body.TranscriptionText
let from_number = req.body.From

client.send({
body: 'You have received a voicemail from the number ' + from_number +
'. The voicemail transcription is as follows: "' + transcription_text +
'" and the Call SID is ' + call_sid,
from: '+###########',
to: '+###########'
})
})

Wrap up

It is very simple to send a particular recording's transcription via SMS, and it doesn't take a lot of code to accomplish this functionality either!

Sign Up Here

If you would like to test this example out, you can create a SignalWire account and space here.

Please feel free to reach out to us on our Community Slack or create a Support ticket if you need guidance!