@@ -3,34 +3,32 @@ exports.handler = (context, event, callback) => {
3
3
const response = new Twilio . Response ( ) ;
4
4
const twiml = new Twilio . twiml . MessagingResponse ( ) ;
5
5
6
+ // Since we're returning TwiML, the content type must be XML
7
+ response . appendHeader ( 'Content-Type' , 'text/xml' ) ;
8
+
6
9
// Cookies are accessed by name from the event.request.cookies object
7
10
// If the user doesn't have a count yet, initialize it to zero. Cookies are
8
11
// always strings, so you'll need to convert the count to a number
9
12
const count = Number ( event . request . cookies . count ) || 0 ;
10
13
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
+
11
23
// Return a dynamic message based on if this is the first message or not
12
24
const message =
13
25
count > 0
14
26
? `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. ' ;
16
28
17
29
twiml . message ( message ) ;
18
30
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 ( ) ) ;
34
32
35
33
return callback ( null , response ) ;
36
34
} ;
0 commit comments