Skip to contentSkip to navigationSkip to topbar
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 to use Programmable Messaging(link takes you to an external page) to accomplish this in 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 CookiesLink to code sample: Tracking SMS Conversations using Cookies
1
// In Package Manager, run:
2
// Install-Package Twilio.AspNet.Mvc -DependencyVersion HighestMinor
3
4
using System.Collections.Generic;
5
using System.Web.Mvc;
6
using Twilio.AspNet.Mvc;
7
using Twilio.TwiML;
8
9
public class SmsController : TwilioController
10
{
11
[HttpPost]
12
public ActionResult Index()
13
{
14
var counter = 0;
15
16
// get the session varible if it exists
17
if (Session["counter"] != null)
18
{
19
counter = (int) Session["counter"];
20
}
21
22
// increment it
23
counter++;
24
25
// save it
26
Session["counter"] = counter;
27
28
// make an associative array of senders we know, indexed by phone number
29
var people = new Dictionary<string, string>()
30
{
31
{"+14158675308", "Rey"},
32
{"+12349013030", "Finn"},
33
{"+12348134522", "Chewy"}
34
};
35
36
// if the sender is known, then greet them by name
37
var name = "Friend";
38
var from = Request.Form["From"];
39
var to = Request.Form["To"];
40
if (people.ContainsKey(from))
41
{
42
name = people[from];
43
}
44
45
var response = new MessagingResponse();
46
response.Message($"{name} has messaged {to} {counter} times");
47
48
return TwiML(response);
49
}
50
}

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.