AgentResponse agentResponse = null; if (this.user != null) { agentResponse = agent.process(new AgentRequest(new Request(request.getText(), this.user, request.getId()))); } else { agentResponse = agent.process(new AgentRequest(request));
@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); }
@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()); 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 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; }