File tree 4 files changed +64
-0
lines changed
serverless/examples/cookies
4 files changed +64
-0
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ "title" : " Add counter state to an SMS response that persists for four hours" ,
3
+ "type" : " server"
4
+ }
5
+
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
1
+ {
2
+ "title" : " Add counter state to an SMS response" ,
3
+ "type" : " server"
4
+ }
5
+
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments