如何在故事结束时或者用户希望重新启动进程时重置或清除上下文?我已经有了我自己的重置功能..。不是很有效!你能解释一下我要做什么吗?非常感谢
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;
};,我如何在故事结束时杀死会话和/或删除上下文,并重置进程请!
发布于 2017-03-20 03:32:21
您可能会删除旧会话,并在web钩子访问Wit.ai控制器之前创建一个新会话。
有关示例,请参见下面的代码:
Web钩子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 = {};
}https://stackoverflow.com/questions/41685057
复制相似问题