<

Setting up a dev server in 5 minutes

In this guide, you will learn how to set up a server in under 5 minutes and start serving valid Plivo XML to control your call flow. Using a tunnelling software like ngrok, we will see how you can expose your local dev server with a public address to test out your integration with Plivo. Choose the language of your choice to get started.

Python

Prerequisites

Setup a virtual env (optional)

Go to the directory where you want to write the code for your server. We will first set up a virtual environment so that the packages installed won’t interfere with the system ones.

To create a virtual environment with the name env_name use the following command.

virtualenv env_name

This creates a directory with the name env_name along with the required binaries inside it. The next step is to activate the virtual environment so you can start using it.

source env_name/bin/activate

You should now see that your CLI is in the env_name environment. The next time you do a pip install some_package it would installed in this environment and will not over-write the system versions.

Once you are done with your development and want to return to the normal environment, you can deactivate this environment using

deactivate

Set up a Flask server

Install the required packages, and use the following code snippet to start a local server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from flask import Flask, Response, request

import plivoxml

app = Flask(__name__)


@app.route('/dial_a_number', methods=['POST'])
def dial_a_number():
    to_number = request.form.get('To')

    # Building Plivo XML with the SDK
    xml_response = plivoxml.Response()
    xml_response.addDial().addNumber(to_number)

    return Response(xml_response, mimetype='text/xml')


if __name__ == '__main__':
    app.run()

This code returns the following XML

1
2
3
4
5
<Response>
    <Dial>
        <Number>{to_number}</Number>
    </Dial>
</Response>

Note that it is easier to use Plivo’s SDK to generate complex XML to change the call flow. But if you want to use plain XML directly, you can directly return it as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from flask import Flask, Response, request

app = Flask(__name__)


@app.route('/dial_a_number', methods=['POST'])
def dial_a_number():
    to_number = request.form.get('To')
    return Response(
        '<Response><Dial><Number>{to_number}</Number></Dial></Response>'.format(
            to_number=to_number),
        mimetype='text/xml')


if __name__ == '__main__':
    app.run()

Ruby

Prerequisites

Set up a Flask server

Install the required gems, and use the following code snippet to start a local server.

1
2
3
4
5
6
7
8
9
10
require 'sinatra'
require 'plivo'

include Plivo

post '/dial_a_number' do
  response = Response.new()
  response.addDial().addNumber(params['To'])
  response.to_xml()
end

This code returns the following XML

1
2
3
4
5
<Response>
    <Dial>
        <Number>{to_number}</Number>
    </Dial>
</Response>

Note that it is easier to use Plivo’s SDK to generate complex XML to change the call flow. But if you want to use plain XML directly, you can directly return it as follows:

1
2
3
4
5
require 'sinatra'

post '/dial_a_number' do
  "<Response><Dial><Number>#{params['To']}</Number></Dial></Response>"
end

PHP

Prerequisites

Set up a PHP server

Use the following code snippet to start a local server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
require './vendor/autoload.php';
use Plivo\Response;

function sendResponseWithPlivo()
{
    $number = $_POST["To"];
    $response = new Response();
    $dial = $response->addDial();
    $dial->addNumber($number);
    return $response->toXML();
}
 header("Content-type: text/xml; charset=utf-8");
 echo sendResponseWithPlivo();

Note that it is easier to use Plivo’s SDK to generate complex XML to change the call flow. But if you want to use plain XML directly, you can directly return it as follows:

1
2
3
4
5
6
7
8
9
10
11
<?php
function sendResponse()
{
    $number = $_POST["To"];
    $response = '<?xml version="1.0" encoding="utf-8"?>';
    $response .= '<Response><Dial><Number>'.$number.'</Number></Dial></Response>';
    return $response;
}

 header("Content-type: text/xml; charset=utf-8");
 echo sendResponse();

Save this code in any file (let’s say the file name is plivo-server.php). To run this file on the server, go to the folder where this file resides and use the following command:

php -S localhost:8000

You can choose to run it on any other IP or port that your machine has access to.

Node.js

Prerequisites

Set up an Express server

Install the required modules, and use the following code snippet to start a local server.

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
const express = require('express');
const plivo = require('plivo');
const app = express()
const bodyParser = require('body-parser');

// for parsing the body
app.use(bodyParser.urlencoded({
  extended: false
}));
app.use(bodyParser.json())

// respond with the xml
app.post('/dial_a_number', handleRequest);

// start the server at port 3000
app.listen(3000, function() {
  console.log('Server Running at 127.0.0.1:3000')
})

// generate the xml and send it as response
function handleRequest(request, response) {
  let toNumber = request.body.To;
  let xml_response = plivo.Response();
  xml_response.addDial().addNumber(toNumber);
  response.set('Content-Type', 'text/xml');
  response.send(xml_response.toXML());
}

This code returns the following XML

1
2
3
4
5
<Response>
    <Dial>
        <Number>{to_number}</Number>
    </Dial>
</Response>

Note that it is easier to use Plivo’s SDK to generate complex XML to change the call flow. But if you want to use plain XML directly.

Go

Prerequisites

Set up a Go server

Use the following code snippet to start a local server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import (
	"fmt"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	w.Header().Add("Content-Type", "text/xml")
	fmt.Fprintf(w, "<Response><Dial><Number>%s</Number></Dial></Response>", r.FormValue("To"))
}

func main() {
	http.HandleFunc("/dial_a_number", handler)
	http.ListenAndServe(":8080", nil)
}

Exposing your local server to the internet

You can use a tunnelling software like ngrok to expose your local server to the internet.

./ngrok http <port_on_which_your_local_server_is_running>

This will give you a UI with links that look like ngrok.io/* which you can use to access your local server using the public network.