Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Create an SMS Conversation in C#


How do you turn a handful of isolated messages to and from the same party into a true conversation? You need some way to remember state between each message that is exchanged. This is because SMS is a stateless protocol. Building traditional web applications has this same hurdle, as HTTP is also a stateless protocol. This problem has been solved for web applications through the use of HTTP cookies and, rather than reinvent the wheel, the Twilio Messaging API uses the same solution.

This guide will show you how Programmable Messaging(link takes you to an external page) makes this easy for you and your ASP.NET application. The code snippets in this guide are written using modern C# language features and require the .NET Framework version 4.5 or higher. They also make use of the Twilio C# SDK(link takes you to an external page).

If you haven't written your own SMS webhooks with C# and ASP.NET before, you may want to first check out our guide, Receive and Reply to SMS and MMS Messages in C#. Ready to go? Let's get started!

(information)

Info

Twilio Conversations, a more recent product offering, is an omni-channel messaging platform that allows you to build engaging conversational, two-way messaging experiences. Be sure to check out our Conversations product to see if it's a better fit for your needs.


Using HTTP Cookies with Webhooks

using-http-cookies-with-webhooks page anchor

In web apps, you write a cookie to keep "statefulness" between separate requests from the same browser. Similarly, SMS messages are independent communications between two parties, so Twilio allows you to tie them together as a logical session via cookies. This means you can use server-side sessions to keep track of application state between requests. How cool is that? Twilio will expire the cookies for that conversation after four hours of inactivity, as if the user "closed the browser."

Storing Conversation Data

storing-conversation-data page anchor

The cookies let you share state across multiple messages allowing you to treat separate messages as a conversation, and store data about the conversation in the cookies for future reference.

You can store the data directly in a cookie, or you can use a session state management framework. The code sample below does the latter, using the Session State(link takes you to an external page) feature available in ASP.NET.


Track SMS Conversations using a Session

track-sms-conversations-using-a-session page anchor

Let's try using session counters to see if a particular user has messaged us before. If they're a new sender, we'll thank them for the new message. If they've sent us messages before, we'll specify how many messages we've gotten from them.

Tracking SMS Conversations using Cookies

tracking-sms-conversations-using-cookies page anchor
C#

_50
// In Package Manager, run:
_50
// Install-Package Twilio.AspNet.Mvc -DependencyVersion HighestMinor
_50
_50
using System.Collections.Generic;
_50
using System.Web.Mvc;
_50
using Twilio.AspNet.Mvc;
_50
using Twilio.TwiML;
_50
_50
public class SmsController : TwilioController
_50
{
_50
[HttpPost]
_50
public ActionResult Index()
_50
{
_50
var counter = 0;
_50
_50
// get the session varible if it exists
_50
if (Session["counter"] != null)
_50
{
_50
counter = (int) Session["counter"];
_50
}
_50
_50
// increment it
_50
counter++;
_50
_50
// save it
_50
Session["counter"] = counter;
_50
_50
// make an associative array of senders we know, indexed by phone number
_50
var people = new Dictionary<string, string>()
_50
{
_50
{"+14158675308", "Rey"},
_50
{"+12349013030", "Finn"},
_50
{"+12348134522", "Chewy"}
_50
};
_50
_50
// if the sender is known, then greet them by name
_50
var name = "Friend";
_50
var from = Request.Form["From"];
_50
var to = Request.Form["To"];
_50
if (people.ContainsKey(from))
_50
{
_50
name = people[from];
_50
}
_50
_50
var response = new MessagingResponse();
_50
response.Message($"{name} has messaged {to} {counter} times");
_50
_50
return TwiML(response);
_50
}
_50
}


Rate this page: