Skip to main content

Overview

This guide shows how to conduct an SMS survey. Surveys can help businesses with market research. Using SMS for surveys lets organizations process input quickly and efficiently. You can conduct SMS surveys either by using our PHLO visual workflow builder or our APIs. Follow the instructions in one of the tabs below.
Here’s how to use Plivo APIs to create surveys using SMS text messages — sending questions and collecting answers.

How it works

Outbound-SMS Flow

Prerequisites

To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. To receive incoming messages, you must have a Plivo phone number that supports SMS; you can rent numbers from the Numbers page of the Plivo console or by using the Numbers API. If this is your first time using Plivo APIs, follow our instructions to set up a .NET development environment.

Create a .NET controller

Create a new project and navigate to the Controllers directory. Create a controller called Survey.cs and paste into it this code.
using System;
using Plivo;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;


namespace demo.Controllers
{
    public class Survey : Controller
    {

        public IActionResult Index()
        {
            var api = new PlivoApi("<auth_id>","<auth_token>");
            var response = api.Message.Create(
                src: "<sender_id>",
                dst: "<destination_number>",
                text: "Did you find out all the information you needed? Please reply 'Yes' or 'No'");
            return this.Content(response.ToString());
        }

        public IActionResult survey_response()
        {
            String from_number = Request.Form["From"];
            String to_number = Request.Form["To"];
            String text = Request.Form["Text"];
            String body;

            if (text.ToLower() == "yes")
            {
                body = "Thank you for your feedback";
            }
            else if(text.ToLower() == "no")
            {
                body = "We apologize for the inconvenience. A representative will contact you to assist you";
            }
            else
            {
                body = String.Format("Response received was {0}, which is invalid. Please reply with either 'Yes' or 'No'", text);
            }

            Plivo.XML.Response resp = new Plivo.XML.Response();
            resp.AddMessage(body, new Dictionary<string, string>()
            {
                {"src", to_number},
                {"dst", from_number},
                {"type", "sms"}
            });
            var output = resp.ToString();
            Console.WriteLine(output);
            return this.Content(output, "text/xml");
        }
    }
}
Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234). In countries other than the US and Canada you can use a sender ID for the message source. You must have a Plivo phone number to send messages to the US or Canada; you can buy a Plivo number from Phone Numbers > Buy Numbers on the Plivo console or via the Numbers API.
Note: We recommend that you store your credentials in the auth_id and auth_token environment variables to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and Plivo will automatically fetch the values from the environment variables. You can use the Environment.SetEnvironmentVariable method to store environment variables and Environment.GetEnvironmentVariable to fetch them when initializing the client.
Run the project and you should see your basic server application in action at http://localhost:5001/survey/.Set up ngrok to expose your local server to the internet.

Create a Plivo application for the SMS survey

Associate the controller you created with Plivo by creating a Plivo application. Visiting Messaging > Applications and click Add New Application. You can also use Plivo’s Application API.Give your application a name — we called ours SMS-Survey. Enter the server URL you want to use (for example https://<yourdomain>.com/survey/survey_response/) in the Message URL field and set the method to POST. Click Create Application to save your application.
Create Application

Assign a Plivo number to your application

Navigate to the Numbers page and select the phone number you want to use for this application.From the Application Type drop-down, select XML Application.From the Plivo Application drop-down, select SMS-Survey (the name we gave the application).Click Update Number to save.
Assign Phone Number to Plivo Number

Test

Send a text message to the Plivo number you specified using any phone.