Skip to content

Commit 23e34b1

Browse files
authored
Merge pull request TwilioDevEd#963 from TwilioDevEd/serverless-cookies-state
feat: add code snippets for serverless cookie state examples
2 parents e89a6c3 + 25bbd44 commit 23e34b1

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
// Return a dynamic message based on if this is the first message or not
12+
const message =
13+
count > 0
14+
? `Your current count is ${count}`
15+
: 'Hello, thanks for the new message!';
16+
17+
twiml.message(message);
18+
19+
response
20+
// Add the stringified TwiML to the response body
21+
.setBody(twiml.toString())
22+
// Since we're returning TwiML, the content type must be XML
23+
.appendHeader('Content-Type', 'text/xml')
24+
// You can increment the count state for the next message, or any other
25+
// operation that makes sense for your application's needs. Use the
26+
// third argument of setCookie to set cookie attributes, such as making
27+
// count last for the max of 4 hours instead of the default 1 hour
28+
.setCookie('count', (count + 1).toString(), [
29+
'HttpOnly',
30+
'Secure',
31+
'SameSite=Strict',
32+
'Max-Age=14400',
33+
]);
34+
35+
return callback(null, response);
36+
};
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: 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+
// Return a dynamic message based on if this is the first message or not
12+
const message =
13+
count > 0
14+
? `Your current count is ${count}`
15+
: 'Hello, thanks for the new message!';
16+
17+
twiml.message(message);
18+
19+
response
20+
// Add the stringified TwiML to the response body
21+
.setBody(twiml.toString())
22+
// Since we're returning TwiML, the content type must be XML
23+
.appendHeader('Content-Type', 'text/xml')
24+
// You can increment the count state for the next message, or any other
25+
// operation that makes sense for your application's needs. Remember
26+
// that cookies are always stored as strings
27+
.setCookie('count', (count + 1).toString());
28+
29+
return callback(null, response);
30+
};

0 commit comments

Comments
 (0)