Google Actions sdk - 无法将app.data保留在Q / A会话之外

时间:2018-03-16 16:23:30

标签: javascript actions-on-google

我有一个简单的游戏,我在用户和GA之间的Q / A会话之间传递值。但是,我注意到app.state没有为下一个会话维护值。

它基本上会询问真/假类型的问题并传递这些问题以进行检查和验证。

示例代码

exports.myQNAFunction = functions.https.onRequest((request, response) => {
 // get an instance of the Google Actions SDK
const app = new ActionsSdkApp({request: request, response: response});
app.data = {answer: ''}

// Function to handle the built in intents
function handleBuiltInIntent (app) {

    // Get dialog state to know where we are in the conversation
    var dialogState = app.getDialogState();

    // get our custom parameter
    var stage = dialogState.stage;
    console.info('handleBuiltInIntent: ' + stage);
    console.info('app.data.answer: ' + app.data.answer);

    // Check where we are in the conversation
    if(stage === 1) {
        console.info("block stage 1 ")
        let rawInput = app.getRawInput();
        if(rawInput === 'ok' || rawInput === 'yes' || rawInput === 'sure') {
            // user wants to know more
            app.tell('Ok then, here are the instructions!');
        } else if(rawInput === 'no') {
            dialogState.stage = 2;
            let question = getNextQuestion();
            let answer = qna_object_dict[question];
            app.data.answer = answer
            app.ask(question, dialogState);
        } else {
            // we finish the dialog
            app.tell('Ok then, goodbye!');
        }
    } else if(stage === 2) {
        console.info("block stage 2 ")
        let rawInput = app.getRawInput();
        if (rawInput === app.data.answer) {
            console.info(app.data.answer)
            dialogState.stage = 2;
            let question = getNextQuestion();
            let answer = qna_object_dict[question];
            app.data.answer = answer
            app.ask(question, dialogState);
        } else {
            app.tell('Wong Answer, goodbye!');
        }
    } else {
        // Build a prompt
        console.info("block stage 0 ")
        let welcome_string = welcomePrompt()
        let inputPrompt = app.buildInputPrompt(false,
            welcome_string);
        dialogState.stage = 1;
        app.ask(inputPrompt, dialogState);
    }
}

});

1 个答案:

答案 0 :(得分:2)

app.data是特定于会话的数据,用于维持单个会话的不同步骤之间的状态。

如果您希望在会话/会话之间为给定用户保留数据,则可以使用app.userStorage