【问题标题】:how to use user sessions in dialogflow?如何在对话流中使用用户会话?
【发布时间】: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


    【解决方案1】:

    您可以将会话数据存储在会话本身中,并在 JSON 请求和响应中传递参数。您可以使用conv.data 提供要保存的变量键值对。

    app.intent('Default Welcome Intent', conv => {
      conv.data.someProperty = 'someValue'
    })
    

    【讨论】:

    • 我有类似这样的函数 function sessiontest(agent){ agent.add("session") } 我怎样才能对这种类型的函数做同样的事情? @尼克·费尔克
    • 我已经粘贴了代码,我怎样才能通过我想在 api 函数上使用会话变量的方式转换成你共享的东西
    • 你应该使用contexts来存储数据,即。 agent.context.set('sample context name');let context = agent.context.get('sample context name');
    • @xjd 请确认上述解决方案是否正常工作
    • 嘿@ArokyaNehrayous 我用过hashmaps 我不知道它的工作与否我想它应该工作他是专业人士我认为
    猜你喜欢
    • 2018-10-03
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多