Twilio Client for Android Quickstart Tutorial

Connecting to a TwiML Application

Wondering what actually happens when you connect using your app, and why you keep hearing that same welcome message? The key to this mystery is in the python server we deployed earlier. Let's take a look at that code:

server.py

APP_SID = 'YOUR APP SID HERE'

# some code ommitted

@app.route('/token')
def token():
  account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
  auth_token = os.environ.get("AUTH_TOKEN", AUTH_TOKEN)
  app_sid = os.environ.get("APP_SID", APP_SID)

   capability = TwilioCapability(account_sid, auth_token)

  # This allows outgoing connections to TwiML application
  if request.values.get('allowOutgoing') != 'false':
    capability.allow_client_outgoing(app_sid)

  # This allows incoming connections to client (if specified)
  client = request.values.get('client')
  if client != None:
    capability.allow_client_incoming(client)

  # This returns a token to use with Twilio based on the account and capabilities defined above
  return capability.generate()

This portion of the code is responsible for creating a capability token that tells your Twilio Device what actions it is allowed to perform.

The two lines below tell your device to make outgoing connections to the App Sid APa4983020a0b29fb5db1b1dc68e322fbe.

APP_SID = 'APa4983020a0b29fb5db1b1dc68e322fbe'

# ...

capability.allow_client_outgoing(app_sid)

Applications are just references to TwiML URLs that are fetched when a connection is established from a device, a browser, or a phone. If you are not familiar with TwiML, you may want to check out the Twilio Markup Language Quickstart.

The string starting with 'AP' is an Application SID. You entered this Application Sid when you deployed the server in the steps above. That Application's Voice URL points to your server's / endpoint, which returns the TwiML to say "Welcome to Twilio".

<?xml version="1.0" encoding="UTF-8"?>
  <Response>
    <Say>Welcome to Twilio</Say>
  </Response>

We change the user's audio experience by generating different TwiML. Let's continue with an example that lets us dial any phone number.

Next: Passing Parameters to your Application »