From 98eb3636ef47a6eefbb189b67318284f177a3a3a Mon Sep 17 00:00:00 2001 From: Max Ripper Date: Thu, 27 Feb 2020 13:19:16 +0100 Subject: [PATCH 1/4] :sparkles: ActionBuilder can be passed to action-related methods Updated docs and examples as well. Also added shortcut for adding plain-speech to the ActionBuilder. --- docs/platforms/core-platform/README.md | 52 ++++++++++--------- .../javascript/06_core/hello-world/src/app.js | 1 + .../typescript/06_core/hello-world/src/app.ts | 1 + .../jovo-platform-core/src/ActionBuilder.ts | 9 ++-- .../src/core/CorePlatformApp.ts | 19 ++++--- 5 files changed, 46 insertions(+), 36 deletions(-) diff --git a/docs/platforms/core-platform/README.md b/docs/platforms/core-platform/README.md index 85ac20465d..d20998732b 100644 --- a/docs/platforms/core-platform/README.md +++ b/docs/platforms/core-platform/README.md @@ -5,11 +5,13 @@ Learn more about the Jovo Core Platform, which can be used to deploy a voice exp ## Installation Install the integration into your project directory. + ```sh npm install jovo-platform-core --save ``` -Import the installed module, initialize and add it to the `app` object. +Import the installed module, initialize and add it to the `app` object. + ```javascript // @language=javascript @@ -114,64 +116,64 @@ this.$corePlatformApp?.$actions.addSpeech({ #### Adding Actions +Adds the given actions or actions of the given ActionBuilder. + ```javascript // @language=javascript if (this.$corePlatformApp) - this.$corePlatformApp.addActions({ - type: ActionType.Speech, - plain: 'plainText' - }); + this.$corePlatformApp.addActions(this.$corePlatformApp.$actions); // @language=typescript -this.$corePlatformApp?.addActions({ - type: ActionType.Speech, - plain: 'plainText' -}); +this.$corePlatformApp?.addActions(this.$corePlatformApp?.$actions); ``` #### Setting Actions +Sets the current actions to the given actions or to the actions of the given ActionBuilder. + +> **INFO** The actions generated for the speech of `tell` and `ask` will NOT be overwritten. + ```javascript // @language=javascript if (this.$corePlatformApp) - this.$corePlatformApp.setActions( - this.$actions.addSpeech({ plain: 'text' }).build() - ); + this.$corePlatformApp.setActions(this.$corePlatformApp.$actions); // @language=typescript -this.$corePlatformApp?.setActions( - this.$actions.addSpeech({ plain: 'text' }).build() -); +this.$corePlatformApp?.setActions(this.$corePlatformApp?.$actions); ``` #### Adding RepromptActions +Adds the given actions or actions of the given ActionBuilder. + ```javascript // @language=javascript if (this.$corePlatformApp) - this.$corePlatformApp.addRepromptActions({ - type: ActionType.Speech, - plain: 'plainText' - }); + this.$corePlatformApp.addRepromptActions( + this.$corePlatformApp.$repromptActions + ); // @language=typescript -this.$corePlatformApp?.addRepromptActions({ - type: ActionType.Speech, - plain: 'plainText' -}); +this.$corePlatformApp?.addRepromptActions( + this.$corePlatformApp?.$repromptActions +); ``` #### Setting RepromptActions +Sets the reprompt actions to the given actions or to the actions of the given ActionBuilder. + +> **INFO** The action generated for the reprompt of `ask` will NOT be overwritten. + ```javascript // @language=javascript if (this.$corePlatformApp) this.$corePlatformApp.setRepromptActions( - this.$repromptActions.addSpeech({ plain: 'text' }).build() + this.$corePlatformApp.$repromptActions ); // @language=typescript this.$corePlatformApp?.setRepromptActions( - this.$repromptActions.addSpeech({ plain: 'text' }).build() + this.$corePlatformApp?.$repromptActions ); ``` diff --git a/examples/javascript/06_core/hello-world/src/app.js b/examples/javascript/06_core/hello-world/src/app.js index 861ce76873..d69738d898 100644 --- a/examples/javascript/06_core/hello-world/src/app.js +++ b/examples/javascript/06_core/hello-world/src/app.js @@ -43,6 +43,7 @@ app.setHandler({ HelloWorldIntent() { if (this.$corePlatformApp) { this.$corePlatformApp.$actions.addQuickReplies(['John', 'Jack', 'Joe']); + this.$corePlatformApp.setActions(this.$corePlatformApp.$actions); } this.ask("Hello World! What's your name?", 'Please tell me your name.'); }, diff --git a/examples/typescript/06_core/hello-world/src/app.ts b/examples/typescript/06_core/hello-world/src/app.ts index 66bd57760f..fea8759894 100644 --- a/examples/typescript/06_core/hello-world/src/app.ts +++ b/examples/typescript/06_core/hello-world/src/app.ts @@ -31,6 +31,7 @@ app.setHandler({ HelloWorldIntent() { this.$corePlatformApp?.$actions.addQuickReplies(['John', 'Jack', 'Joe']); + this.$corePlatformApp?.setActions(this.$corePlatformApp?.$actions); this.ask("Hello World! What's your name?", 'Please tell me your name.'); }, diff --git a/jovo-platforms/jovo-platform-core/src/ActionBuilder.ts b/jovo-platforms/jovo-platform-core/src/ActionBuilder.ts index 2ae63b10b3..05f147f586 100644 --- a/jovo-platforms/jovo-platform-core/src/ActionBuilder.ts +++ b/jovo-platforms/jovo-platform-core/src/ActionBuilder.ts @@ -4,7 +4,6 @@ import { AudioActionData, ProcessingActionData, QuickReply, - QuickReplyActionData, SpeechActionData, } from './Interfaces'; @@ -25,8 +24,12 @@ export class ActionBuilder { return this; } - addSpeech(data: SpeechActionData): ActionBuilder { - this.actions.push({ type: ActionType.Speech, ...data }); + addSpeech(data: SpeechActionData | string): ActionBuilder { + const action = + typeof data === 'string' + ? { type: ActionType.Speech, plain: data } + : { type: ActionType.Speech, ...data }; + this.actions.push(action); return this; } diff --git a/jovo-platforms/jovo-platform-core/src/core/CorePlatformApp.ts b/jovo-platforms/jovo-platform-core/src/core/CorePlatformApp.ts index e8abab8f89..edcfdd7206 100644 --- a/jovo-platforms/jovo-platform-core/src/core/CorePlatformApp.ts +++ b/jovo-platforms/jovo-platform-core/src/core/CorePlatformApp.ts @@ -91,23 +91,26 @@ export class CorePlatformApp extends Jovo { } // Output methods - setActions(actions: Action[]): CorePlatformApp { - this.$output.CorePlatform.Actions = actions; + setActions(actions: Action[] | ActionBuilder): CorePlatformApp { + this.$output.CorePlatform.Actions = + actions instanceof ActionBuilder ? actions.build() : actions; return this; } - addActions(...actions: Action[]): CorePlatformApp { - this.$output.CorePlatform.Actions.push(...actions); + addActions(actions: Action[] | ActionBuilder): CorePlatformApp { + this.$output.CorePlatform.Actions.push( + ...(actions instanceof ActionBuilder ? actions.build() : actions), + ); return this; } - setRepromptActions(actions: Action[]): CorePlatformApp { - this.$output.CorePlatform.RepromptActions = actions; + setRepromptActions(actions: Action[] | ActionBuilder): CorePlatformApp { + this.$output.CorePlatform.RepromptActions = actions instanceof ActionBuilder ? actions.build() : actions; return this; } - addRepromptActions(...actions: Action[]): CorePlatformApp { - this.$output.CorePlatform.RepromptActions.push(...actions); + addRepromptActions(actions: Action[] | ActionBuilder): CorePlatformApp { + this.$output.CorePlatform.RepromptActions.push(...(actions instanceof ActionBuilder ? actions.build() : actions)); return this; } } From 158db1d4f9326f9b0034041d44defcaeb4c83e07 Mon Sep 17 00:00:00 2001 From: Max Ripper Date: Thu, 27 Feb 2020 13:21:43 +0100 Subject: [PATCH 2/4] :twisted_rightwards_arrows: Merge remote-tracking branch 'upstream/master' into improvements/core-platform --- docs/platforms/core-platform/README.md | 2 +- docs/platforms/facebook-messenger/README.md | 2 +- .../typescript/06_core/hello-world/src/app.ts | 2 +- .../src/DashbotDialogflow.ts | 46 +++++++++++++++++++ .../jovo-analytics-dashbot/src/index.ts | 4 ++ 5 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 jovo-integrations/jovo-analytics-dashbot/src/DashbotDialogflow.ts diff --git a/docs/platforms/core-platform/README.md b/docs/platforms/core-platform/README.md index d20998732b..15ceaf104b 100644 --- a/docs/platforms/core-platform/README.md +++ b/docs/platforms/core-platform/README.md @@ -48,7 +48,7 @@ The returned object will be an instance of `CorePlatformApp` if the current requ ## Output -These sections provide an overview of Core Platform specific features for output. \ +These sections provide an overview of Core Platform specific features for output. For the basic concept, take a look here: [Basic Concepts > Output](../../basic-concepts/output './output'). ### Actions and the ActionBuilder diff --git a/docs/platforms/facebook-messenger/README.md b/docs/platforms/facebook-messenger/README.md index 37652b6294..230c5f7a51 100644 --- a/docs/platforms/facebook-messenger/README.md +++ b/docs/platforms/facebook-messenger/README.md @@ -170,7 +170,7 @@ The returned object will be an instance of `MessengerBot` if the current request ## Output -These sections provide an overview of Facebook Messenger specific features for output. \ +These sections provide an overview of Facebook Messenger specific features for output. For the basic concept, take a look here: [Basic Concepts > Output](../../basic-concepts/output './output'). ### No Reprompts diff --git a/examples/typescript/06_core/hello-world/src/app.ts b/examples/typescript/06_core/hello-world/src/app.ts index fea8759894..6270f05e20 100644 --- a/examples/typescript/06_core/hello-world/src/app.ts +++ b/examples/typescript/06_core/hello-world/src/app.ts @@ -30,7 +30,7 @@ app.setHandler({ }, HelloWorldIntent() { - this.$corePlatformApp?.$actions.addQuickReplies(['John', 'Jack', 'Joe']); + this.$corePlatformApp?.$actions.addQuickReplies(['Joe', 'Jane', 'Max']); this.$corePlatformApp?.setActions(this.$corePlatformApp?.$actions); this.ask("Hello World! What's your name?", 'Please tell me your name.'); }, diff --git a/jovo-integrations/jovo-analytics-dashbot/src/DashbotDialogflow.ts b/jovo-integrations/jovo-analytics-dashbot/src/DashbotDialogflow.ts new file mode 100644 index 0000000000..9cbc9224bc --- /dev/null +++ b/jovo-integrations/jovo-analytics-dashbot/src/DashbotDialogflow.ts @@ -0,0 +1,46 @@ +import { Google } from 'dashbot'; +import * as dashbot from 'dashbot'; // tslint:disable-line +import { Analytics, BaseApp, HandleRequest, PluginConfig } from 'jovo-core'; +import _merge = require('lodash.merge'); + +export interface Config extends PluginConfig { + key: string; +} + +export class DashbotDialogflow implements Analytics { + config: Config = { + key: '', + }; + dashbot!: Google; + + constructor(config?: Config) { + if (config) { + this.config = _merge(this.config, config); + } + this.track = this.track.bind(this); + } + + install(app: BaseApp) { + // @ts-ignore + this.dashbot = dashbot(this.config.key).google; + app.on('response', this.track); + } + + uninstall(app: BaseApp) { + app.removeListener('response', this.track); + } + + track(handleRequest: HandleRequest) { + if (!handleRequest.jovo) { + return; + } + + if (handleRequest.jovo.constructor.name === 'DialogflowAgent') { + this.dashbot.logIncoming(handleRequest.host.getRequestObject()); + this.dashbot.logOutgoing( + handleRequest.host.getRequestObject(), + handleRequest.jovo.$response!, + ); + } + } +} diff --git a/jovo-integrations/jovo-analytics-dashbot/src/index.ts b/jovo-integrations/jovo-analytics-dashbot/src/index.ts index ee7d281611..ff8114cf53 100644 --- a/jovo-integrations/jovo-analytics-dashbot/src/index.ts +++ b/jovo-integrations/jovo-analytics-dashbot/src/index.ts @@ -1,9 +1,12 @@ import { Config as AlexaConfig } from './DashbotAlexa'; +import { Config as DialogflowConfig } from './DashbotDialogflow'; import { Config as GoogleAssistantConfig } from './DashbotGoogleAssistant'; interface AppDashbotConfig { DashbotAlexa?: AlexaConfig; DashbotGoogleAssistant?: GoogleAssistantConfig; + DashbotDialogflow?: DialogflowConfig; + } declare module 'jovo-core/dist/src/Interfaces' { @@ -13,3 +16,4 @@ declare module 'jovo-core/dist/src/Interfaces' { export { DashbotAlexa, Config as AlexaConfig } from './DashbotAlexa'; export { DashbotGoogleAssistant, Config as GoogleAssistantConfig } from './DashbotGoogleAssistant'; +export { DashbotDialogflow, Config as DialogflowConfig } from './DashbotDialogflow'; From f77d40d446bcb6bc4eb32133b144918848a728d0 Mon Sep 17 00:00:00 2001 From: Max Ripper Date: Thu, 27 Feb 2020 13:24:43 +0100 Subject: [PATCH 3/4] :art: Added missing line to Core platform example --- examples/typescript/06_core/hello-world/src/app.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/typescript/06_core/hello-world/src/app.ts b/examples/typescript/06_core/hello-world/src/app.ts index 3bff43baf0..6270f05e20 100644 --- a/examples/typescript/06_core/hello-world/src/app.ts +++ b/examples/typescript/06_core/hello-world/src/app.ts @@ -31,6 +31,7 @@ app.setHandler({ HelloWorldIntent() { this.$corePlatformApp?.$actions.addQuickReplies(['Joe', 'Jane', 'Max']); + this.$corePlatformApp?.setActions(this.$corePlatformApp?.$actions); this.ask("Hello World! What's your name?", 'Please tell me your name.'); }, From 18f4c6b4e48a57d086f6d4a03cc4eeceb3432822 Mon Sep 17 00:00:00 2001 From: Max Ripper Date: Thu, 27 Feb 2020 13:27:42 +0100 Subject: [PATCH 4/4] :art: Fixed Code style --- .../jovo-platform-core/src/core/CorePlatformApp.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/jovo-platforms/jovo-platform-core/src/core/CorePlatformApp.ts b/jovo-platforms/jovo-platform-core/src/core/CorePlatformApp.ts index edcfdd7206..377072d2ca 100644 --- a/jovo-platforms/jovo-platform-core/src/core/CorePlatformApp.ts +++ b/jovo-platforms/jovo-platform-core/src/core/CorePlatformApp.ts @@ -105,12 +105,15 @@ export class CorePlatformApp extends Jovo { } setRepromptActions(actions: Action[] | ActionBuilder): CorePlatformApp { - this.$output.CorePlatform.RepromptActions = actions instanceof ActionBuilder ? actions.build() : actions; + this.$output.CorePlatform.RepromptActions = + actions instanceof ActionBuilder ? actions.build() : actions; return this; } addRepromptActions(actions: Action[] | ActionBuilder): CorePlatformApp { - this.$output.CorePlatform.RepromptActions.push(...(actions instanceof ActionBuilder ? actions.build() : actions)); + this.$output.CorePlatform.RepromptActions.push( + ...(actions instanceof ActionBuilder ? actions.build() : actions), + ); return this; } }