@0xc/dialogflow-as-code
v3.0.0-beta3
Published
 [](https://builds.sr.ht/~tcarrio/dialogflow-as-code/.build.yml?) [
.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 sampledialogflow-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 informationThe converter also makes use of prettier to format the project after its creation.
