Jovo Hooks
Middleware hooks are the easiest way to extend certain parts of the Jovo Framework. You can see them as a lightweigt version of Jovo Plugins.
Watch the video here:
Introduction
Jovo Hooks allow you to "hook" into middlewares
of the Jovo Architecture.
Here is how a hook could look like:
Here is a list of all available middlewares:
Middleware | Description |
---|---|
setup |
First initialization of app object with first incoming request. Is executed once as long as app is alive |
request |
Raw JSON request from platform gets processed. Can be used for authentication middlewares. |
platform.init |
Determines which platform (e.g. Alexa , GoogleAssistant ) sent the request. Initialization of abstracted jovo (this ) object. |
platform.nlu |
Natural language understanding (NLU) information gets extracted for built-in NLUs (e.g. Alexa ). Intents and inputs are set. |
nlu |
Request gets routed through external NLU (e.g. Dialogflow standalone). Intents and inputs are set. |
user.load |
Initialization of user object. User data is retrieved from database. |
router |
Request and NLU data (intent, input, state) is passed to router. intentMap and inputMap are executed. Handler path is generated. |
handler |
Handler logic is executed. Output object is created and finalized. |
user.save |
User gets finalized, DB operations. |
platform.output |
Platform response JSON gets created from output object. |
response |
Response gets sent back to platform. |
fail |
Errors get handled if applicable. |
Hook Elements
Each middleware hook contains of a specified middleware
and an anonymous function that is called with the following parameters:
error
The error
is undefined
unless the fail
middleware is used.
The error
property has the following interface:
host
The second property is the host
object (Lambda, Azure, ExpressJS, etc.), which has the following interface:
jovo
The jovo
object is the third property, which is the same object you reference inside your handler using this
.
Depending on the middleware you use, the object can be undefined
, since the initialization happens inside the platform.init
middleware.
Middleware Execution
Each middleware can either be accessed by using its name (e.g. platform.output
), or by prepending before
(e.g. before.platform.output
) or after
(e.g. after.platform.output
) to specify when in the order the hook should be triggered.
For example, the order of platform.output
and the following response
middlewares looks like this:
The anonymous function that you pass into the hook is then called when it is reached in the stack of middlewares.
If you need to do API calls and await them in your hooks, use asnyc/await
: