无法在Dialogflow中存储下一轮对话的变量列表

时间:2018-08-22 12:26:39

标签: javascript dialogflow

我真的很希望能够存储一个数组,其中包含在对话中获取的字符串变量,以便在下一次对话中进行检索,以便能够将旧数组与新数组进行比较。 即使我将数组的所有项目专门添加到新数组中,它也将在下一轮对话中消失。

谢谢。

这是我当前的功能:

function game(agent, parameters) {
    var suitcase = agent.parameters.objecten;
    var oldsuitcase = agent.parameters.oldsuitcase;
    if (suitcase.length > 1) {
        if (suitcase.splice(-1,1) != oldsuitcase) {
            agent.add('Helaas, je hebt niet de juiste objecten genoemd. Ik heb gewonnen! Volgende keer beter.');
            return;
        }
    }
    var items = [ "paspoort", "visum", "bankpas", "creditcard", "geld", "adressenlijst", "rijbewijs", "toiletspullen", "zonnebrandcrème", "shampo", "douchegel", "tandenstokers", "badlaken", "washandje", "handdoek", "deodorant", "maandverband", "nagelschaar", "tandenborstel", "tandpasta", "zakdoeken", "paracetamol", "medicijnen", "neusdruppels", "pleisters", "klamboe", "slippers", "broek", "rok", "jurk", "blouse", "shirt", "sokken", "onderbroek", "badpak", "zwembroek", "schoenen", "sandalen", "waterschoenen", "pet", "hoed", "zonnebril", "pyjama", "bril", "lenzenvloeistof", "horloge", "portemonnee", "brillenkoker", "tas", "sleutels", "boek", "tijdschrift", "spel", "pen", "zaklamp", "speelgoed", "zakmes", "schrijfpapier", "telefoon", "telefoonlader", "tablet", "camera", "reiswekker", "selfiestick", "reisgids", "chocola", "muggenlamp", "gitaar" ];
    var newitem = agent.parameters.objecten[0];
    while (suitcase.includes(newitem)) {
        newitem = items[Math.floor(Math.random()*items.length)];
    }
    for (var i = 0; i < suitcase.length; i++) {
        oldsuitcase.push(suitcase[i]);
        }
    suitcase.push(newitem);
    oldsuitcase.push(newitem);
    agent.add(oldsuitcase);
    agent.add('Ik ga op vakantie en ik neem mee:' + suitcase);
    }

1 个答案:

答案 0 :(得分:0)

在以下question中查看我的答案。

您可以像这样存储它

let param1 = [];
let param2 = {};
let ctx = {'name': '<context name>', 'lifespan': 5, 'parameters': {'param1':param1, 'param2': param2}};
agent.setContext(ctx);

并以其他意图将其获取

let params = agent.getContext("<context name>").parameters;
let param1 = params.param1;
let param2 = params.param2; 

播放寿命值以使其在会话中保持有效。