问题
I changed the question to be more explicitly:
The server side is node.js and I try to get data like weather from an external API.
When I send a question with POSTMAN I receive a response from the server. Ok now, the issue...
I use ngrok tunnel on Dialogflow because I need HTTPS not HTTP, and when I try the Dialogflow interface no get the same response from the server as I do with POSTMAN I didn't receive anything, error 404. Can someone help me, please?
I attack here the code with the route.
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)
})
ON POSTMAN IT'S WORK
{
"text" : "weather in Paris tomorrow"
}
I received that
{
"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": ""
}
Image of fulfillment and were I try to get the response
来源:https://stackoverflow.com/questions/62121780/how-to-post-response-from-server-to-dialogflow