首页 > 解决方案 > 使用 Dialogflow Conv.Data 或 Conv.User.Storage 的简单计数器

问题描述

我正在寻找使用谷歌操作在firebase函数中的对话中创建一个超级简单的计数器。

文档建议:

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

但是,typescript不会将 conv.data 之后的任何类型的点表示法识别为值,并且不允许部署代码。

但是,据我所知,使用

app.intent('Default Welcome Intent', conv => {
 conv.data["someProperty"] = 1;
})

可以,但似乎不允许计算 int ...

我努力了:

conv.data['currentIndex'] = parseInt(conv.data['currentIndex']) + 1;


conv.data['currentIndex'] = parseInt(conv.data['currentIndex'])++;


conv.data['currentIndex'] += 1;

我觉得我在这里错过了一些超级基本的东西。

谢谢

标签: node.jstypescriptgoogle-cloud-functionsdialogflow-esactions-on-google

解决方案


我认为您需要明确指定要使用的变量类型。

尝试定义如下接口:

//use this in conv.data

interface ConvData {
  counter?: number  
}

// use in conv.user.storage
interface UserStorage {
  location?: string
  name?: string
}

并将应用程序初始化为:

const app = dialogflow<ConvData, UserStorage>({ debug: true })

然后使用

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

参考:Google TS 示例上的操作


推荐阅读