Skip to contentSkip to navigationSkip to topbar
On this page

Create an SMS Conversation in Node.js


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 Node.js application. The code snippets in this guide are written using modern JavaScript language features in Node.js version 6 or higher, and make use of the following modules:

If you haven't written your own SMS webhooks with Node.js before, you may want to first check out our guide, Receive and Reply to SMS and MMS Messages in Node.js. 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 express-session module for Node.js and Express.


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
const http = require('http');
2
const express = require('express');
3
const session = require('express-session');
4
const MessagingResponse = require('twilio').twiml.MessagingResponse;
5
6
const app = express();
7
8
app.use(session({secret: 'anything-you-want-but-keep-secret'}));
9
10
app.post('/sms', (req, res) => {
11
const smsCount = req.session.counter || 0;
12
13
let message = 'Hello, thanks for the new message.';
14
15
if(smsCount > 0) {
16
message = 'Hello, thanks for message number ' + (smsCount + 1);
17
}
18
19
req.session.counter = smsCount + 1;
20
21
const twiml = new MessagingResponse();
22
twiml.message(message);
23
24
res.writeHead(200, {'Content-Type': 'text/xml'});
25
res.end(twiml.toString());
26
});
27
28
http.createServer(app).listen(1337, () => {
29
console.log('Express server listening on port 1337');
30
});

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.