function handleMessageWithStateManagement(senderID, recipientID, message) {
  let messageId = message.mid
  let messageText = message.text
  let messageAttachments = message.attachments

  // Load user state from dynamodb
  SDB.loadUserRecord(senderID, recipientID).then((record) => {
    let state = null
    if (record && record.Item && record.Item.vars)
      state = record.Item.vars
    // Process the incoming message
    return RS.getReply(senderID, messageText, state)
  }).then((answer) => {
    // Send the processed message back to sender
    return sendTextMessage(senderID, answer)    
  }).then(() => {
    // Save user state
    let userState = RS.getUserState(senderID)
    return SDB.saveUserRecord(senderID, recipientID, userState)
  }).then((result) => {
    console.log('User state saved')
  }).catch((err) => {
    console.error(err.message)
    sendTextMessage(senderID, 'Internal error: ' + messageText)
  })
}
/**
 * Load the user state, process message, send back the reply and then save the user state finally.
 * @param {string} senderID Messenger sender ID
 * @param {*} recipientID Messenger recipient ID
 * @param {*} message Message object sent from Messenger
 */
Load the user state, process message, send back the reply and then save the user state finally.
@param {string} senderID Messenger sender ID
@param {*} recipientID Messenger recipient ID
@param {*} message Message object sent from Messenger
Load the user state process message send back the reply and then save the user state finally . @param { string } senderID Messenger sender ID @param { * } recipientID Messenger recipient ID @param { * } message Message object sent from Messenger
function handleMessageWithStateManagement(senderID, recipientID, message) {
  let messageId = message.mid
  let messageText = message.text
  let messageAttachments = message.attachments
  SDB.loadUserRecord(senderID, recipientID).then((record) => {
    let state = null
    if (record && record.Item && record.Item.vars)
      state = record.Item.vars
    return RS.getReply(senderID, messageText, state)
  }).then((answer) => {
    return sendTextMessage(senderID, answer)    
  }).then(() => {
    let userState = RS.getUserState(senderID)
    return SDB.saveUserRecord(senderID, recipientID, userState)
  }).then((result) => {
    console.log('User state saved')
  }).catch((err) => {
    console.error(err.message)
    sendTextMessage(senderID, 'Internal error: ' + messageText)
  })
}
function handleMessageWithStateManagement ( senderID , recipientID , message ) { let messageId = message . mid let messageText = message . text let messageAttachments = message . attachments SDB . loadUserRecord ( senderID , recipientID ) . then ( ( record ) => { let state = null if ( record && record . Item && record . Item . vars ) state = record . Item . vars return RS . getReply ( senderID , messageText , state ) } ) . then ( ( answer ) => { return sendTextMessage ( senderID , answer ) } ) . then ( ( ) => { let userState = RS . getUserState ( senderID ) return SDB . saveUserRecord ( senderID , recipientID , userState ) } ) . then ( ( result ) => { console . log ( 'User state saved' ) } ) . catch ( ( err ) => { console . error ( err . message ) sendTextMessage ( senderID , 'Internal error: ' + messageText ) } ) }
Load the user state, process message, send back the reply and then save the user state finally.
Load the user state process message send back the reply and then save the user state finally .
// Load user state from dynamodb // Process the incoming message // Send the processed message back to sender // Save user state