When an SMS is sent to an Plivo phone number, you can receive the text on your server by setting a Message URL in your Plivo app. Plivo will send the message along with other parameters to your Message URL. You can reply back using the Plivo Message XML.
Note: In order to reply to a US or Canadian phone number, the `'src'` (source) phone number must be a Plivo US or Canadian phone number that is SMS-enabled. You can purchase a Plivo number from the Buy Numbers tab on your Plivo Console and filter the Phone Numbers by selecting SMS in the features option to buy SMS enabled numbers.
Check out our server SDKs page and install the right helper based on the programming language you want to use.
A phone number is required to receive and reply to SMS text messages. You can buy a Plivo phone number in over 19 countries through the Buy Numbers tab on your Plivo account UI. Check the SMS API coverage page for all the supported countries.
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/reply_to_sms/ this route will be invoked.
Note: For PHP, the route will be example.com/reply_to_sms.php.
Copy the relevant code below into a text file and save it. Let’s call it, 'reply_to_sms'.
Customize the 'body' with your reply.
Next, you will now have to configure this URL in your Plivo application.
fromflaskimportFlask,request,make_response,Responseimportplivofromplivoimportplivoxmlapp=Flask(__name__)@app.route('/reply_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))# send the details to generate an XML
response=plivoxml.ResponseElement()response.add(plivoxml.MessageElement('Thank you, we have received your request',# The text to be sent
src=to_number,# Sender's phone number
dst=from_number,# 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::XMLget"/reply_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}"# send the details to generate an XMLresponse=Response.newparams={src: to_number,# Sender's phone numberdst: from_number,# Receiver's phone Numbertype: "sms",callbackUrl: "https://www.foo.com/sms_status",callbackMethod: "POST",}message_body="Thank you, we have received your request"response.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('/reply_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;// Print the messageconsole.log('Message received - From: '+from_number+', To: '+to_number+', Text: '+text);//send the details to generate an XMLvarr=plivo.Response();varparams={'src':to_number,//Sender's phone number'dst':from_number,//Receiver's phone Number'type':"sms",'callbackUrl':"https://www.foo.com/sms_status",'callbackMethod':"POST"};varmessage_body="Thank you, we have received your request";//The text to be sentr.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");// send the details to generate an XML$response=newResponse();$params=array('src'=>$to_number,//Sender's phone number'dst'=>$from_number,//Receiver's phone Number'type'=>"sms",'callbackUrl'=>"https://www.foo.com/sms_status/",'callbackMethod'=>"POST");$message_body="Thank you, we have received your request";//The text to be sent$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;publicclassReply{publicstaticvoidmain(String[]args){get("/reply_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");response.type("application/xml");// Print the messageSystem.out.println(from_number+" "+to_number+" "+text);// send the details to generate an XMLResponseres=newResponse().children(// from_number is passed as destination,to_number is passed as source and text// is being passednewMessage(to_number,from_number,"Thank you, we have received your request").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")fromnumber:=r.FormValue("From")tonumber:=r.FormValue("To")text:=r.FormValue("Text")print("Message Received - ",fromnumber," ",tonumber," ",text)response:=xml.ResponseElement{Contents:[]interface{}{new(xml.MessageElement).SetCallbackMethod("POST").SetCallbackUrl("http://foo.com/sms status/").// Number from which the SMS needs to be sent.SetDst(tonumber).// Sender's phone numberSetSrc(fromnumber).// The receiver's number.SetType("sms").SetContents("Thanks, we have received your request"),},}print(response.String())returnresponse.String()})m.Run()}
usingNancy;usingSystem;usingSystem.Collections.Generic;usingSystem.Reflection;usingPlivo.XML;namespaceNancyStandalone{publicclassFunctionsModule:NancyModule{publicFunctionsModule(){Post("/reply_sms",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);Plivo.XML.Responseresp=newPlivo.XML.Response();// Generate the Message XMLPlivo.XML.Responseresp=newPlivo.XML.Response();resp.AddMessage("Thank you, we have received your request",newDictionary<string,string>(){{"src",to_number},{"dst",from_number},{"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!