reactjsamazon-web-servicesamazon-lexaws-chatbot

Using AWS amplify interactions to end Lex sessions


I have currently set up a system combining react chatbot kit and Lex V2 using Amplify interactions. I have set a session timeout of 5 mins. But if i refresh the page, lex still resumes the converstaions from where i last left it. Is there a way I can end the Lex session everytime i refresh page or close the chatbot window?

The amplify interactions documentation has only 2 methods - send and oncomplete, neither of which suit my requirement. Is there any other method or system I can follow to achieve the goal?


Solution

  • Heads up

    I don't know if anyone else will have the same problem I did, but I did find a way to make things work with my current set up and I am posting an answer here so others may find it useful.

    Solution

    For my set up between react chatbot kit to Lex, I use a 'secretkey' to trigger an intent a.k.a I have the intent set to trigger only on one specific utterance and the chatbot automatically sends this to lex when it is opened, hence triggering the intent.

    Similarly, every time I toggle the chatbot window to close, I've connected it to an async function to send another 'secretkey' (which is just any other normal message you want) to lex. I've connected my lex to a lambda function where a simple if else clause will check if the input transcript is equal to the secret key, and if yes, then I will send a response of this format.

     const response = {
      sessionState: {
        sessionAttributes: {},
        dialogAction: {
        type: 'Close',
        fulfillmentState: 'Fulfilled'
      },
      intent: 
        {
          confirmationState: "Confirmed",
          name: event.sessionState.intent.name,
          state: "Fulfilled",
        }
      },
      messages: [{
        contentType: 'PlainText',
        content: 'The conversation has been restarted.',
      }],
      requestAttributes: sessionState.requestAttributes, 
      shouldEndSession: true,
      
    };
    return response;
    

    Otherwise, it will continue on with its usual functionality, sample code for the else clause:

    const defaultResponse = {
          sessionState: {
            dialogAction: event.proposedNextState.dialogAction,
            intent: 
            {
              confirmationState: "Confirmed",
              name: event.sessionState.intent.name,
              state: "InProgress",
            }
          }
          
        };
        return defaultResponse;
    

    Additional info

    Hope this helps!