Skip to content

Commit 24baf98

Browse files
committed
feat: add code snippets for serverless cookie state examples
1 parent e89a6c3 commit 24baf98

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"title": "Add counter state to an SMS response that persists for four hours",
3+
"type": "server"
4+
}
5+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
exports.handler = (context, event, callback) => {
2+
// Initialize a new Response and some TwiML
3+
const response = new Twilio.Response();
4+
const twiml = new Twilio.twiml.MessagingResponse();
5+
6+
// Cookies are accessed by name from the event.request.cookies object
7+
// If the user doesn't have a count yet, initialize it to zero. Cookies are
8+
// always strings, so you'll need to convert the count to a number
9+
const count = Number(event.request.cookies.count) || 0;
10+
11+
twiml.message(`Your current count is ${count}`);
12+
13+
response
14+
// Add the stringified TwiML to the response body
15+
.setBody(twiml.toString())
16+
// Since we're returning TwiML, the content type must be XML
17+
.appendHeader('Content-Type', 'text/xml')
18+
// You can increment the count state for the next message, or any other
19+
// operation that makes sense for your application's needs. Use the
20+
// third argument of setCookie to set cookie attributes, such as making
21+
// count last for the max of 4 hours instead of the default 1 hour
22+
.setCookie('count', (count + 1).toString(), [
23+
'HttpOnly',
24+
'Secure',
25+
'SameSite=Strict',
26+
'Max-Age=14400',
27+
]);
28+
29+
return callback(null, response);
30+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"title": "Add counter state to an SMS response",
3+
"type": "server"
4+
}
5+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
exports.handler = (context, event, callback) => {
2+
// Initialize a new Response and some TwiML
3+
const response = new Twilio.Response();
4+
const twiml = new Twilio.twiml.MessagingResponse();
5+
6+
// Cookies are accessed by name from the event.request.cookies object
7+
// If the user doesn't have a count yet, initialize it to zero. Cookies are
8+
// always strings, so you'll need to convert the count to a number
9+
const count = Number(event.request.cookies.count) || 0;
10+
11+
twiml.message(`Your current count is ${count}`);
12+
13+
response
14+
// Add the stringified TwiML to the response body
15+
.setBody(twiml.toString())
16+
// Since we're returning TwiML, the content type must be XML
17+
.appendHeader('Content-Type', 'text/xml')
18+
// You can increment the count state for the next message, or any other
19+
// operation that makes sense for your application's needs. Remember
20+
// that cookies are always stored as strings
21+
.setCookie('count', (count + 1).toString());
22+
23+
return callback(null, response);
24+
};

0 commit comments

Comments
 (0)