Dialoglfow fulfillment with Spring Boot

Carvia Tech | November 21, 2020 | 2 min read | 649 views


In this tutorial we will learn how to implement Google Dialogflow response fulfilment webhook API with a Spring Boot application.

Gradle setup

Create a simple Spring Boot starter project using Spring Initializer and add the Google API services dependency for dialoglow.

build.gradle
plugins {
    id 'java'
    id 'org.springframework.boot' version '2.3.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation group: 'com.google.apis', name: 'google-api-services-dialogflow', version: 'v2-rev124-1.25.0'
    ...
}

You can check latest version of Google Dialogflow api from Maven Repository

Deserializer configuration

Default Jackson deserializer cannot deserialize webhook response from Google Dialoglow API. For consuming Dialogflow webhook response, we need to use instances of JacksonFactory or GsonFactory provided by google-api-services-dialogflow.

DialogFlowConfig - JacksonFactory & GsonFactory
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DialogFlowConfiguration {

    @Bean
    public JacksonFactory jacksonFactory() {
        return JacksonFactory.getDefaultInstance();
    }
}

If you do not want to use JacksonFactory, then use Gsonfactory which should work equally well.

Creating GsonFactory
@Bean
public GsonFactory gsonFactory() {
    return GsonFactory.getDefaultInstance();
}

Creating a Controller

Next is to create a Controller that will accept incoming requests from Google Dialogflow WebHook API

DialogFlowWebhookController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DialogFlowWebhookController {

    @Autowired
    private final JacksonFactory jacksonFactory;

    @PostMapping(value = "/dialogflow-webhook", produces = {MediaType.APPLICATION_JSON_VALUE})
    public String webhook(@RequestBody String rawData) {
        //Step 1. Parse the request
        GoogleCloudDialogflowV2WebhookRequest request = jacksonFactory
                        .createJsonParser(rawData)
                        .parse(GoogleCloudDialogflowV2WebhookRequest.class);

        //Step 2. Process the request
        //Step 3. Build the response message
        GoogleCloudDialogflowV2IntentMessage msg = new GoogleCloudDialogflowV2IntentMessage();
        GoogleCloudDialogflowV2IntentMessageText text = new GoogleCloudDialogflowV2IntentMessageText();
        text.setText("Welcome to Spring Boot");
        msg.setText(text);

        GoogleCloudDialogflowV2WebhookResponse response = new GoogleCloudDialogflowV2WebhookResponse();
        response.setFulfillmentMessages(asList(msg));
        StringWriter stringWriter = new StringWriter();
        JsonGenerator jsonGenerator = jacksonFactory.createJsonGenerator(stringWriter);
        jsonGenerator.enablePrettyPrint();
        jsonGenerator.serialize(response);
        jsonGenerator.flush();
        return stringWriter.toString();
    }
}

Will add more on this later on.


Top articles in this category:
  1. SendGrid Attachments with Spring Boot
  2. Sendgrid Dynamic Templates with Spring Boot
  3. Feign Client Logging and connection timeout
  4. Elasticsearch with Spring Boot + Spring Data
  5. SendGrid emails in Spring Boot
  6. Spring Boot multipart file upload server
  7. Basic Auth Security in Spring Boot 2

Find more on this topic:

Recommended books for interview preparation:
Buy interview books

Java & Microservices interview refresher for experienced developers.


This website uses cookies to ensure you get the best experience on our website. more info