@0xc/dialogflow-as-code
v3.0.0-beta3
Published
![npm (scoped)](https://img.shields.io/npm/v/@0xc/dialogflow-as-code) [![builds.sr.ht status](https://builds.sr.ht/~tcarrio/dialogflow-as-code/.build.yml.svg)](https://builds.sr.ht/~tcarrio/dialogflow-as-code/.build.yml?) [![License](https://img.shields.i
Downloads
24
Readme
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.