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
- Python
- Node
@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
const express = require("express")
const { Messaging } = require('@signalwire/realtime-api')
var app = express();
app.use(express.urlencoded());
app.listen(process.env.PORT || 3000, () => {
console.log("Server running on port 3000");
});
let project_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX";
let access_token = "PTxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
const client = new Messaging.Client({
project: project_id,
token: access_token,
})
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: '+###########'
})
})
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