Get Started

SMS Quickstart Tutorial

Sending SMS Messages During a Phone Call

We've responded to incoming SMS messages directly, but what about sending SMS during phone calls? Now we will send a message to anyone who calls our Twilio phone number. If you haven't yet, read over the TwiML Quickstart, as this example utilizes concepts introduced in that guide.

Next, set the Voice URL of your Twilio phone number to http://companyfoo.com/sms-twiml.cshtml where companyfoo.com is the name of your server and you have placed the sms-twiml.cshtml file on your server.

sms-twiml.cshtml
@{
    // make an associative array of senders we know, indexed by phone number
    var people = new Dictionary<string, string>() { 
        {"+14158675309","Curious George"},
        {"+14158675310","Boots"},
        {"+14158675311","Virgil"}
    };

    // if the sender is known, then greet them by name
    // otherwise, consider them just another monkey
    string name = "Monkey";
    if (!string.IsNullOrEmpty(Request["From"]))
    {
        name = people[Request["From"]];
    }
    
    // now greet the caller
    Response.ContentType = "text/xml";
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say>Hello @name.</Say>
    <Sms>@name, thanks for the call!</Sms>
</Response>

When someone calls your Twilio phone number, the caller will be greeted by name and then sent an SMS message thanking them for calling. Isn't that nice?

For more information, see the <Sms> verb documentation.

And that's all she wrote folks! Back to Quickstarts.