Skip to content

Commit 5907e4d

Browse files
committed
feat: pivot from cookie attributes to cookie removal in serverless state with cookies example
1 parent fe2947f commit 5907e4d

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

serverless/examples/cookies/state-long/state-long-function.js renamed to serverless/examples/cookies/clear-state/clear-state-function.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,32 @@ exports.handler = (context, event, callback) => {
33
const response = new Twilio.Response();
44
const twiml = new Twilio.twiml.MessagingResponse();
55

6+
// Since we're returning TwiML, the content type must be XML
7+
response.appendHeader('Content-Type', 'text/xml');
8+
69
// Cookies are accessed by name from the event.request.cookies object
710
// If the user doesn't have a count yet, initialize it to zero. Cookies are
811
// always strings, so you'll need to convert the count to a number
912
const count = Number(event.request.cookies.count) || 0;
1013

14+
if (count > 5) {
15+
twiml.message("You've reached the end of the count!");
16+
// In this case we want to remove the count and let the user begin
17+
// a new conversation
18+
response.setBody(twiml.toString()).removeCookie('count');
19+
// Use an early return to respond to the user and avoid other logic paths
20+
return callback(null, response);
21+
}
22+
1123
// Return a dynamic message based on if this is the first message or not
1224
const message =
1325
count > 0
1426
? `Your current count is ${count}`
15-
: 'Hello, thanks for the new message!';
27+
: 'Hello, thanks for the new message! Message again to see your count update.';
1628

1729
twiml.message(message);
1830

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-
]);
31+
response.setBody(twiml.toString()).setCookie('count', (count + 1).toString());
3432

3533
return callback(null, response);
3634
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"title": "Clear counter state from an SMS conversation",
3+
"type": "server"
4+
}
5+

serverless/examples/cookies/state-long/meta.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)