A tool (API) for creating bots. More details can be found in flatfisher's post. Try using API.AI that can easily implement natural language processing # api.ai
You can get it from the JSON API, but since a library for Java is prepared, let's use this. Below is the URL of GitHub. api-ai/apiai-java-client
There is a sample Servlet in the above GitHub path "apiai-java-client / samples / clients / web-client / src / main / java / ai / api / examples / web / ServiceServletSample.java". It supplements the contents of the key points.
ServiceServletSample.java
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ai.api.examples.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ai.api.AIServiceException;
import ai.api.model.AIResponse;
import ai.api.web.AIServiceServlet;
/**
* Servlet implementation class AIServiceServlet
*/
//Supplement 1
@WebServlet(urlPatterns = {"/ai"}, initParams = {
@WebInitParam(name = ServiceServletSample.PARAM_API_AI_KEY,
value = "1bea0a262c924f43bf87a88e4a69cd94")})
public class ServiceServletSample extends AIServiceServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//Supplement 2
AIResponse aiResponse = request(request.getParameter("query"), request.getSession());
response.setContentType("text/plain");
//Supplement 3
response.getWriter().append(aiResponse.getResult().getFulfillment().getSpeech());
} catch (AIServiceException e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Although it is the source of the API key, there is an image of the corresponding part in "apiai-java-client/docs/images/apiKeys.png " on GitHub.
It will be a request to API.AI and will be stored in the AIResponse object. In addition, the Java session is set in the second argument of request, but this is for synchronizing the conversation session of API.AI and the Java session. (If the Java session expires, the API.AI conversation will also expire)
I'm getting the answer from API.AI from AIResponse. From this object, you can also get the following, which I have tried. ■ Result set in the parameter It can be obtained with "aiResponse.getResult (). GetStringParameter (" parameter name ")". Please refer to the following guide page for parameters. Actions and Parameters
■ Context settings You can get the List of the set contexts with "aiResponse.getResult (). GetContexts ()". See the Example page below for context. Context Examples
Recommended Posts