SMS and Voice Services via Twilio

You can use Twilio to embed voice, VoIP, and messaging into applications. The Twilio platform consists of the Twilio Markup Language (TwiML), a RESTful API, and VoIP SDKs for web browsers, Android, and iOS. Helper libraries are available in multiple languages.

TwiML tells Twilio what to do when you receive an incoming call or SMS. When someone makes a call or sends an SMS to one of your Twilio numbers, Twilio looks up the URL associated with that phone number, makes a request, and then reads TwiML instructions at that URL to determine what to do, such as record a call, or send an SMS.

You can use the Twilio REST API to query metadata about your account, phone numbers, calls, text messages, and recordings. You can also initiate outbound calls and send text messages.


Find more details at Twilio Pricing.

Before you begin

    Sélectionnez ou créez un projet Google Cloud Platform.

    Accéder à la page "Gérer les ressources"

Setting Up

You will use the standard Google App Engine Python Runtime Environment to construct this example. If this is your first time writing Python for Google App Engine, follow the Python Hello World tutorial.

After you have a working "Hello, World!" application, you will need to add Twilio's Python library to your application. This process involves installing some package management tools and the latest version of the Twilio Python module, then creating a symbolic link to the library and its dependencies into your Google App Engine application:

  1. Navigate to the directory where your application is stored, this will be the helloworld directory you created if you followed the tutorial.

    cd helloworld
  2. Install setuptools.

  3. Install pip using setuptools:

    sudo easy_install pip
  4. Install Twilio's Python library and dependencies:

    pip install twilio
  5. Locate your site-packages directory. This directory is where pip installed the Twilio helper library:

    python -c "import site; print(site.getsitepackages()[0])"
  6. Create a symbolic link to the Twilio Python module in your application directory. Replace {SITE_PACKAGES} with the value from the previous command.

    ln -s {SITE_PACKAGES}/twilio .
  7. Create a symbolic link to the dependencies of the Twilio Python module in your application directory:

    ln -s {SITE_PACKAGES}/httplib2 .
    ln -s {SITE_PACKAGES}/ .

You have now installed the Twilio library into your Google App Engine project.

Receiving an Incoming Call

To create your first application with Twilio:

Let's walk through creating your first application, titled "Hello Monkey."

After completing the instructions in Setting Up, modify to look like this.

After updating, follow these directions to deploy your project to App Engine.

After you've deployed your project to App Engine you will be able to send an HTTP POST request to http://<your app>, which will return the following text:

<?xml version="1.0" encoding="UTF-8"?>
  <Say>Hello Monkey!</Say>

Next, copy and paste the http://<your app> URL into the "Voice" URL box on the Numbers page of your Twilio Account.

Now call your Twilio number! You should hear a voice say "Hello Monkey!" in response. When you call, Twilio will fetch your URL, and execute the XML instructions listed above. Then, Twilio will hang up, because there are no more instructions.

Sending an SMS

The twilio-python helper library makes it easy to send an outgoing SMS using Twilio.

To send an SMS message using Twilio:

  1. Modify the file and provide your "Account SID" and "Auth Token" (example) You can get these values from your Twilio account.

  2. Change the from_ and to parameters to use real phone numbers. The from_ number must be a valid Twilio phone number in your account. For this example, use the phone number you called in the example above. The to number can be any outgoing number, such as your cell phone number.

  3. Deploy your updated code and trigger an SMS message by loading the follow request path for your app:

    http://<your app>

What's next

Now that you've learned some of the basics, learn more about additional features and some best practices for building secure and scalable applications:

Cette page vous a-t-elle été utile ? Évaluez-la :

Envoyer des commentaires concernant…

App Engine standard environment for Python 2