如何在我的漫游器中保留和更新上下文?

时间:2019-06-28 19:59:28

标签: javascript node.js express dialogflow serverless

我正在使用Google的Dialogflow NLP创建聊天机器人。在node.js中,我将导入dialogflow和dialogflow实现库,以处理Lambda函数中的自定义逻辑。这是通过webhook调用的,当Dialogflow拾取用户提供的意图时将调用该webhook。

当前,如果toLoc和fromLoc变量未满,我试图重新提示用户输入值。我相信我必须使用上下文,但是我不确定如何具体执行此操作。因此,总的来说,我的问题是我如何继续创建一个对话流程,要求用户输入缺少的参数值,直到满足所有值,然后仅为特定会话保存这些值。

const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const express = require('express');
const dialogflow = require('dialogflow');
const app = express();
app.use(bodyParser.json({ strict: false}));
const { WebhookClient, Payload, Image, Card, Suggestion } = require('dialogflow-fulfillment');
const req = require('request');

//const contextsClient = new dialogflow.createContext(dialogflow.contexts.create)
//const sessionClient = new dialogflow.cli

app.use(function(err, req, res, next){
    res.status(500);
    res.send(err);
});

app.post('/', function (req, res) {
    //Instantiate agent
    const agent = new WebhookClient({ request: req, response: res });

    let intentMap = new Map();

    function shuttle(agent){
        try{
                var toLoc = agent.parameters.to_loc;
                var fromLoc = agent.parameters.from_loc;

                console.log("From Location: ${fromLoc} To Location ${toLoc}" + fromLoc + "To Location: " + toLoc);
                if (toLoc === '' || fromLoc === '') {
                    if (fromLoc === 'Harvard') {
                        toLoc = '784';
                        agent.response_(`You're going from ${fromLoc} to ${toLoc}`);

                    } else if (fromLoc === 'Central') {
                        toLoc = '784';
                        agent.add(`You're going from ${fromLoc} to ${toLoc}`);
                    } else if (fromLoc === '784'){
                        agent.addResponse('Where do you want to go? (Harvard, Central)');
                    } else if (toLoc === 'Harvard') {
                        fromLoc = '784';
                        agent.add(`You're going from ${fromLoc} to ${toLoc}`);
                    } else if (toLoc === 'Central') {
                        fromLoc = '784'
                        agent.add(`You're going from ${fromLoc} to ${toLoc}`);
                    } else if (toLoc === '784'){
                        agent.response(`Where are you coming from? (Harvard, Central)`);
                    }
                } else {
                    agent.add(`Shuttle from ${fromLoc} to ${toLoc} is arriving...`);
                }
        }catch(error){
            console.log(error);
        }
    }
    intentMap.set('shuttle', shuttle);
    agent.handleRequest(intentMap);

});
module.exports.server = serverless(app);

在将fromLoc与784匹配时,在else if块中,我想再次提示用户询问他们要去哪里,同时仍保留fromLoc变量。如果您需要我重新构造这个问题,我将非常高兴! >

0 个答案:

没有答案