1.0.2 • Published 3 years ago

@pepsterd/dialogflow-fulfillment-helper v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Dialogflow fulfillment helper

Introduction

This package was created to help developers quickly set up Dialogflow ES webhook fulfillment.

Getting started

Installation

npm install

Basic usage

Create a server that can handle Dialogflow fulfillment, in this example I will use Express.

import express, {Express, Request, Response} from "express";
const app: Express = express();
app.use(express.json());

app.post('/fulfillment', (req, res) => {
    // Read more about creating the fulfillmentHelper below
    res.send(fulfillmentHelper.handleRequest(req.body.queryResult));
})

Creating the repository

We first need to create a repository.

const intentHandlerRepository = new IntentHandlerRepository();

When developing we like to use classes to separate our logic. In this example we will create a Retailer class which returns the name of our restaurant.

const retailer = new Retailer().init();

export class Retailer extends BaseIntentHandler implements IntentHandlerInterface {
    mapIntents(): void {
        this.addIntent('my-intent-name', this.getName);
    }

    getName(): TextResponse {
        return new TextResponse({text: "Pizza Pepsterd"});
    }
}

//Register the retailer in the repository
intentHandlerRepository.register(retailer)

All classes that handle intent related logic must extend BaseIntentHandler and implement IntentHandlerInterface.\ The function mapIntents() Accepts intent display name and a callback. In this example the intent named my-intent-name will return Pizza Pepsterd.

Finishing

Lastly we need to create the fulfillmentHelper.

const fulfillmentHelper = new FulfillmentHelper(intentHandlerRepository, {language_code: 'nl-NL'});

language_code default = en_US.

Todo:

  • Create tests
  • Add more response types