Forward Incoming SMS
This tutorial will allow you to forward SMS text messages (sent to your Plivo number) to your another phone number. This is especially useful in use cases that involve message routing such as CRM softwares, sales teams, and help desk applications. The functionality behind forwarding text messages can be broken down into these two steps:
- Your friend sends you an SMS to your Plivo Number
- The SMS gets forwarded to another phone number
Getting Started
- Sign up for a free Plivo trial account.
- Check out our server SDKs page and install the right helper based on the programming language you want to use.
- Buy a Plivo phone number (optional).Note: A phone number is required only for sending SMS to US and Canadian phone numbers. However, country-specific carrier conditions may still apply. You can buy a US or Canadian phone number through the Buy Numbers" tab on your Plivo account UI.
- Use a web hosting service to host your web application. There are many inexpensive cloud hosting providers that you can use for just a few dollars a month. Follow the instructions of your hosting provider to host your web application.Note: If you are using a Plivo Trial account for this example, you can only send sms to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.
Set up a Web Server
Let’s assume your web server is located at http://example.com. Below is a snippet to set up a route on your webserver. Now when we send an HTTP request to http://example.com/forward_sms
this route will be invoked.
Note: For PHP, the route will be example.com/forward_sms.php.
- Copy the relevant code below into a text file and save it. Let’s call it,
'forward_sms'
. - Configure the
'to_forward'
parameter to the phone number you want to forward the SMS to. Be sure that all phone numbers include country code, area code, and phone number without spaces or dashes (e.g., 14153336666). - Next, you will now have to configure this URL in your Plivo application.Note: If you are using a trial account, your destination number needs to be verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.
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
from flask import Flask, request, make_response, Response
import plivo
from plivo import plivoxml
app = Flask(__name__)
@app.route('/forward_sms/', methods=['GET', 'POST'])
def inbound_sms():
# Sender's phone number
from_number = request.values.get('From')
# Receiver's phone number - Plivo number
to_number = request.values.get('To')
# The text which was received
text = request.values.get('Text')
# Print the message
print('Message received - From: %s, To: %s, Text: %s' %(from_number, to_number, text))
# The phone number to which the SMS has to be forwarded
to_forward = '+14152223333'
# send the details to generate an XML
response = plivoxml.ResponseElement()
response.add(
plivoxml.MessageElement(
text, # The text which was received
src=to_number, # Sender's phone number
dst=to_forward, # Receiver's phone Number
type='sms',
callback_url='http://foo.com/sms_status/',
callback_method='POST'))
print(response.to_string()) # Prints the XML
# Returns the XML
return Response(response.to_string(), mimetype='application/xml')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
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
require "plivo"
require "sinatra"
include Plivo
include Plivo::XML
post "/forward_sms/" do
# Sender's phone number
from_number = params[:From]
# Receiver's phone number - Plivo number
to_number = params[:To]
# The text which was received
text = params[:Text]
# Print the message
puts "Message received - From: #{from_number}, To: #{to_number}, Text: #{text}"
body = "Forwarded message : #{text}"
to_forward = "+14152223333"
# send the details to generate an XML
response = Response.new
params = {
src: to_number, # Sender's phone number
dst: to_forward, # Receiver's phone Number
type: "sms",
callbackUrl: "https://www.foo.com/sms_status",
callbackMethod: "POST",
}
message_body = body
response.addMessage(message_body, params)
xml = PlivoXML.new(response)
puts xml.to_xml() # Prints the XML
content_type "application/xml"
return xml.to_s() # Returns the XML
end
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
var plivo = require('plivo');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(function (req, response, next) {
response.contentType('application/xml');
next();
});
app.set('port', (process.env.PORT || 5000));
app.all('/forward_sms/', function (request, response) {
// Sender's phone number
var from_number = request.body.From || request.query.From;
// Receiver's phone number - Plivo number
var to_number = request.body.To || request.query.To;
// The text which was received
var text = request.body.Text || request.query.Text;
// Prints the message
console.log('Message received - From: ' + from_number + ', To: ' + to_number + ', Text: ' + text); //Print the message
var to_forward = '+14152223333'; //The phone number to which the SMS has to be forwarded
//send the details to generate an XML
var r = plivo.Response();
var params = {
'src': to_number, //Sender's phone number
'dst': to_forward, //Receiver's phone Number
'type': "sms",
'callbackUrl': "https://www.foo.com/sms_status",
'callbackMethod': "POST"
};
var message_body = text; //The text which was received
r.addMessage(message_body, params);
console.log(r.toXML()); //Prints the XML
response.end(r.toXML()); //Returns the XML
});
app.listen(app.get('port'), function () {
console.log('Node app is running on port', app.get('port'));
});
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
<?php
require 'vendor/autoload.php';
use Plivo\XML\Response;
// Sender's phone numer
$from_number = $_REQUEST["From"];
// Receiver's phone number - Plivo number
$to_number = $_REQUEST["To"];
// The SMS text message which was received
$text = $_REQUEST["Text"];
// Prints the message
echo("Message received - From $from_number, To: $to_number, Text: $text");
// The phone number to which the SMS has to be forwarded
$to_forward = '+14152223333';
// send the details to generate an XML
$response = new Response();
$params = array(
'src' => $to_number, //Sender's phone number
'dst' => $to_forward, //Receiver's phone Number
'type' => "sms",
'callbackUrl' => "https://www.foo.com/sms_status/",
'callbackMethod' => "POST"
);
$message_body = $text; //The text which was received
$response->addMessage($message_body, $params);
Header('Content-type: text/xml');
echo($response->toXML()); // Returns the XML
?>
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
import static spark.Spark.*;
import com.plivo.api.xml.Message;
import com.plivo.api.xml.Response;
public class HelloWorld {
public static void main(String[] args) {
get("/forward_sms", (request, response) -> {
// Sender's phone number
String from_number = request.queryParams("From");
// Receiver's phone number - Plivo number
String to_number = request.queryParams("To");
// The text which was received
String text = request.queryParams("Text");
// Returns the response in application/xml type
response.type("application/xml");
// Print the message
System.out.println(from_number + " " + to_number + " " + text);
// The phone number to which the SMS has to be forwarded
String to_forward = "+14152223333";
// send the details to generate an XML
Response res = new Response().children(
// from_number,to_forward,text is being passed
new Message(from_number, to_forward, text).callbackMethod("POST")
.callbackUrl("http://foo.com/sms status/").type("sms"));
// Returns the XML
return res.toXmlString();
});
}
}
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
package main
import (
"net/http"
"github.com/go-martini/martini"
"github.com/plivo/plivo-go/xml"
)
func main() {
m := martini.Classic()
m.Get("/", func(w http.ResponseWriter, r *http.Request) string {
w.Header().Set("Content-Type", "application/xml")
// Sender's phone number
fromnumber := r.FormValue("From")
// Receiver's phone number - Plivo number
tonumber := r.FormValue("To")
// The text which was received
text := r.FormValue("Text")
// Print the message
print("Message Received - ", fromnumber, " ", tonumber, " ", text)
//The phone number to which the SMS has to be forwarded
toforward := "+14152223333"
//send the details to generate an XML
response := xml.ResponseElement{
Contents: []interface{}{
new(xml.MessageElement).
SetCallbackMethod("POST").
SetCallbackUrl("http://foo.com/sms_status/").
SetDst(toforward). // Sender's phone number
SetSrc(tonumber). // Receiver's phone number
SetType("sms").
SetContents(text),
},
}
return response.String()
})
m.Run()
}
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
using Nancy;
using System;
using System.Collections.Generic;
using System.Reflection;
using Plivo.XML;
namespace NancyStandalone
{
public class FunctionsModule : NancyModule
{
public FunctionsModule()
{
Post("/forward", parameters =>
{
// Sender's phone number
String from_number = Request.Form["From"];
// Receiver's phone number
String to_number = Request.Form["To"];
// The text which was received
String text = Request.Form["Text"];
// Print the message
Console.WriteLine("Message received - From: {0}, To: {1}, Text: {2}", from_number, to_number, text);
String to_forward = "+14152223333"; // The phone number to which the sms has to be forwarded
Plivo.XML.Response resp = new Plivo.XML.Response();
// Generate the Message XML
Plivo.XML.Response resp = new Plivo.XML.Response();
resp.AddMessage(text,
new Dictionary<string, string>()
{
{"src", to_number},
{"dst", to_forward} ,
{"type", "sms"},
{"callbackUrl", "http://foo.com/sms_status/"},
{"callbackMethod", "POST"}
});
var output = resp.ToString();
var res = (Nancy.Response)output;
res.ContentType = "application/xml";
return res;
};
}
}
}
Rate this page
🥳 Thank you! It means a lot to us!
×
Help Us Improve
Thank you so much for rating the page, we would like to get your input for further improvements!
Subscribe to Updates
Thank you for your feedback!