【问题标题】:Wit.ai Clear or Reset contextWit.ai 清除或重置上下文
【发布时间】:2017-01-16 21:12:18
【问题描述】:

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

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

我怎样才能在故事结束时终止会话和/或终止上下文并重置流程!

【问题讨论】:

标签: wit.ai wit-ai


【解决方案1】:

您可能会在 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 = {};
}

【讨论】:

    猜你喜欢
    • 2011-01-28
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 2023-03-17
    相关资源
    最近更新 更多