如何将服务器的响应发布到对话流?

问题描述 投票:0回答:0

我把问题改得更明确了。

服务器端是node.js,我试图从外部API中获取天气等数据。

当我用POSTMAN发送一个问题时,我收到了来自服务器的响应。好了,现在的问题是...

我在Dialogflow上使用ngrok隧道,因为我需要HTTPS而不是HTTP,当我尝试Dialogflow接口时,没有从服务器得到相同的响应,就像我用POSTMAN一样,我没有收到任何东西,错误404。谁能帮帮我,好吗?

我攻击这里的代码与路由。


router.post('/weather/textQuery', async (req, res) => {
    var currentTime = new Date().getTime();
    if (currentTime + 4500 >= new Date().getTime()) {
        //currentTime
        try {


            const projectId = process.env.GOOGLE_PROJECT_ID_WEATHER
            const sessionId = process.env.DIALOGFLOW_SESSION_ID_WEATHER
            const languageCode = process.env.DIALOGFLOW_LANGUAGE_CODE


            // Create a new session
            const sessionClient = new dialogflow.SessionsClient();
            const sessionPath = sessionClient.sessionPath(projectId, sessionId);
            const request = {
                session: sessionPath,
                queryInput: {
                    text: {
                        // The query to send to the dialogflow agent
                        text: req.body.text,
                        // The language used by the client (en-US)
                        languageCode: languageCode,
                    },
                },
            };

            console.log(request)
            res.setHeader('Content-Type', 'application/json');

            const responses = sessionClient.detectIntent(request)
                .then(responses => {
                    const result = responses[0].queryResult;

                    if (result.action === 'input.weather') {
                        console.log("am intrat in if")

                        let city = result.parameters.fields['geo-city'].stringValue; // city is a required param
                        console.log("city" + city)
                        let date = '';
                        if (result.parameters.fields['date'].stringValue) {
                            date = (result.parameters.fields['date'].stringValue);
                            console.log('Date: ' + date);
                        }
                        //result.fulfillmentMessages[0].text[0].text= output
                        callWeatherApi(city, date).then((output) => {
                            var trimite = output

                            console.log("output ul de la API: " + output)
                            //response.json({ 'fulfillmentText': trimite });
                            let response = output;
                            let responseObj = {
                                "fulfillmentText": response
                                , "fulfillmentMessages": [{ "text": { "text": [trimite] } }]
                                , "source": ""
                            }
                            //res.send(JSON.stringify({speech:trimite, displayText: trimite, source:'api-weather'}));
                            return res.json(responseObj);

                        })

                            .catch(() => {
                                res.json({ 'fulfillmentText': `I don't know the weather but I hope it's good!` });
                            });

                    } else {
                        res.send(result);
                        console.log(`  Query: ${result.queryText}`);
                        console.log(`  Response: ${result.fulfillmentText}`);
                    }
                    //res.json({  'fulfillmentText': trimite });


                })
                .catch(err => {
                    console.error('ERROR:', err);
                });
        } catch (err) {
            console.log("Input is " + err);
        }
    }
console.log("current time response: " + currentTime)
})

在POSTMAN上,它的工作

{

    "text" : "weather in Paris tomorrow"
}

我收到了

{
    "fulfillmentText": "Current conditions in the City \n        Paris, France are Sunny with a projected high of\n        22°C or 71°F and a low of \n        14°C or 57°F on \n        2020-05-31.",
    "fulfillmentMessages": [
        {
            "text": {
                "text": [
                    "Current conditions in the City \n        Paris, France are Sunny with a projected high of\n        22°C or 71°F and a low of \n        14°C or 57°F on \n        2020-05-31."
                ]
            }
        }
    ],
    "source": ""
}

形象的实现和我试图得到的回应。

node.js http post postman dialogflow
© www.soinside.com 2019 - 2024. All rights reserved.