首页 > 解决方案 > 如何从 node.js 对话框流包设置对话框流上下文

问题描述

我正在使用sessionClient.detectIntent()将文本发送到对话流并且它工作正常,现在我也想用文本发送上下文。我怎样才能做到这一点?

标签: node.jsdialogflow-esdialogflow-es-fulfillmentdialogflow-cx

解决方案


我通常通过以下方式创建上下文:

exports.createContext = async function(contextId, parameters, sessionPath, sessionId, lifespan = 333) {
    let contextPath;
    try {
        contextPath = contextClient.contextPath(projectId, sessionId, contextId);

    } catch (e) {
        console.error("Error => ");
        console.error(e)
    }
    const request = {
        parent: sessionPath,
        context: {
            name: contextPath,
            parameters: struct.encode(parameters),
            lifespanCount: lifespan
        }
    };
    contextClient.createContext(request)
};

当我必须在调用 detectIntent 方法之前创建上下文时,只需调用此方法即可:

bot.createContext('CONTEXT_NAME_GOES_HERE', '{PARAMETER: VALUE}', sessionPath, session_id, lifespan = 1);

推荐阅读