Dialogflow代理未在agent.conv()。data中存储数据 [英] Dialogflow agent isn't storing data in agent.conv().data

查看:50
本文介绍了Dialogflow代理未在agent.conv()。data中存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在dialogflow实现中,处理程序如下所示:

  exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request,response) => {
const agent = new WebhookClient({request,response});
let conv = agent.conv();
console.log('Dialogflow Request headers:'+ JSON .stringify(request.headers));
console.log('Dialogflow Request body:'+ JSON.stringify(request.body));

我已将conv设置为agent.conv()。用户受到欢迎,我想存储一个随机生成的数字。

 函数welcome(agent){
agent.add(`Welcome to Med Ed!I can get your name?`);
//生成随机数number-> num
conv.data.random = num
console.log(conv.data.random)
}

上面的方法工作正常,并且该数字已打印到控制台上,但是当调用另一个函数处理程序时,我尝试提取该数字:

 函数intentHandler(agent){
//从agent.conv()
getNum = conv.data.random
console.log(getNum)
}

打印出来的getNum对控制台来说是NaN。 / p>

我以为数据仍然存在,但我显然错了。



谢谢

解决方案

似乎您是在Google Cloud Platform或Dialogflow内联编辑器中使用Cloud Functions。



在两种情况下,履行代码均在Cloud Functions中运行,该函数应为 stateless 。每个请求都可以在不同的环境中执行;因此,如果需要存储变量,建议添加一个额外的持久层,例如数据库。



对于您的用例,您可以提出三点建议考虑:



1)使用全局变量。通常,这些函数将在相同的环境中执行,因此,您可以将信息存储在全局变量中。但是,这不能保证,因此您应该处理该信息不可用的情况



2)将Dialogflow上下文用作临时存储。您可以将随机数存储为上下文中的参数,只要上下文处于活动状态,它将通过Dialogflow发送到webhook



3)添加持久性功能层。在GCP中,您可以使用 Firestore 数据库更适合您的情况


In dialogflow fulfilment the handler looks like:

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  let conv = agent.conv();
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

I've set conv to agent.conv(). The user is welcomed and I want to store a randomly generated number.

  function welcome(agent) {
    agent.add(`Welcome to Med Ed! Can I get your name?`);
    //generate random number -> num
    conv.data.random = num
    console.log(conv.data.random)
  } 

The above works fine and the number is printed to the console. But when another function handler is called and I try and extract the number:

 function intentHandler(agent) {
        // get the number from the agent.conv()
        getNum = conv.data.random 
        console.log(getNum)
 } 

It's print getNum is NaN to the console.

I thought data persists but I'm obviously wrong.

Thanks

解决方案

It seems that you're using Cloud Functions either in Google Cloud Platform or from the Dialogflow inline editor.

In both cases, the fulfillment code runs in Cloud Functions which should be stateless. Each request could be executed in a different environment; therefore, if it's necessary to store variables, is recommended to add an additional persistance layer, for example, a database.

For your use case, there are three suggestions you can consider:

1) Use a global variable. Normally, the functions will be executed in the same environment, so, you could store information in a global variable; however, this is not guaranteed, so you should handle the case where that information is not available

2) Use Dialogflow contexts as temporary storage. You can store the random number as a parameter in the context and it will be send by Dialogflow to the webhook, as long as the context is active

3) Add a persistance layer to your functions. Within GCP you could use Firestore or the database that better fits your scenario

这篇关于Dialogflow代理未在agent.conv()。data中存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆