3

I am trying to write a lambda function that takes information from lex input, and calls a rest api, passing that information as a parameter, returns a string, which i then want to send to lex as a response. It works as expected when run in eclipse, but when i upload the jar to amazon lambda, it is giving no error, but the output string is null.

public class LambdaFunctionHandler implements RequestHandler<Map<String,Object>, Object> {


@Override
public Object handleRequest(Map<String,Object> input, Context context) {

    ValidationHook.LexRequest lexRequest= LexRequestFactory.createLexRequest(input);
    String orgbotcommand = lexRequest.getCommand()+"%20"+lexRequest.getType()+"%20"+lexRequest.getNew_variable();
    lexRequest.setOrgbotcommand(orgbotcommand);

    try {
        URL url = new URL("http://localhost:8080/mindtuit/execute?command="+orgbotcommand);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
            lexRequest.setResponse(output);

        }
        System.out.println(lexRequest.getResponse());

        conn.disconnect();

      } catch (MalformedURLException e) {

        e.printStackTrace();

      } catch (IOException e) {

        e.printStackTrace();

      }



    String content = String.format("command recieved by %s is %s,response is %s ",lexRequest.getBotName(),lexRequest.getOrgbotcommand(),lexRequest.getResponse());

    Message message = new Message("PlainText",content);
    DialogAction dialogAction = new DialogAction("Close", "Fulfilled", message );
    System.out.println(dialogAction);

    return new LexRespond(dialogAction);
}

}

Kanika Agarwal
  • 191
  • 1
  • 4
  • 9
  • 1
    Did you change the URL before uploading it to Lambda? Because it points to localhost in your example.. If you have a local API running, you have to make it public (e.g. by using again Lambda or another service, e.g. EC2). – s.hesse Feb 18 '18 at 15:36
  • @ s.hesse You are right, but that might not be the case here, as it has been handled. @Kanika logs might be helpful, if you can share. – J. Parashar Feb 19 '18 at 06:10
  • @ s.hesse i hosted my api on EC2 and it worked with lex. Thanks a lot :) . One more thing, the computation time is very high(58 sec) for a single call. Is there any other way (except increasing computation resources) to make this more efficient – Kanika Agarwal Feb 19 '18 at 07:47
  • @kanika did you find, why it is taking so much time and your fix was just to change the hostname is it? – Rahul Singh Jul 17 '19 at 04:34
  • @KanikaAgarwal maybe the warmup time is included? – payne Feb 10 '20 at 16:12
  • @KanikaAgarwal was this ever resolved, if so, can you put your answer in – MattG Sep 09 '20 at 00:36

1 Answers1

0

use ngrok and generate a http url for application running port.

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/29953167) – Sangeerththan Balachandran Sep 29 '21 at 21:19