在与Google的“操作”对话中保存数据

时间:2018-12-11 10:45:02

标签: node.js dialogflow actions-on-google

我正在Google的Actions上开发一个应用程序,我注意到使用Dialogflow Fulfillment library时无法保存对话之间的数据。 这是使用WebhookClient的代码:

const { WebhookClient, Card, Suggestion } = require('dialogflow-fulfillment');
exports.aog_app = functions.https.onRequest((request, response)=>{
  let agent = new WebhookClient({request, response});
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', (agent)=>{
    agent.add("hello there!") ;
  });
  intentMap.set('presentation', (agent)=>{
    let conv = agent.conv();
    let counter = conv.data.counter;
    console.log("counter", counter)
    if(counter){
      conv.data.counter = counter+1;
    }else{
      conv.data.counter = 1;
    }
    agent.add("counter is "+counter) ;
  });
  agent.handleRequest(intentMap)
});

counter每次旋转都保持未定义状态。

但是当使用Action on Google Nodejs Library时,我可以毫无问题地保存数据:

const {
  dialogflow,
  SimpleResponse,
  BasicCard,
  Permission,
  Suggestions,
  BrowseCarousel,
  BrowseCarouselItem,
  Button,
  Carousel,
  DateTime,
  Image,
  DialogflowConversation
} = require('actions-on-google');
const app = dialogflow({debug: true});
app.intent('Default Welcome Intent', (conv)=>{
  conv.ask("hello there!");
});
app.intent('presentation', (conv)=>{
  let counter = conv.data.counter;
  console.log("counter", counter)
  if(counter){
    conv.data.counter = counter+1;
  }else{
    conv.data.counter = 1;
  }
  conv.ask("counter is "+counter)

})
exports.aog_app = functions.https.onRequest(app);

counter每转一圈。

是否有一种方法可以使用Dialogflow履行库在对话之间保存数据?

2 个答案:

答案 0 :(得分:3)

您需要在更新conv后将conv.data重新添加到代理中

agent.add(conv);

答案 1 :(得分:0)

Google的Dialogflow团队发布了一个代码示例,展示了如何通过集成Google Cloud的Firebase Cloud Firestore来实现数据持久性。

您可以在此处找到示例:https://github.com/dialogflow/fulfillment-firestore-nodejs

(可能是)您正在寻找的代码:

#!/bin/bash
while [ "$response" != "n" ]
do
read -p "What is the answer? " answer
sed -i "s/placeholder/$answer/g" file.txt
  exit
done