nestjs-google-actions-handler

Web hook handling for your Google Actions

Usage no npm install needed!

<script type="module">
  import nestjsGoogleActionsHandler from 'https://cdn.skypack.dev/nestjs-google-actions-handler';
</script>

README

GoogleActions module for NestJS :satellite:

Web hook handling for your Google Actions

Getting Started

To start using this module you should run the following command

npm i google-actions-handler @nestjs/common @nestjs/core reflect-metadata

Features

There are 2 decorators provided by the module that allow you to handle intent or pick properties from the response.

Name behavior
@GoogleActionsIntent('Intent')
public method(param: GoogleActionsResponse)
Handle the specified intent into the decorated method
@GoogleActionsParam('Intent')
public method(@GoogleActionsParam('query') param: QueryResult)
Get the value of the property specified through the parameter decorator

Set up

To use the module, you have to import it into your ApplicationModule and call the forRoot in order to initialize the module. The forRoot method can take as parameters an object with a basePath and a postPath in order to configure the controller used for the web hook.

@Module({
    imports: [
        GoogleActionsModule.forRoot({
            basePath: 'web-hooks',
            postPath: 'google-actions'
        })
    ]
})
export class ApplicationModule { }

The url with the default config should looks like https://myurl.me/web-hooks/google-actions

To handle an intent, you have to create your own Injectable that will implement all the methods needed in order to handle the concerned intents/action.

@Injectable()
export class MyGoogleActionsProvider {
    
    @GoogleActionsIntent('Intent1')
    public async handleMyIntent1(googleActionsResponse: GoogleActionsResponse): Promise<GoogleActionsFulfillmentResponse> {
        /* Your code here */
        return {} as DialogFlowFulfillmentResponse;
    }

    @GoogleActionsIntent('Intent2')
    public async handleMyIntent2(googleActionsResponse: GoogleActionsResponse): Promise<GoogleActionsFulfillmentResponse> {
        /* Your code here */
        return {} as DialogFlowFulfillmentResponse;
    }
    
}

You also have the possibility to pick any properties that you need directly from the googleActionsResponse, to get them from the handler parameters. To do that, you can use the @GoogleActionsParam decorator and pass as parameter a string path to the property that you want to pick.

@Injectable()
export class MyGoogleActionsProvider {
    
    @GoogleActionsIntent('Intent1')
    public async handleMyIntent1(@GoogleActionsParam('handler.name') handlerName: string): Promise<GoogleActionsFulfillmentResponse> {
        /* Your code here */
        return {} as GoogleActionsFulfillmentResponse;
    }
}