Access API.AI from Java

What is API.AI?

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

JAVA library

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

To access from JAVA

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);
  }
}
[Supplement 1]

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.

[Supplement 2]

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)

[Supplement 3]

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

Access API.AI from Java
Access Teradata from a Java application
Access modifier [Java]
Access protected fields from grandchildren (Java / PHP)
Access Forec.com from Java using Axis2 Enterprise WSDL
Call Java from JRuby
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Eval Java source from Java
From Java to Ruby !!
About Java access modifiers
Java starting from beginner, override
Creating ElasticSearch index from Java
New features from Java7 to Java8
Connect from Java to PostgreSQL
Java, instance starting from beginner
Java starting from beginner, inheritance
Java life starting from scratch
Using Docker from Java Gradle
From Ineffective Java to Effective Java
JavaScript as seen from Java
Execute non-Java instructions from Java
[JDBC] I tried to access the SQLite3 database from Java.
Access the network interface in Java
Call Kotlin's sealed class from Java
Java, abstract classes starting from beginners
Install the memcached plugin on MySQL and access it from Java
Deep Learning Java from scratch 6.4 Regularization
Get country from IP address (Java)
Run node.js from android java (processing)
Run a batch file from Java
[Java] Remove whitespace from character strings
Akka hands-on preparation procedure from Java
Access the in-memory data grid Apache Ignite from a Java client
Use Chrome Headless from Selenium / Java
Java to be involved from today
Java
From Java to VB.NET-Writing Contrast Memo-
Java overload constructor starting from beginner
Work with Google Sheets from Java
Java, interface to start from beginner
JNA (Java Native Access) pattern collection
Java
The road from JavaScript to Java
Reintroducing Java 8 available from Android Studio 2.4
Call TensorFlow Java API from Scala
Easy database access with Java Sql2o
[Java] Conversion from array to List
Sample code using Minio from Java
Access MySQL on a Docker container from a local (host OS) Java program
Note: Differences from Java as seen from C #
Study Deep Learning from scratch in Java.
[Java Silver] Summary of access modifier points
Java starting from beginner, variables and types
Relationship between kotlin and java access modifiers
Data processing using stream API from Java 8
Call Java library from C with JNI
Call GitHub API from Java Socket API part2
API integration from Java with Jersey Client
Use PostgreSQL data type (jsonb) from Java
Call Java method from JavaScript executed in Java