Book Image

Twilio Best Practices

Book Image

Twilio Best Practices

Overview of this book

Table of Contents (15 chapters)
Twilio Best Practices
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Digging deeper – Twilio's requests


In the preceding example, you've seen that Twilio includes some helpful data in its request when it hits your server to fetch the TwiML it needs in order to handle an incoming call or message.

We pulled out the country where the caller is located, which is stored in the FromCountry parameter. We grabbed this in PHP using $_GET, but you can do the same in any web language.

Alongside the caller's location, Twilio includes a whole lot of useful information.

Here are the highlights:

Parameter

What it means

CallSid/MessageSid

This refers to the unique reference for this call or message, as appropriate.

This can be particularly useful to store in a database if you might want to look up this call using the REST API later.

From

This refers to the caller's phone number in the international format (for example, +, then the country code, and then the local number).

To

This tells you which of your Twilio phone numbers is being dialed in the international format, as described previously.

CallStatus

This tells you about the current status of the call, which may be queued, ringing, in-progress, completed, busy, failed or no-answer.

ApiVersion

This refers to the Twilio API version being used for this call; you can safely ignore this parameter.

Direction

This refers to the direction—or in some sense, the type—of call in progress. This will be inbound for incoming calls, outbound-api for calls initiated with the REST API (we'll learn more about this in Chapter 2, Exploring REST API) and outbound-dial for calls initiated from the TwiML <Dial> verb.

ForwardedFrom

For some forwarded calls, this will include the number from which the call was forwarded, but this is not supported by all carriers.

CallerName

Twilio allows you to enable caller ID lookup on your phone numbers for $0.01 per lookup. If you've enabled this, this will contain the caller's name if a result was found.

From/To

City

This refers to the city where the caller/number being called is located (this will not necessarily be provided).

 

State

This refers to the state, province, or country where the caller/number being called is located (this will not necessarily be provided).

 

Zip

This refers to the zip or postal code of the caller/number being called (this will not necessarily be provided).

 

Country

This refers to the country where the caller/number being called is located.

Body

This refers to the text received in the SMS (SMS only).

This data, which Twilio provides, can help you implement a wide range of dynamic features into your TwiML, such as the following:

  • Changing how the call is handled depending on the number being called

  • Switching languages based on the location of the caller

  • Responding to what someone actually said in an SMS

Note

Further details are available in Twilio's comprehensive documentation at

https://www.twilio.com/docs/api/twiml/twilio_request