【发布时间】:2021-06-27 03:52:40
【问题描述】:
我想在我的 Dialogflow 机器人中使用用户会话,目前发生的情况是,当我将某些内容保存在 webhook 的变量中时,它会保存给所有用户>
const axios = require('axios');
const express = require('express')
const {WebhookClient} = require('dialogflow-fulfillment')
var cookieParser = require('cookie-parser');
var session = require('express-session');
const app = express()
app.use(express.json())
app.get('/', (req, res) => {
res.send("Server Is Working.....")
})
app.post('/webhook', (req, res) => {
// get agent from request
let agent = new WebhookClient({request: req, response: res})
// create intentMap for handle intent
let intentMap = new Map();
// add intent map 2nd parameter pass function
intentMap.set('weather',weatherIntent)
intentMap.set('sessiontest',sessiontest)
// now agent is handle request and pass intent map
agent.handleRequest(intentMap)
})
function sessiontest(agent){
agent.add("session")
}
function weatherIntent(agent){
const apiKey = "23756992e06787aa9225e9b361dfcd66"
const city = agent.parameters.city;
const url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=imperial`
return axios.get(url)
.then(function (response) {
// handle success
console.log(response.data.main.temp);
agent.add("Temp in " + city + " is " + response.data.main.temp)
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always execut
});
}
app.listen(3000, () => {
console.log("Server is Running on port 3000")
})
【问题讨论】:
标签: node.js session dialogflow-es chatbot actions-on-google