protected AgentResponse createResponse(String json) { Gson gson = new Gson(); JsonObject jsonObj = gson.fromJson(json, JsonObject.class); String heading = jsonObj.get("Heading").getAsString(); if (heading.equals("")) { // duckduckgo does not know return new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_KNOW_ANSWER); } String abstractText = jsonObj.get("AbstractText").getAsString(); if (abstractText.equals("")) { // most likely a disambiguation page ResponseStatus status = new ResponseStatus(ResponseStatus.Code.AGENT_DID_NOT_KNOW_ANSWER, "Multiple answers for this question"); return new AgentResponse(status); } return new AgentResponse(abstractText); }
/** * Create a response * * @param response response from an agent */ public Response(AgentResponse response) { this.type = response.getType(); this.text = response.getText(); this.payload = response.getPayload(); this.status = response.getStatus(); }
/** * Is this a valid response? * * @return true if valid */ public boolean isValid() { if (type == null || status == null) { return false; } if (Response.Type.usesPayload(type) && payload == null) { return false; } return !(isSuccess() && text == null); } }
@Override public AgentResponse process(AgentRequest request) { AgentResponse response; String json = gson.toJson(request); HttpRequest httpRequest = new HttpRequest(HttpMethod.POST, requestUrl).setBody(json, ContentType.APPLICATION_JSON); HttpResponse httpResponse = client.execute(httpRequest); if (httpResponse != null) { String text = httpResponse.asString(); try { response = gson.fromJson(text, AgentResponse.class); // the gson deserialization code is very permissive so we verify if (!response.isValid()) { response = new AgentResponse(ResponseStatus.Code.INVALID_RESPONSE_FROM_AGENT); logger.warn("Invalid json for request: " + text); } } catch (JsonSyntaxException e) { response = new AgentResponse(ResponseStatus.Code.INVALID_RESPONSE_FROM_AGENT); logger.warn("Invalid json for request: " + text); } } else { response = new AgentResponse(ResponseStatus.Code.NO_RESPONSE_FROM_AGENT); } return response; }
@Override public AgentResponse process(AgentRequest request) { logger.debug("Handling the request: " + request.getText()); return new AgentResponse(request.getText()); }
@Override public AgentResponse process(AgentRequest request) { logger.debug("Handling the request: " + request.getText()); Date now = new Date(); AgentResponse response; String requestType = getRequestType(request.getText()); switch (requestType) { case DATE: response = new AgentResponse(getDate(now)); break; case DAY: response = new AgentResponse(getDay(now)); break; case TIME: response = new AgentResponse(getTime(now)); break; default: response = new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_UNDERSTAND_REQUEST); break; } return response; }
@Override public AgentResponse process(AgentRequest request) { logger.debug("Handling the request: " + request.getText()); String agentRequest = request.getText(); String searchPhrase = getSearchPhrase(agentRequest); logger.debug("Search phrase: " + searchPhrase); if (searchPhrase == null) { return new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_UNDERSTAND_REQUEST); } String json = getFlickrJson(searchPhrase); logger.debug("Flickr JSON: " + json); if (json == null) { return new AgentResponse(ResponseStatus.Code.AGENT_INTERNAL_ERROR); } String url = getImageUrl(json); logger.debug("Flickr image URL: " + url); byte[] data = getImage(url); if (data == null) { return new AgentResponse(ResponseStatus.Code.AGENT_INTERNAL_ERROR); } String payload = encodeImage(data); return new AgentResponse(Response.Type.IMAGE_EMBED, "Here is your image", payload); }
@Override public AgentResponse process(AgentRequest request) { logger.debug("Handling the request: " + request.getText()); String agentRequest = request.getText(); String movieTitle = findMovieTitle(agentRequest); if (movieTitle.equals("")) { return new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_UNDERSTAND_REQUEST); } String keyword = findKeyWord(agentRequest); if (keyword == null) { return new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_UNDERSTAND_REQUEST); } AgentResponse response = null; String json = requestData(movieTitle); if (json == null) { response = new AgentResponse(new ResponseStatus( ResponseStatus.Code.AGENT_INTERNAL_ERROR, "Something went wrong with the Rotten Tomatoes API request. Please check your API key.")); } else { String responseText = formatResponse(keyword, json); if (responseText != null) { response = new AgentResponse(responseText); } else { response = new AgentResponse(ResponseStatus.Code.AGENT_INTERNAL_ERROR); } } return response; }
@Override public AgentResponse process(AgentRequest request) { logger.debug("Handling the request: " + request.getText()); Expression exp = null; try { exp = new ExpressionBuilder(request.getText()).build(); } catch (IllegalArgumentException e) { logger.debug("CalculatorAgent could not parse " + request.getText()); return new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_UNDERSTAND_REQUEST); } double result = 0; try { result = exp.evaluate(); } catch (IllegalArgumentException e) { logger.debug("CalculatorAgent could not parse " + request.getText()); return new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_UNDERSTAND_REQUEST); } // handle integers differently from floating point String answer; if (result % 1 == 0) { answer = String.valueOf((int)result); } else { answer = String.valueOf(result); } return new AgentResponse(answer); }
return new AgentResponse(new ResponseStatus(ResponseStatus.Code.AGENT_DID_NOT_KNOW_ANSWER, "I am sorry, I don't understand which company you are asking about.")); return new AgentResponse(new ResponseStatus(ResponseStatus.Code.AGENT_DID_NOT_KNOW_ANSWER, ("I am sorry, I can't find the proper stock data for the company " + keywordCompanyName + ".").replace("..", "."))); return new AgentResponse(Response.Type.JSON, ("Here is the longitudinal stock market data from the last " + DAYS_IN_A_MONTH returnObj.toString()); return new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_KNOW_ANSWER);