Wit.ai清除或重置上下文

时间:2017-01-16 21:12:18

标签: wit.ai wit-ai

如何在故事结束时或用户想要重启过程时重置或清除上下文?我已经拥有了自己的重置功能......效果不是很好!你能解释一下我要做什么吗?非常感谢你

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

对于会议,我这样做:

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;
};

如何在故事结束时终止会话和/或终止上下文并重置流程呢

1 个答案:

答案 0 :(得分:0)

您可能会在webhook点击Wit.ai控制器之前删除旧会话并创建一个新会话。

请参阅下面的代码示例:

Webhook POST处理程序

// 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类

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 = {};
}