1

How can I reset or clear the context at the end of my story or when the user wants to restart the process ? I already have a reset function of my own ... Not very effective ! Can you please explain me what I have to do ? Thank you very much

 reset({sessionId,context}) {
    return new Promise(function(resolve, reject) {
        context = null;
        return resolve(context);
    });
 }

And for the session, I do that :

var findOrCreateSession = (fbid) => {
    let sessionId;
    Object.keys(sessions).forEach(k => {
        if (sessions[k].fbid === fbid) {
            sessionId = k;
        }
    });
    if (!sessionId) {
        console.log("je cree une session car yen a pas");
        sessionId = new Date().toISOString();
        sessions[sessionId] = {
            fbid: fbid,
            context: {}
        };
    }
    return sessionId;
};

How can I kill the session at the end of the story and/or kill context and reset the process please !

Pablo DelaNoche
  • 677
  • 1
  • 9
  • 28
  • You may want to check the answers to this question too http://stackoverflow.com/questions/43353252/how-to-delete-context-session-id-at-end-of-conversation-in-wit-ai-bot – Franco May 11 '17 at 21:22

1 Answers1

0

You could potentially delete the old session and create a new one before the webhook hits the Wit.ai controller.

See the code below for an example:

Webhook POST handler

// Conversation History
let sessionId = WitRequest.getSession(senderId);

if (message.text.toLowerCase() == "break") {
    // New Conversation History
    // Delete the old session
    WitRequest.deleteSession(sessionId);

    // Get new Session Id
    sessionId = WitRequest.getSession(senderId);

}

let sessionData = WitRequest.getSessionData(sessionId);

wit.runActions(
    sessionId,
    message.text,
    sessionData
)
.then((context) => {
    sessionData = context;
})
.catch((error) => {
    console.log("Error: " + error);
});

WitRequest Class

static getSession(senderId) {

    let sessionId = null;

    for (let currentSessionId in sessions) {
        if (sessions.hasOwnProperty(currentSessionId)) {
            if (sessions[currentSessionId].senderId == senderId) {
                sessionId = currentSessionId
            }
        }
    }

    if (!sessionId) {
        sessionId = new Date().toISOString();
        sessions[sessionId] = {
            senderId: senderId,
            context: {}
        }
    }

    return sessionId;
}

static getSessionData(sessionId) {

    return sessions[sessionId].context;

}

static deleteSession(sessionId) {

    delete sessions[sessionId];

}

static clearSession(session) {
    session.context = {};
}
James Marino
  • 668
  • 1
  • 9
  • 25