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'
...
}
Dialoglfow fulfillment with Spring Boot
Upasana | 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.
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
.
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.
@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
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:
- SendGrid Attachments with Spring Boot
- Sendgrid Dynamic Templates with Spring Boot
- Feign Client Logging and connection timeout
- Elasticsearch with Spring Boot + Spring Data
- SendGrid emails in Spring Boot
- Spring Boot multipart file upload server
- Basic Auth Security in Spring Boot 2