LoginSignup
0
0

More than 5 years have passed since last update.

API.AIへJavaからアクセスする

Last updated at Posted at 2017-08-21

API.AIとは

ボットを作るためのツール(API)です。
flatfisherさんの投稿に詳細は記載されています。
 自然言語処理を容易に実装できるAPI.AIを使ってみる #api.ai

JAVAのライブラリ

JSONのAPIから取得してもよいのですが、Java用のライブラリが用意されているのでせっかくなのでこれを使いましょう。
下記がGitHubのURLです。
 api-ai/apiai-java-client

JAVAからアクセスするには

上記GitHubのパス「apiai-java-client/samples/clients/web-client/src/main/java/ai/api/examples/web/ServiceServletSample.java」にサンプルのサーブレットがあります。
ポイントとなる箇所の内容の補足します。

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
 */
//補足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 {
      //補足2
      AIResponse aiResponse = request(request.getParameter("query"), request.getSession());
      response.setContentType("text/plain");
//補足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);
  }
}
【補足1】

APIのキーの取得元ですがGitHubの「apiai-java-client/docs/images/apiKeys.png」に該当箇所のイメージがあります。

【補足2】

API.AIへのリクエストとなります、AIResponseオブジェクトへ格納されます。なお、requestの引数の2つ目にJavaのセッションを設定していますが、これはAPI.AIの会話のセッションとJavaのセッションを同期化させるためのものとなります。
(Javaのセッションが切れればAPI.AIの会話も切れます)

【補足3】

API.AIからの回答をAIResponseから取得しています。このオブジェクトからはその他、私が試してみたもので以下のようなものもとれます。
 ■パラメータに設定された結果
 「aiResponse.getResult().getStringParameter("パラメータ名")」で取得可能です。
  パラメータについては以下のガイドページを参照ください。
   Actions and Parameters

 ■コンテキストの設定内容
 「aiResponse.getResult().getContexts()」で、設定されているコンテキストのListが取得できます。
  コンテキストについては、以下のExampleページを参照ください。
   Context Examples

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0