Skip to main content

Documentation Index

Fetch the complete documentation index at: https://plivo.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

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 Python development environment.

Create the survey application

Create a file called survey.py and paste into it this code.
from flask import Flask, request, Response
import plivo
from plivo import plivoxml

app = Flask(__name__)


@app.post("/send_survey/")
def send_survey():
    client = plivo.RestClient("<auth_id>", "<auth_token>")
    response = client.messages.create(
        src="<sender_id>",
        dst="<destination_number>",
        text='Did you find out all the information you needed? Please reply "Yes" or "No"',
    )
    return Response(str(response), mimetype="application/json"), 200


@app.post("/survey_response/")
def survey_response():
    from_number = request.values.get("From")
    to_number = request.values.get("To")
    text = request.values.get("Text")
    print(f"Message received - From: {from_number}, To: {to_number}, Text: {text}")

    if text.lower() == "yes":
        message_body = "Thank you for your feedback"
    elif text.lower() == "no":
        message_body = "We apologize for the inconvenience. A representative will contact you to assist you"
    else:
        message_body = f'Response received was "{text}", which is invalid. Please reply with either "Yes" or "No"'
    
    response = plivoxml.ResponseElement()
    response.add(plivoxml.MessageElement(message_body, src=to_number, dst=from_number))
    return Response(response.to_string(), mimetype="application/xml"), 200


if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)
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 os module(os.environ) to store environment variables and fetch them when initializing the client.

Test

Save the file and run it.
$ python survey.py