All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ai.api.model.ResponseMessage Maven / Gradle / Ivy

Go to download

API.AI Java SDK allows using voice commands and integration with dialog scenarios defined for a particular agent in API.AI.

The newest version!
package ai.api.model;

/***********************************************************************************************************************
 *
 * API.AI Java SDK - client-side libraries for API.AI
 * =================================================
 *
 * Copyright (C) 2016 by Speaktoit, Inc. (https://www.speaktoit.com) https://www.api.ai
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************/

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.JsonObject;
import com.google.gson.annotations.Expose;

import ai.api.model.GoogleAssistantResponseMessages.ResponseBasicCard;
import ai.api.model.GoogleAssistantResponseMessages.ResponseCarouselCard;
import ai.api.model.GoogleAssistantResponseMessages.ResponseChatBubble;
import ai.api.model.GoogleAssistantResponseMessages.ResponseLinkOutChip;
import ai.api.model.GoogleAssistantResponseMessages.ResponseListCard;
import ai.api.model.GoogleAssistantResponseMessages.ResponseSuggestionChips;

/**
 * Base model class for 
 * response message objects.
 */
public abstract class ResponseMessage {

  @Expose
  private final MessageType type;

  @Expose
  private final Platform platform;

  /**
   * Constructor initializing message type code 
   * @param type Message type. Cannot be null.
   */
  protected ResponseMessage(MessageType type) {
    this(type, null);
  }
  
  /**
   * Constructor initializing message type code and platform
   * @param type Message type. Cannot be null.
   * @param platform Platform type. If null then
   *    default value will be used
   */
  protected ResponseMessage(MessageType type, Platform platform) {
    assert type != null;
    this.type = type;
    this.platform = platform != null ? platform : Platform.DEFAULT;
  }

  /**
   * Holds the message type integer code and related {@link Type}
   */
  public static enum MessageType {
    /** Text response message object */
    SPEECH(0, "message", ResponseSpeech.class),
    /** Card message object */
    CARD(1, "card", ResponseCard.class),
    /** Quick replies message object */
    QUICK_REPLY(2, "quick_reply", ResponseQuickReply.class),
    /** Image message object */
    IMAGE(3, "image", ResponseImage.class),
    /** Custom payload message object */
    PAYLOAD(4, "custom_payload", ResponsePayload.class),
    CHAT_BUBBLE(5, "simple_response", ResponseChatBubble.class),
    BASIC_CARD(6, "basic_card", ResponseBasicCard.class),
    LIST_CARD(7, "list_card", ResponseListCard.class),
    SUGGESTION_CHIPS(8, "suggestion_chips", ResponseSuggestionChips.class),
    CAROUSEL_CARD(9, "carousel_card", ResponseCarouselCard.class),
    LINK_OUT_CHIP(10, "link_out_chip", ResponseLinkOutChip.class);
    
    private final int code;
    private final String name;
    private final Type type;

    private MessageType(int code, String name, Type curClass) {
      assert name != null;
      assert curClass != null;
      this.code = code;
      this.name = name;
      this.type = curClass;
    }

    /**
     * @return Message integer code value
     */
    public int getCode() {
      return this.code;
    }

    /**
     * @return Type name presentation
     */
    public String getName() {
      return name;
    }

    /**
     * @return Related class {@link Type}
     */
    public Type getType() {
      return type;
    }
    

    
    private static Map typeByCode = new HashMap<>();
    private static Map typeByName = new HashMap<>();

    static {
      for (MessageType type : values()) {
        typeByCode.put(type.code, type);
        typeByName.put(type.name.toLowerCase(), type);
      }
    }
    
    public static MessageType fromCode(int code) {
      return typeByCode.get(code);
    }
    
    public static MessageType fromName(String name) {
      return typeByName.get(name != null ? name.toLowerCase() : null);
    }
  }

  public enum Platform {
    DEFAULT(null),
    GOOGLE("google"),
    FACEBOOK("facebook"),
    SLACK("slack"),
    TELEGRAM("telegram"),
    KIK("kik"),
    VIBER("viber"),
    SKYPE("skype"),
    LINE("line");

    private final String name;

    Platform(String name) {
      this.name = name;
    }
    
    public String getName() {
      return name;
    }
    
    private static Map platformByName = new HashMap<>();
    
    static {
      for (Platform platform : values()) {
        String platformName = platform.getName();
        platformByName.put(platformName != null ? platformName.toLowerCase() : null, platform);
      }
    }
    
    public static Platform fromName(String name) {
      return platformByName.get(name != null ? name.toLowerCase() : null);
    }
  }

  /**
   * Text response
   * message object
   */
  public static class ResponseSpeech extends ResponseMessage {

    @Expose
    public List speech;
    
    public ResponseSpeech() {
      super(MessageType.SPEECH);
    }

    /**
     * Get agent's text replies.
     */
    public List getSpeech() {
      return this.speech;
    }

    /**
     * Set agent's text replies.
     */
    public void setSpeech(List speech) {
      this.speech = speech;
    }
    
    /**
     * Set agent's text replies.
     */
    public void setSpeech(String... speech) {
      setSpeech(Arrays.asList(speech));
    }
  }

  /**
   * Card message object
   */
  public static class ResponseCard extends ResponseMessage {

    @Expose
    private String title;

    @Expose
    private String subtitle;

    @Expose
    private String imageUrl;

    @Expose
    private List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy