When an SMS message comes in to one of your Twilio numbers, Twilio makes an HTTP request to the SMS URL configured for that number. In your response to that request you can tell Twilio what to do in response to the SMS.
Twilio behaves just like a web browser, so there's nothing new to learn.
Twilio will keep cookie state across multiple SMS messages between the same two phone numbers. This allows you to treat the separate messages as a conversation, and store data about the conversation, such as a session identifier, in the cookies for future reference. Twilio will expire the cookies for that conversation after four hours of inactivity.
Twilio does the right thing when your application responds with different MIME types.
| MIME Type | Behavior |
|---|---|
| text/xml, application/xml, text/html | Twilio interprets the returned document as a TwiML XML Instruction Set. See the XML Verbs section for details. This is the most commonly used response. |
| text/plain | Twilio returns the content of the text file to the sender in the form of a SMS message. |
When your application responds to a Twilio request with XML, Twilio runs your document through the TwiML interpreter. To keep things simple, the TwiML interpreter only understands a few specially named XML elements. In TwiML parlance these are divided into three groups: the root <Response> element, "verbs" and "nouns". Each group is discussed below.
The interpreter starts at the top of your TwiML document and executes instructions ("verbs") in order from top to bottom. As an example, the following TwiML SMS snippet sends "Hello World" as an SMS reply to the sender before redirecting control to the TwiML at http://api.twilio.com/sms/welcome.
<?xml version="1.0" encoding="UTF-8" ?>
<Response>
<Sms>Hello World!</Sms>
<Redirect>http://api.twilio.com/sms/welcome</Redirect>
</Response>
You can provide multiple <Sms> verbs in one TwiML document to send multiple messages. For example:
<?xml version="1.0" encoding="UTF-8" ?>
<Response>
<Sms>This is message 1 of 2.</Sms>
<Sms>This is message 2 of 2.</Sms>
</Response>
TwiML elements ("verbs" and "nouns") have case-sensitive names. For example, using <sms> instead of <Sms> will result in an error. Attribute names are also case sensitive and "camelCased." And you can use XML comments freely; the interpreter ignores them.
<Response> ElementThe root element of Twilio's XML Markup is the <Response> element. In any TwiML response
to a Twilio request, all verb elements must be nested within this element. Any other structure is
considered invalid.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Sms>I'm hungry!</Sms>
</Response>
Most XML elements in a TwiML document are TwiML verbs. Verb names are case sensitive, as are their attribute names. There is only one core TwiML SMS verb and one secondary verb, with detailed documentation on each. The core verb is:
<Sms>: Send an SMS message in reply to the incoming SMS.Note that there are certain situations when the TwiML interpreter may not reach verbs in a TwiML document because control flow has passed to a different document. This usually happens when a verb's 'action' attribute is set. For example, if an <Sms> is followed by a <Redirect>, the <Redirect> is unreachable if the <Sms> verb's 'action' URL is set. In this case, SMS session flow continues with the TwiML received in your response to the 'action' URL request.
The following verbs may impact control flow: <Sms> and <Redirect>
A Noun in TwiML is anything nested inside a verb that is not itself a verb. It's whatever the verb is acting on. In TwiML SMS, all nouns are plain text.