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:
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.
fromflaskimportFlask,request,make_response,Responseimportplivofromplivoimportplivoxmlapp=Flask(__name__)@app.route('/forward_sms/',methods=['GET','POST'])definbound_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
returnResponse(response.to_string(),mimetype='application/xml')if__name__=='__main__':app.run(host='0.0.0.0',debug=True)
require"plivo"require"sinatra"includePlivoincludePlivo::XMLpost"/forward_sms/"do# Sender's phone numberfrom_number=params[:From]# Receiver's phone number - Plivo numberto_number=params[:To]# The text which was receivedtext=params[:Text]# Print the messageputs"Message received - From: #{from_number}, To: #{to_number}, Text: #{text}"body="Forwarded message : #{text}"to_forward="+14152223333"# send the details to generate an XMLresponse=Response.newparams={src: to_number,# Sender's phone numberdst: to_forward,# Receiver's phone Numbertype: "sms",callbackUrl: "https://www.foo.com/sms_status",callbackMethod: "POST",}message_body=bodyresponse.addMessage(message_body,params)xml=PlivoXML.new(response)putsxml.to_xml()# Prints the XMLcontent_type"application/xml"returnxml.to_s()# Returns the XMLend
varplivo=require('plivo');varexpress=require('express');varbodyParser=require('body-parser');varapp=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 numbervarfrom_number=request.body.From||request.query.From;// Receiver's phone number - Plivo numbervarto_number=request.body.To||request.query.To;// The text which was receivedvartext=request.body.Text||request.query.Text;// Prints the messageconsole.log('Message received - From: '+from_number+', To: '+to_number+', Text: '+text);//Print the messagevarto_forward='+14152223333';//The phone number to which the SMS has to be forwarded//send the details to generate an XMLvarr=plivo.Response();varparams={'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"};varmessage_body=text;//The text which was receivedr.addMessage(message_body,params);console.log(r.toXML());//Prints the XMLresponse.end(r.toXML());//Returns the XML});app.listen(app.get('port'),function(){console.log('Node app is running on port',app.get('port'));});
<?phprequire'vendor/autoload.php';usePlivo\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 messageecho("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=newResponse();$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?>
importstaticspark.Spark.*;importcom.plivo.api.xml.Message;importcom.plivo.api.xml.Response;publicclassHelloWorld{publicstaticvoidmain(String[]args){get("/forward_sms",(request,response)->{// Sender's phone numberStringfrom_number=request.queryParams("From");// Receiver's phone number - Plivo numberStringto_number=request.queryParams("To");// The text which was receivedStringtext=request.queryParams("Text");// Returns the response in application/xml typeresponse.type("application/xml");// Print the messageSystem.out.println(from_number+" "+to_number+" "+text);// The phone number to which the SMS has to be forwardedStringto_forward="+14152223333";// send the details to generate an XMLResponseres=newResponse().children(// from_number,to_forward,text is being passednewMessage(from_number,to_forward,text).callbackMethod("POST").callbackUrl("http://foo.com/sms status/").type("sms"));// Returns the XMLreturnres.toXmlString();});}}
packagemainimport("net/http""github.com/go-martini/martini""github.com/plivo/plivo-go/xml")funcmain(){m:=martini.Classic()m.Get("/",func(whttp.ResponseWriter,r*http.Request)string{w.Header().Set("Content-Type","application/xml")// Sender's phone numberfromnumber:=r.FormValue("From")// Receiver's phone number - Plivo numbertonumber:=r.FormValue("To")// The text which was receivedtext:=r.FormValue("Text")// Print the messageprint("Message Received - ",fromnumber," ",tonumber," ",text)//The phone number to which the SMS has to be forwardedtoforward:="+14152223333"//send the details to generate an XMLresponse:=xml.ResponseElement{Contents:[]interface{}{new(xml.MessageElement).SetCallbackMethod("POST").SetCallbackUrl("http://foo.com/sms_status/").SetDst(toforward).// Sender's phone numberSetSrc(tonumber).// Receiver's phone numberSetType("sms").SetContents(text),},}returnresponse.String()})m.Run()}
usingNancy;usingSystem;usingSystem.Collections.Generic;usingSystem.Reflection;usingPlivo.XML;namespaceNancyStandalone{publicclassFunctionsModule:NancyModule{publicFunctionsModule(){Post("/forward",parameters=>{// Sender's phone numberStringfrom_number=Request.Form["From"];// Receiver's phone numberStringto_number=Request.Form["To"];// The text which was receivedStringtext=Request.Form["Text"];// Print the messageConsole.WriteLine("Message received - From: {0}, To: {1}, Text: {2}",from_number,to_number,text);Stringto_forward="+14152223333";// The phone number to which the sms has to be forwardedPlivo.XML.Responseresp=newPlivo.XML.Response();// Generate the Message XMLPlivo.XML.Responseresp=newPlivo.XML.Response();resp.AddMessage(text,newDictionary<string,string>(){{"src",to_number},{"dst",to_forward},{"type","sms"},{"callbackUrl","http://foo.com/sms_status/"},{"callbackMethod","POST"}});varoutput=resp.ToString();varres=(Nancy.Response)output;res.ContentType="application/xml";returnres;};}}}
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!