@0xc/dialogflow-as-code 中文文档教程
dialogflow-as-code
此包允许使用 nodejs-dialogflow 项目以及类型来创建 Dialogflow 资源我们帮助定义了 @types/dialogflow。 它是使用 Node.js 和 TypeScript 开发的。
构建器助手可用于生成资源,但是该模块还允许将对象直接传递给构建器。 构建器最终会生成 nodejs-dialogflow
API 所期望的相同类型,因此无论您发现哪个更容易,都可以正常工作。 对于一些更高级的类型,使用构建器会更容易。 下面提供了几个定义 Dialogflow 资源的示例,以及它们最终如何绑定在一起以创建并同步到 Dialogflow 代理:
// Sample Entity Type Builder
export const etFruit = entityType()
.d("fruit")
.e([syn("apple"), syn("strawberry")])
.k(ek.list)
.build();
// Sample Entity Type
export const etSample: EntityType = {
displayName: "sample",
entities: [{ value: "sample", synonyms: ["piece", "swab", "choice"] }],
kind: "KIND_MAP",
autoExpansionMode: "AUTO_EXPANSION_MODE_DEFAULT",
};
// Sample Context Builder
export const cxFruit = cx()
.n("fruit-context")
.lc(5)
.p("date-time-original", "string_value")
.build();
// Sample Events
export enum Event {
FEEDBACK = "FEEDBACK",
YES = "YES",
NO = "NO",
}
// Sample Intent
// prettier-ignore
export const ntFruitInfo = intent("fruitInfo")
.priority(Priority.LOW)
.webhook(true)
.trainingPhrases([
tp(["describe the ", pb("sample"), " of ", etFruit, " over ", det("date-time")]),
tp(["how was the ", pb("sample"), " of ", etFruit]),
tp([pb("sample"), " of ", etFruit, " ", det("date-time")]),
tp([pb("sample"), " of ", etFruit]),
tp(["what was the ", pb("sample"), "inputContextsf ", etFruit, " ", det("date-time"), "?"]),
tp(["what was the ", pb("sample"), " of ", etFruit]),
])
.messages([
msg("text").set(["I'm sorry Dave, I can't do that"]).build(),
msg("text").set(["Second response"]).build(),
])
.events([Event.FEEDBACK])
.outputContexts([cxFruit])
.followUpOf(ntFruitReminder);
// Sample Resource Build and Sync Script
const svcAcctKeyJson: string = "./service-account-key.json";
const svcAcctConfig: DialogflowServiceAccount = require(`.${svcAcctKeyJson}`);
Container.set(KEY_FILENAME, svcAcctKeyJson);
Container.set(DIALOGFLOW_CONFIG, svcAcctConfig);
const resources = Container.get(DialogflowBuilder)
.entityTypes([etSample, etFruit])
.intents([ntFruitInfo, ntFruitReminder])
.build();
Container.get(DialogflowCreator).sync(resources);
注意:您需要在 Dialogflow 网站上创建一个服务帐户并将其提供在项目级别为 service-account-key.json
。 文件路径和配置对象被传递给 DialogflowBuilder
。 路径可以改变,这只是示例和转换后的项目预期。
如果您想试一试,可以运行提供的项目示例:
git clone https://git.sr.ht/~tcarrio/dialogflow-as-code
pushd dialogflow-as-code
npm i
npm run sample
dialogflow-converter
该项目包含一个模块,用于将从 Dialogflow 导出的资源转换为供 dialogflow-as-code
使用的资源。 目前这些都是初步的,未来计划对 dialogflow-as-code
的模板结构和完整功能集进行改进。
通过调用 npm start -- -i $input_path -o $output_path
运行转换。 也可以直接用node
调用,调用node lib/converter/dialogflow-converter.js
。
如果安装了这个包,它在安装范围的 bin
目录中可用(本地 node_modules/.bin/dialogflow-as-code
或全局 $NODE_HOME/bin/dialogflow-as-code
)。
开始构建 dialogflow-as-code
项目的最简单方法是 npm i -g dialogflow-as-code
。 有了这个,您可以使用它的 CLI 指定输入和输出目录,以便从提取的 Dialogflow 导出中生成代码。
Usage: dialogflow-as-code [options]
Generate a Dialogflow-as-Code project from a Dialogflow export
Options:
-V, --version output the version number
-o, --output <dir> Output directory (default: ./output)
-i, --input <dir> Input directory (default: ./input)
-h, --help output usage information
转换器还利用 prettier 在项目创建后对其进行格式化。
dialogflow-as-code
This package allows the creation of Dialogflow resources by using the nodejs-dialogflow project along with the types we have helped define in @types/dialogflow. It was developed using Node.js and TypeScript.
The builder helpers can be used for generating resources, however the module also allows for objects to be directly passed to the builder. The builders eventually generate the same types expected by the nodejs-dialogflow
API, so whichever you find easier will work fine. For some of the more advanced types, it can be much easier to work with the builders. The following provides several examples of defining Dialogflow resources and how they eventually tie together to be created and synced to a Dialogflow agent:
// Sample Entity Type Builder
export const etFruit = entityType()
.d("fruit")
.e([syn("apple"), syn("strawberry")])
.k(ek.list)
.build();
// Sample Entity Type
export const etSample: EntityType = {
displayName: "sample",
entities: [{ value: "sample", synonyms: ["piece", "swab", "choice"] }],
kind: "KIND_MAP",
autoExpansionMode: "AUTO_EXPANSION_MODE_DEFAULT",
};
// Sample Context Builder
export const cxFruit = cx()
.n("fruit-context")
.lc(5)
.p("date-time-original", "string_value")
.build();
// Sample Events
export enum Event {
FEEDBACK = "FEEDBACK",
YES = "YES",
NO = "NO",
}
// Sample Intent
// prettier-ignore
export const ntFruitInfo = intent("fruitInfo")
.priority(Priority.LOW)
.webhook(true)
.trainingPhrases([
tp(["describe the ", pb("sample"), " of ", etFruit, " over ", det("date-time")]),
tp(["how was the ", pb("sample"), " of ", etFruit]),
tp([pb("sample"), " of ", etFruit, " ", det("date-time")]),
tp([pb("sample"), " of ", etFruit]),
tp(["what was the ", pb("sample"), "inputContextsf ", etFruit, " ", det("date-time"), "?"]),
tp(["what was the ", pb("sample"), " of ", etFruit]),
])
.messages([
msg("text").set(["I'm sorry Dave, I can't do that"]).build(),
msg("text").set(["Second response"]).build(),
])
.events([Event.FEEDBACK])
.outputContexts([cxFruit])
.followUpOf(ntFruitReminder);
// Sample Resource Build and Sync Script
const svcAcctKeyJson: string = "./service-account-key.json";
const svcAcctConfig: DialogflowServiceAccount = require(`.${svcAcctKeyJson}`);
Container.set(KEY_FILENAME, svcAcctKeyJson);
Container.set(DIALOGFLOW_CONFIG, svcAcctConfig);
const resources = Container.get(DialogflowBuilder)
.entityTypes([etSample, etFruit])
.intents([ntFruitInfo, ntFruitReminder])
.build();
Container.get(DialogflowCreator).sync(resources);
Note: You will need to create a service account on the Dialogflow website and provide it at the project level as service-account-key.json
. The file path and config object are passed to the DialogflowBuilder
. The path can be changed, this is just the sample and converted project expectations.
If you would like to to take it for a spin, you can run the provided project sample:
git clone https://git.sr.ht/~tcarrio/dialogflow-as-code
pushd dialogflow-as-code
npm i
npm run sample
dialogflow-converter
This project contains a module for converting resources exported from Dialogflow into resources for use by dialogflow-as-code
. These are rudimentary at the moment, with future improvements planned for the templating structure and full feature-set of dialogflow-as-code
.
Run the conversion by calling npm start -- -i $input_path -o $output_path
. It can also be directly invoked with node
, by calling node lib/converter/dialogflow-converter.js
.
If this package was installed, it is available at the bin
directory of the installation scope (locally at node_modules/.bin/dialogflow-as-code
or globally at $NODE_HOME/bin/dialogflow-as-code
).
The easiest way to get started with building out a dialogflow-as-code
project is to npm i -g dialogflow-as-code
. With this available you can use its CLI to specify input and output directories for generating the code from an extracted Dialogflow export.
Usage: dialogflow-as-code [options]
Generate a Dialogflow-as-Code project from a Dialogflow export
Options:
-V, --version output the version number
-o, --output <dir> Output directory (default: ./output)
-i, --input <dir> Input directory (default: ./input)
-h, --help output usage information
The converter also makes use of prettier to format the project after its creation.