.NET Quickstart: Replying to Text Messages By Name

Ok, we've said "Hello" to the mobile monkey, but what if the monkey wants to say, "Thanks" back? Let's lookup the sender, and thank them for sending us a message by name if we can:

Loading Code Samples...
Language
SDK Version:
  • 4.x
  • 5.x
Format:
  • TwiML
// In Package Manager, run:
// Install-Package Twilio.Mvc -DependencyVersion HighestMinor

using System.Web.Mvc;
using Twilio.Mvc;
using Twilio.TwiML;
using Twilio.TwiML.Mvc;

public class ReplyByName : TwilioController
{
    [HttpPost]
    public ActionResult Index(SmsRequest request)
    {
        // make an associative array of people we know, indexed by phone number
        var people = new Dictionary<string, string>() {
            {"+14158675309", "Curious George"},
            {"+14158675310", "Boots"},
            {"+14158675311", "Virgil"}
        };

        var name = people.ContainsKey(Request.Form["From"])
            ? people[Request.Form["From"]]
            : "Monkey";

        var response = new TwilioResponse();
        response.Message($"{name}, thanks for the message!");

        return TwiML(response);
    }
}
// In Package Manager, run:
// Install-Package Twilio.Mvc -DependencyVersion HighestMinor

using System.Web.Mvc;
using Twilio.AspNet.Mvc;
using Twilio.TwiML;

public class ReplyByName : TwilioController
{
    [HttpPost]
    public ActionResult Index(SmsRequest request)
    {
        // make an associative array of people we know, indexed by phone number
        var people = new Dictionary<string, string>() {
            {"+14158675309", "Curious George"},
            {"+14158675310", "Boots"},
            {"+14158675311", "Virgil"}
        };

        var name = people.ContainsKey(Request.Form["From"])
            ? people[Request.Form["From"]]
            : "Monkey";

        var message = new Message();
        message.Body($"{name}, thanks for the message!");

        return TwiML(message);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        Curious George, thanks for the message!
    </Message>
</Response>
Reply SMS by Name using Twilio

If you add your phone number and name to the people Dictionary you will get a personalized SMS thank-you from your app.


Next: Sending SMS and MMS Messages via the REST API »

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

1 / 1
Loading Code Samples...
SDK Version:
  • 4.x
  • 5.x
Format:
  • TwiML
// In Package Manager, run:
// Install-Package Twilio.Mvc -DependencyVersion HighestMinor

using System.Web.Mvc;
using Twilio.Mvc;
using Twilio.TwiML;
using Twilio.TwiML.Mvc;

public class ReplyByName : TwilioController
{
    [HttpPost]
    public ActionResult Index(SmsRequest request)
    {
        // make an associative array of people we know, indexed by phone number
        var people = new Dictionary<string, string>() {
            {"+14158675309", "Curious George"},
            {"+14158675310", "Boots"},
            {"+14158675311", "Virgil"}
        };

        var name = people.ContainsKey(Request.Form["From"])
            ? people[Request.Form["From"]]
            : "Monkey";

        var response = new TwilioResponse();
        response.Message($"{name}, thanks for the message!");

        return TwiML(response);
    }
}
// In Package Manager, run:
// Install-Package Twilio.Mvc -DependencyVersion HighestMinor

using System.Web.Mvc;
using Twilio.AspNet.Mvc;
using Twilio.TwiML;

public class ReplyByName : TwilioController
{
    [HttpPost]
    public ActionResult Index(SmsRequest request)
    {
        // make an associative array of people we know, indexed by phone number
        var people = new Dictionary<string, string>() {
            {"+14158675309", "Curious George"},
            {"+14158675310", "Boots"},
            {"+14158675311", "Virgil"}
        };

        var name = people.ContainsKey(Request.Form["From"])
            ? people[Request.Form["From"]]
            : "Monkey";

        var message = new Message();
        message.Body($"{name}, thanks for the message!");

        return TwiML(message);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        Curious George, thanks for the message!
    </Message>
</Response>