- Node
- Ruby
- Python
- PHP
- .NET
- Java
Overview
This guide shows how to receive and reply to SMS text messages on a Plivo phone number using Plivo APIs and Node.js.Prerequisites
To get started, you need a Plivo account — sign up if you don’t have one. You’ll also need a Plivo phone number that supports SMS. If you’re new to Plivo APIs, follow our instructions to set up a Node.js development environment and expose your web server to the internet.Create an Express server to reply to messages
Create a file namedreply_sms.js and paste this code into it. This server listens for incoming messages at the /replysms/ endpoint and replies with a thank you message using Plivo’s XML.Copy
Ask AI
const plivo = require('plivo');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use((req, response, next) => {
response.contentType('application/xml');
next();
});
app.set('port', (process.env.PORT || 3000));
app.all('/replysms/', (request, response) => {
const from_number = request.body.From || request.query.From;
const to_number = request.body.To || request.query.To;
const text = request.body.Text || request.query.Text;
console.log(`Message received - From: ${from_number}, To: ${to_number}, Text: ${text}`);
const r = plivo.Response();
const params = {
'src': to_number,
'dst': from_number,
};
const message_body = "Thank you, we received your request";
r.addMessage(message_body, params);
response.end(r.toXML());
});
app.listen(app.get('port'), () => {
console.log('Node app is running on port', app.get('port'));
});
Create and configure a Plivo application
- Create an Application: Go to Messaging > Applications in the Plivo console and click Add New Application.
- Configure the URL: Give the application a name (e.g.,
Reply Incoming SMS). In theMessage URLfield, enter your server URL (e.g.,https://<yourdomain>.com/replysms/) and set the method toPOST. Click Create Application. - Assign a Number: Navigate to the Numbers page and select the phone number you want to use. In the “Application Type” dropdown, select
XML Application, and in the “Plivo Application” dropdown, select the app you just created. Click Update Number.
Test it out
Send a text message to your Plivo number. You should receive an automated reply saying, “Thank you, we received your request.”Overview
This guide shows how to receive and reply to SMS text messages on a Plivo phone number using Plivo APIs and Ruby on Rails.Prerequisites
To get started, you need a Plivo account — sign up if you don’t have one. You’ll also need a Plivo phone number that supports SMS. If you’re new to Plivo APIs, follow our instructions to set up a Ruby development environment and expose your web server to the internet.Create a Rails controller to reply to messages
-
Generate Controller: In your project directory, run this command to create a controller and an
smsaction.You can delete the generated view file atCopyAsk AI$ rails generate controller Plivo smsapp/views/plivo/sms.html.erb. -
Add Code: Edit the new file at
app/controllers/plivo_controller.rband paste in this code.CopyAsk AIinclude Plivo include Plivo::Exceptions include Plivo::XML class PlivoController < ApplicationController skip_before_action :verify_authenticity_token def replysms from_number = params[:From] to_number = params[:To] text = params[:Text] response = Response.new params = { src: to_number, dst: from_number, } message_body = "Thank you, we received your request" response.addMessage(message_body, params) xml = PlivoXML.new(response) puts xml.to_xml render xml: xml.to_xml end end -
Add Route: Edit
config/routes.rbto route POST requests for/plivo/replysms/to your new controller action.CopyAsk AIRails.application.routes.draw do post 'plivo/replysms/' => 'plivo#replysms' end -
Run Server: Start the Rails server.
CopyAsk AI
$ rails serverFor ngrok testing, add this line toconfig/environments/development.rb:
config.hosts << /[a-z0-9-]+\.ngrok\.io/
Create and configure a Plivo application
- Create an Application: Go to Messaging > Applications in the Plivo console and click Add New Application.
- Configure the URL: Give the application a name (e.g.,
Reply Incoming SMS). In theMessage URLfield, enter your server URL (e.g.,https://<yourdomain>.com/plivo/replysms/) and set the method toPOST. Click Create Application. - Assign a Number: Navigate to the Numbers page and select the phone number you want to use. In the “Application Type” dropdown, select
XML Application, and in the “Plivo Application” dropdown, select the app you just created. Click Update Number.
Test it out
Send a text message to your Plivo number. You should receive an automated reply saying, “Thank you, we received your request.”Overview
This guide shows how to receive and reply to SMS text messages on a Plivo phone number using Plivo APIs with Python and Flask.Prerequisites
To get started, you need a Plivo account — sign up if you don’t have one. You’ll also need a Plivo phone number that supports SMS. If you’re new to Plivo APIs, follow our instructions to set up a Python development environment and expose your web server to the internet.Create a Flask application to reply to messages
Create a file namedreply_sms.py and paste this code into it. This Flask app listens for incoming messages at /replysms/ and uses Plivo’s XML library to generate a reply.Copy
Ask AI
from flask import Flask, request, Response
from plivo import plivoxml
app = Flask(__name__)
@app.route('/replysms/', methods=['GET', 'POST'])
def reply_sms():
from_number = request.values.get('From')
to_number = request.values.get('To')
response = plivoxml.ResponseElement()
response.add(
plivoxml.MessageElement(
"Thank you, we received your request",
src=to_number,
dst=from_number
)
)
return Response(response.to_string(), mimetype='application/xml')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
Create and configure a Plivo application
- Create an Application: Go to Messaging > Applications in the Plivo console and click Add New Application.
- Configure the URL: Give the application a name (e.g.,
Reply Incoming SMS). In theMessage URLfield, enter your server URL (e.g.,https://<yourdomain>.com/replysms/) and set the method toPOST. Click Create Application. - Assign a Number: Navigate to the Numbers page and select the phone number you want to use. In the “Application Type” dropdown, select
XML Application, and in the “Plivo Application” dropdown, select the app you just created. Click Update Number.
Test it out
Send a text message to your Plivo number. You should receive an automated reply saying, “Thank you, we received your request.”Overview
This guide shows how to receive and reply to SMS text messages on a Plivo phone number using Plivo APIs with PHP and Laravel.Prerequisites
To get started, you need a Plivo account — sign up if you don’t have one. You’ll also need a Plivo phone number that supports SMS. If you’re new to Plivo APIs, follow our instructions to set up a PHP development environment and expose your web server to the internet.Create a Laravel controller to reply to messages
-
Generate Controller: In your project directory, run this command:
CopyAsk AI
php artisan make:controller SMSController -
Add Code: Edit the new file at
app/Http/Controllers/SMSController.phpand paste in this code. It defines thereplysmsmethod that handles incoming messages.CopyAsk AI<?php namespace App\Http\Controllers; require_once 'vendor/autoload.php'; use Plivo\XML\Response; class SMSController extends Controller { public function replysms() { $from_number = $_REQUEST["From"]; $to_number = $_REQUEST["To"]; $response = new Response(); $params = array( 'src' => $to_number, 'dst' => $from_number, ); $message_body = "Thank you, we received your request"; $response->addMessage($message_body, $params); header('Content-Type: text/xml'); echo($response->toXML()); } } -
Add a route for the
replysmsfunction in your Laravel application.
Create and configure a Plivo application
- Create an Application: Go to Messaging > Applications in the Plivo console and click Add New Application.
- Configure the URL: Give the application a name (e.g.,
Reply Incoming SMS). In theMessage URLfield, enter your server URL (e.g.,https://<yourdomain>.com/replysms/) and set the method toPOST. Click Create Application. - Assign a Number: Navigate to the Numbers page and select the phone number you want to use. In the “Application Type” dropdown, select
XML Application, and in the “Plivo Application” dropdown, select the app you just created. Click Update Number.
Test it out
Send a text message to your Plivo number. You should receive an automated reply saying, “Thank you, we received your request.”Overview
This guide shows how to receive and reply to SMS text messages on a Plivo phone number using Plivo APIs with .NET MVC.Prerequisites
To get started, you need a Plivo account — sign up if you don’t have one. You’ll also need a Plivo phone number that supports SMS. If you’re new to Plivo APIs, follow our instructions to set up a .NET development environment and expose your web server to the internet.Create an MVC controller to reply to messages
- Create Project: In Visual Studio, create a new ASP.NET Web Application (.NET Framework) and select the MVC template.
-
Add Controller: In the
Controllersdirectory, create a new controller namedReplysmsController.cs. -
Add Code: Paste this code into your new controller. It reads the
FromandTonumbers from the incoming request and builds a Plivo XML response to send a message back.CopyAsk AIusing System; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; namespace Replysms.Controllers { public class ReplysmsController : Controller { [HttpPost] public IActionResult Index() { String from_number = Request.Form["From"]; String to_number = Request.Form["To"]; Plivo.XML.Response resp = new Plivo.XML.Response(); resp.AddMessage("Thank you, we received your request", new Dictionary<string, string>() { {"src", to_number}, {"dst", from_number} }); return this.Content(resp.ToString(), "text/xml"); } } }
Create and configure a Plivo application
- Create an Application: Go to Messaging > Applications in the Plivo console and click Add New Application.
- Configure the URL: Give the application a name (e.g.,
Reply Incoming SMS). In theMessage URLfield, enter your server URL (e.g.,https://<yourdomain>.com/replysms/) and set the method toPOST. Click Create Application. - Assign a Number: Navigate to the Numbers page and select the phone number you want to use. In the “Application Type” dropdown, select
XML Application, and in the “Plivo Application” dropdown, select the app you just created. Click Update Number.
Test it out
Send a text message to your Plivo number. You should receive an automated reply saying, “Thank you, we received your request.”Overview
This guide shows how to receive and reply to SMS text messages on a Plivo phone number using Plivo APIs with Java and Spring.Prerequisites
To get started, you need a Plivo account — sign up if you don’t have one. You’ll also need a Plivo phone number that supports SMS. If you’re new to Plivo APIs, follow our instructions to set up a Java development environment and expose your web server to the internet.Create a Spring application to reply to messages
Use Spring Initializr to create a boilerplate project. In your main application file, paste this code to handle incoming requests at the/replysms/ endpoint.Copy
Ask AI
package com.example.Plivo.SMS;
import com.plivo.api.exceptions.PlivoXmlException;
import com.plivo.api.xml.Message;
import com.plivo.api.xml.Response;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class PlivoSmsApplication {
public static void main(String[] args) {
SpringApplication.run(PlivoSmsApplication.class, args);
}
@GetMapping(value = "/replysms/", produces = {"application/xml"})
public String replySms(String From, String To, String Text) throws PlivoXmlException {
Response res = new Response().children(
new Message("Thank you, we received your request", To, From)
);
return res.toXmlString();
}
}
Create and configure a Plivo application
- Create an Application: Go to Messaging > Applications in the Plivo console and click Add New Application.
- Configure the URL: Give the application a name (e.g.,
Reply Incoming SMS). In theMessage URLfield, enter your server URL (e.g.,https://<yourdomain>.com/replysms/) and set the method toPOST. Click Create Application. - Assign a Number: Navigate to the Numbers page and select the phone number you want to use. In the “Application Type” dropdown, select
XML Application, and in the “Plivo Application” dropdown, select the app you just created. Click Update Number.