Skip to main content

Sending a Recording URL via SMS

Overview

This code snippet uses recording status callbacks in order to send a transcription of the created recording via SMS. When a recording is created with the recording status 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 recording 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: Recording URL to SMS
from flask import Flask, request
from signalwire.rest import Client as signalwire_client

app = Flask(__name__)

# TODO: Update with your own credentials
signalwire_space = "YOURSPACENAME.signalwire.com"
project_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX"
auth_token = "PTxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

@app.route("/sendRecording", methods=["POST"])
def message():

call_sid = request.form.get('CallSid')
recording_url = request.form.get('RecordingUrl')

client = signalwire_client(project_id, auth_token, signalwire_space_url = signalwire_space)

m = client.messages.create(
body='You have received a voicemail. Listen to the recording here: "' + recording_url +
'". The Call SID is ' + call_sid,
from_='+1xxxxxxxxxx',
to='+1xxxxxxxxxx'
)
return recording_url

Python

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

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

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 space_url, project_id, and access_token.

We need to use request.form.get('ParameterName') in order to gather the CallSid and RecordingUrl 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.

from flask import Flask, request
from signalwire.rest import Client as signalwire_client

app = Flask(__name__)

# TODO: Update with your own
signalwire_space = "YOURSPACENAME.signalwire.com"
project_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX"
auth_token = "PTxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

@app.route("/sendRecording", methods=["POST"])
def message():
# accept incoming parameters and store them. Feel free to add any extra parameters that you would like to print to
# to console or add to your message. This example will show CallSID and recording URL.
call_sid = request.form.get('CallSid')
recording_url = request.form.get('RecordingUrl')

# create a client object connected to our account & project
client = signalwire_client(project_id, auth_token, signalwire_space_url = signalwire_space)

# create a text message and send ourselves the text
m = client.messages.create(
body='You have received a voicemail. Listen to the recording here: "' + recording_url +
'". The Call SID is ' + call_sid,
from_='+1xxxxxxxxxx',
to='+1xxxxxxxxxx'
)
return recording_url

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 recordingURLViaSMS.js, for example, you then need to run:
node recordingURLViaSMS.js in the terminal.

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(process.env.PORT || 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 /sendRecording endpoint

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

We gather the call_sid andrecording_url 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("/sendRecording", (req, res, next) => {
let call_sid = req.body.CallSid
let recording_url = req.body.RecordingUrl

client.send({
body: 'You have received a voicemail. Listen to the recording here: ' +
recording_url +
'. The Call SID is ' +
call_sid,
from: '+###########',
to: '+############'
})

return recording_url
});

Wrap up

It is very simple to send a particular recording's URL 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, create a SignalWire account and Space.

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