Write an SMS Autoresponder Using Python

    Overview

    This guide shows how to write an autoresponder for SMS text messages. Autoresponders can streamline marketing campaigns and subscription signups and reduce the amount of work humans have to do.

    You can create an autoresponder either by using our PHLO visual workflow builder or our APIs. Follow the instructions in one of the tabs below.

    You can create and deploy a PHLO to implement an SMS autoresponder with a few clicks on the PHLO canvas.

    How it works

    Prerequisite

    To get started, you need a Plivo account — sign up with your work email address if you don’t have one already.

    Create the PHLO

    To create a PHLO, visit the PHLO page of the Plivo console. If this is your first PHLO, the PHLO page will be empty.

    • Click Create New PHLO.

    • In the Choose your use case pop-up, click Build my own. The PHLO canvas will appear with the Start node.

      Note: The Start node is the starting point of any PHLO. It lets you trigger a PHLO to start upon one of three actions: incoming SMS message, incoming call, or API request.
    • If you plan to use a dynamic payload — passing values through parameters when you trigger the PHLO from your application — click on the Start node to open the Configuration pane. Under API Request, enter key names for the variables you want to use in your payload — for example, a list of numbers to send an SMS message to.

    • Once you’ve configured the node, click Validate to save the configuration.

    • From the list of components on the left-hand side, drag and drop the Send Message component onto the canvas. When a component is placed on the canvas it becomes a node.

    • Draw a line to connect the Start node’s Incoming Message trigger state to the Send Message node.

    • In the Configuration pane at the right of the canvas, configure the Send Message node with a sender ID in the From field. Enter the destination number you wish to send a message to in the To field. Put your message in the Text field.

      Note: You can define a static payload by specifying values when you create the PHLO, or define a dynamic payload by passing values through Liquid templating parameters when you trigger the PHLO from your application.
    • Once you’ve configured the node, click Validate to save the configuration.

    • After you complete the configuration, give the PHLO a name by clicking in the upper left, then click Save.

    Assign the PHLO to a Plivo number

    Once you’ve created and configured your PHLO, assign it to a Plivo number.

    • On the Numbers page of the console, under Your Numbers, click the phone number you want to use for the PHLO.
    • In the Number Configuration box, select PHLO from the Application Type drop-down.
    • From the PHLO Name drop-down, select the PHLO you want to use with the phone number, then click Update Number.

    Assign PHLO to a Plivo Number

    Test

    You can now send a message to your Plivo phone number and see how the autoresponder works.

    For more information about creating a PHLO application, see the PHLO Getting Started guide. For information on components and their variables, see the PHLO Components Library.

    Note: If you’re using a Plivo Trial account, you can send SMS messages only to phone numbers that have been verified with Plivo. You can verify (sandbox) a number by going to the console’s Phone Numbers > Sandbox Numbers page.

    To implement SMS Autoresponder use-case in the traditional API way, you can refer to the instructions in the below section to begin your implementation.

    Outline

    Outbound-SMS Flow

    Implementation

    In this section, we will guide you in setting up an app using Plivo’s API to implement SMS Autoresponder use-case. First, let’s make sure you meet these prerequisites before we dive into the code.

    Prerequisites

    • Plivo Auth Id and Auth Token: You will find your Plivo Auth Id and Auth Token on the home screen of your Plivo Console. Click here to sign-up for a Plivo account if you haven’t already!

      Find Your Auth Credentials on Plivo Console

    • Plivo Phone Number(Optional): To send messages to the United States and Canada, you must have a Plivo phone number that supports SMS. Numbers can be purchased from the Numbers section of your Plivo Console and use the same as the source number/from number for the outbound SMS. This number will also help you receive incoming SMS as you must have a SMS-enabled Plivo phone number to do the same. Please note that you can also purchase numbers using the Numbers API.

      Buy a New Plivo Number

    Set up Your Python Dev Environment

    You must set up and install Python and Plivo’s Python SDK to implement send SMS use-case. Here’s how.

    Install Python

    Operating SystemInstructions
    macOS & LinuxYou would already have Python installed, you can check this by running the command python --version in the terminal
    WindowsTo install Python on Windows you can follow the instructions from here.

    Install Plivo Python Package

    • Create a project directory, run the following command:

      $ mkdir mypythonapp
      
    • Change the directory to our project directory in the command line:

      $ cd mypythonapp
      
    • Install the SDK using pip

      pip install plivo
      
    • Alternatively, you can download the source code from this repo and run

      python setup.py install
      

    We recommend that you use virtualenv to manage and segregate your Python environments, instead of using sudo with your commands and overwriting dependencies.

    Create the Send SMS App

    Once you have the above prerequisites set, You can follow the below instructions to create an app to send an outbound SMS to deliver your message.

    Now, create a file called send_sms.py and paste the below code.

    1
    2
    3
    4
    5
    6
    7
    8
    
    import plivo
    
    client = plivo.RestClient('<auth_id>','<auth_token>')
    response = client.messages.create(
        src='+14151234567',
        dst='+14157654321',
        text='Hey, I’m interested in this property!',)
    print(response)
    
    Note:
    • Replace the placeholders auth_id & auth_token with your credentials from Plivo Console.
    • We recommend that you store your credentials in the auth_id & auth_token environment variables, so as to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and it will automatically fetch them from the environment variables
    • You can use os module(os.environ) to store environment variables and fetch them while initializing the client.
    • Replace the placeholder +14151234567 with the Phone number which you have purchased and +14157654321 with the phone number you will be sending SMS to.
    • Both plivo_source_number and +14157654321 should be in E.164 format
    • For other countries, this can be either a Alpha Sender ID or Numeric Sender ID based on the Sender ID support for the respective countries, to know more you can refer to the Sender ID Usage guide

    Test and Validate

    Once you have created the Send SMS App, save the file and use the below command to run your code.

    $ python send_sms.py
    
    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.