Allows defining classes which serve as handlers via decorators.
Handler:
root.handler.ts
import { Input, Jovo } from 'jovo-core';
import { Handler, InputData, Intent, Session } from 'jovo-plugin-class-handler';
@Handler()
export class RootHandler {
@Intent()
LAUNCH(jovo: Jovo) {
return jovo.toIntent('HelloWorldIntent');
}
@Intent()
HelloWorldIntent(jovo: Jovo) {
jovo.$session.$data.example = 'nice to meet you!';
jovo.ask('Hello World! What's your name?', 'Please tell me your name.');
}
@Intent()
MyNameIsIntent(
jovo: Jovo,
@InputData('name') name: Input,
@Session('example') example: string
) {
jovo.tell(`Hey ${name.value}, ${example || 'no session data passed.'}`);
}
@Intent()
TestIntent(jovo: Jovo) {
jovo.$data.example = 'Hello this is the request-data example.';
return jovo.toStateIntent('example', 'TestIntent');
}
@Intent()
NestedTestIntent(jovo: Jovo) {
return jovo.toStateIntent('some.nested.state', 'TestIntent');
}
}or
import { BaseHandler, Data, Handler, Intent } from 'jovo-plugin-class-handler';
@Handler({ state: 'example' })
export class ExampleHandler extends BaseHandler {
@Intent({ name: 'TestIntent' })
someMethodName(@Data('example') example: string) {
this.tell(example || 'no request-data passed.');
}
}- jovo-plugin-class-handler
These instructions will get you the plugin installed and ready to be used.
- TypeScript v3.8 or newer
- jovo/jovo-framework v3.0 or newer
$ npm install jovo-plugin-class-handler --saveMake sure that
experimentalDecoratorsandemitDecoratorMetadataare set totruein thetsconfig.json
In your app.ts:
import { JovoClassHandler } from 'jovo-plugin-class-handler';
const app = new App();
app.use(
// ...
new JovoClassHandler()
);Handlers previously added by calling
app.setHandler(...)will be merged with handlers that were loaded by this plugin! It is recommended to not set any handlers.
You need to setup a configuration for the plugin in the config.ts or via the constructor in order for the plugin to detect all handlers.
This configuration assumes that all handlers follow the pattern: (name).handler.(ts or js).
In your config.ts:
const config = {
// ...
plugin: {
JovoClassHandler: {
handlers: [__dirname + '/**/*.handler.{ts,js}'],
},
},
// ...
};Parts of handlers can be a string like in the example above (regex is supported), but also a direct import:
import RootHandler from './handlers/root.handler';
const config = {
// ...
plugin: {
JovoClassHandler: {
handlers: [RootHandler],
},
},
// ...
};After following the installation the plugin is usable.
You can find a working example in the github-repository in the example folder.
To get started, create a new TypeScript file and export a class and annotate it with @Handler.
The class could look like this:
import { Handler } from 'jovo-plugin-class-handler';
@Handler()
export class RootHandler {}The first parameter of a
@Intentdecorated method is always aJovo-object if the handler is defined this way.
Additionally you can set the state of the handler:
import { Handler } from 'jovo-plugin-class-handler';
@Handler('example')
export class ExampleHandler {}You can also define a handler the following way:
import { BaseHandler, Data, Handler, Intent } from 'jovo-plugin-class-handler';
@Handler({ state: 'example' })
export class ExampleHandler extends BaseHandler {
@Intent({ name: 'TestIntent' })
someMethodName(@Data('example') example: string) {
this.tell(example || 'no request-data passed.');
}
}If you define a handler this way, you have access to the
Jovo-object viathis.
For more information look at the API here
After you have defined a handler you can define the intents. For that you have to annotate a method with @Intent.
Here is an example:
import { Handler, Intent } from 'jovo-plugin-class-handler';
import { Jovo } from 'jovo-core';
@Handler()
export class RootHandler {
@Intent()
LAUNCH(jovo: Jovo) {
return jovo.toIntent('HelloWorldIntent');
}
@Intent('HelloWorldIntent')
differentName(jovo: Jovo) {
jovo.ask(`Hello World! What's your name?`, 'Please tell me your name.');
}
@Intent({ name: 'MyNameIsIntent' })
anotherDifferentName(jovo: Jovo) {
jovo.tell(`Hey ... ${jovo.$inputs.name.value}`);
}
}For more information look at the API here
You can decorate @Intent-annotated methods with parameter decorators that bind data of the Jovo-object to the corresponding parameter.
| Decorator | Binds ... |
|---|---|
@Data(key?: string) / @RequestData(key?: string) |
$data / $data.{key} |
@Session(key?: string) / @SessionData(key?: string) |
$session.$data / $session.$data.{key} |
@User(key?: string) / @UserData(key?: string) |
$user.$data / $user.$data.{key} |
@AppData(key?: string) |
$app.$data / $app.$data.{key} |
@InputData(key?: string) |
$inputs / $inputs.{key} |
Example:
import { Handler, InputData, Intent, Session } from 'jovo-plugin-class-handler';
import { Input, Jovo } from 'jovo-core';
@Handler()
export class RootHandler {
@Intent()
LAUNCH(jovo: Jovo) {
return jovo.toIntent('HelloWorldIntent');
}
@Intent()
HelloWorldIntent(jovo: Jovo) {
jovo.$session.$data.example = 'nice to meet you!';
jovo.ask(`Hello World! What's your name?`, 'Please tell me your name.');
}
@Intent()
MyNameIsIntent(
jovo: Jovo,
@InputData('name') name: Input,
@Session('example') example: string
) {
jovo.tell(`Hey ${name.value}, ${example || 'no session data passed.'}`);
}
}
HandlerOptions:{state?: string}
The BaseHandler-class is just a wrapper that extends the Jovo-class and gets it's value injected at runtime.
If a @Handler decorated class extends the BaseHandler all properties and methods of the Jovo object are accessible via this.
-
if no
options: The handler's state will be stateless. -
if
optionsof typestring: The handler's state will be set tooptions. -
if
optionsof typeHandlerOptionsandstate: The handler's state will be set tooptions.state. -
if
optionsof typeHandlerOptionsand nostate: The handler will be stateless
IntentOptions:{name?: string}
Attention! The first parameter always is the
Jovo-object for a method that is decorated with@Intentif the handler does not extendBaseHandler.
-
if no
options: The intent's name will be the annotated method's name. -
if
optionsof typestring: The intent's name will be set tooptions. -
if
optionsof typeHandlerOptionsandstate: The intent's name will be set tooptions.name. -
if
optionsof typeHandlerOptionsand nostate: The intent's name will be the method's name
The first parameter of a
@Intentdecorated method is reserved for theJovo-object if the handler-class does not extendBaseHandler.
Binds $data or $data.{key} if key is given.
Binds $session.$data or $session.$data.{key} if key is given.
Binds $user.$data or $user.$data.{key} if key is given.
Binds $app.$data or $app.$data.{key} if key is given.
Binds $inputs or $inputs.{key} if key is given.