在 Node.js 上的打字稿文件之间拆分 Actions-on-Google 意图

Splitting Actions-on-Google intents between typescript files on Node.js

在我的 src/index.ts 文件中,我的 dialogflow + google 操作的 firebase 函数我有如下代码:

import * as functions from 'firebase-functions';
import { dialogflow, Suggestions } from 'actions-on-google';

const app = dialogflow({debug: true})
app.intent('Default Welcome Intent', conv => {
conv.ask('Question')
});

exports.dialogflowFulfillment = functions.https.onRequest(app);

我想将每个意图拆分到它自己的 TS 文件中(因为我打算有很多),但不知道如何从单独的 .ts 文件中导出每个意图以与 app

如有任何想法,我们将不胜感激

在我看来,作为 TypeScript 程序员的一个组织拥有一组 TypeScript 文件,每个文件都可以访问 app 以添加自己的意图。我已经避免了 index.ts 之外的副作用和模块之间的循环依赖。 (也许熟悉 Dialogflow 或 Actions on Google 的人会有他们自己的建议。)

// app.ts
import { dialogflow } from 'actions-on-google';

export function createApp() {
    return dialogflow({debug: true});
}
export type App = ReturnType<typeof createApp>;

// intent1.ts
import { App } from "./app";

export function addIntent1(app: App) {
    app.intent('Default Welcome Intent', conv => {
        conv.ask('Question')
    });        
}

// index.ts
import * as functions from 'firebase-functions';
import { createApp } from './app';
import { addIntent1 } from './intent1';

const app = createApp();
addIntent1(app);

exports.dialogflowFulfillment = functions.https.onRequest(app);