question: private SpeechletResponse handleNextEventRequest ( Session session ) { String cardTitle = "More events on this day in history" ; ArrayList < String > events = ( ArrayList < String > ) session . getAttribute ( SESSION_TEXT ) ; int index = ( Integer ) session . getAttribute ( SESSION_INDEX ) ; String speechOutput = "" ; String cardOutput = "" ; if ( events == null ) { speechOutput = "With History Buff, you can get historical events for any day of the year." + " For example, you could say today, or August thirtieth." + " Now, which day do you want?" ; } else if ( index >= events . size ( ) ) { speechOutput = "There are no more events for this date. Try another date by saying, " + " get events for august thirtieth." ; } else { StringBuilder speechOutputBuilder = new StringBuilder ( ) ; StringBuilder cardOutputBuilder = new StringBuilder ( ) ; for ( int i = 0 ; i < PAGINATION_SIZE && index < events . size ( ) ; i ++ ) { speechOutputBuilder . append ( "<p>" ) ; speechOutputBuilder . append ( events . get ( index ) ) ; speechOutputBuilder . append ( "</p> " ) ; cardOutputBuilder . append ( events . get ( index ) ) ; cardOutputBuilder . append ( " " ) ; index ++ ; } if ( index < events . size ( ) ) { speechOutputBuilder . append ( " Wanna go deeper in history?" ) ; cardOutputBuilder . append ( " Wanna go deeper in history?" ) ; } session . setAttribute ( SESSION_INDEX , index ) ; speechOutput = speechOutputBuilder . toString ( ) ; cardOutput = cardOutputBuilder . toString ( ) ; } String repromptText = "Do you want to know more about what happened on this date?" ; SimpleCard card = new SimpleCard ( ) ; card . setTitle ( cardTitle ) ; card . setContent ( cardOutput . toString ( ) ) ; SpeechletResponse response = newAskResponse ( "<speak>" + speechOutput + "</speak>" , true , repromptText , false ) ; response . setCard ( card ) ; return response ; }
target:  Prepares the speech to reply to the user. Obtains the list of events as well as the current index from the session attributes. After getting the next set of events, increment the index and store it back in session attributes. This allows us to obtain new events without making repeated network calls, by storing values (events, index) during the interaction with the user.