claudiajs/claudia-bot-builder

Custom Termination Not Working...

apanzerj opened this issue · 15 comments

Probably missing something but here goes:

I have a bot that makes an api request and returns a result. Sometimes the whole roundtrip takes too long so I'm trying to do custom termination.

var request = require("request")
var botBuilder = require('claudia-bot-builder');
module.exports = botBuilder(function (message, request) {
  console.log("Never gonna give you up...");
  var rick = new Promise((a, b) => { console.log('Never gonna give you up...') }).then((a) => { 'Never gonna drag you around....' });;
  return rick
  response_url = message.originalRequest.response_url;
var rock = new Promise((resolve, reject) => {
  request({
    url: response_url,
    method: "POST",
    json: {
      text: "my delayed response"
    }
  });
}).then((data) => { request.lambdaContext(null, "done"); })
console.log("Or hurt you.");
request.lambdaContext.done(null)
})

In amazon I just see the following in cloudwatch:

START RequestId: 4c0bef2b-4566-11e6-93fe-c176baba64f0 Version: $LATEST 
2016-07-08T23:47:14.642Z    4c0bef2b-4566-11e6-93fe-c176baba64f0    Never gonna give you up... 
2016-07-08T23:47:14.643Z    4c0bef2b-4566-11e6-93fe-c176baba64f0    Never gonna give you up... 
END RequestId: 4c0bef2b-4566-11e6-93fe-c176baba64f0 
REPORT RequestId: 4c0bef2b-4566-11e6-93fe-c176baba64f0  Duration: 4.08 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 27 MB
gojko commented

not sure if I read this code correctly, but you return rick, terminating the function; pretty much everything below that never gets executed.

It says in the docs if you return a never resolving promise it won't terminate

edit: https://github.com/claudiajs/claudia-api-builder/blob/master/docs/api.md#responding-to-requests

edit 2: I'm totally sure I'm just doing it wrong but I'm lost as to where/how.

gojko commented

yes, but basic JS syntax is that functions get terminated after a return. you need to process stuff before returning the promise.

by the way, there's no reason to do custom termination to call a third-party API that takes too long; just increase the lambda timeout.

It's because slack has a 3 second timeout. So you use the response url to re-respond after the fact.

gojko commented

still, you can't return from a function and do other stuff later. that simply won't work because of how javascript executes functions.

for the slack timeout, the workaround at the moment is to split the work into two lambdas, one that will receive the initial slack request and respond to slack immediately, and another that actually processes the work and responds back. you can use SQS or SNS to decouple the two.

we're modifying the bot builder soon to make delayed responses easier to code. once the change is complete, you'll be able just to add a config argument to the bot builder to allow it to return delayed responses to slack.

ahh, is there way to add the extra lambda within the framework of claudia js ? Or do I just have to manually make an extra lambda myself?

(also, thanks for the help :-D )

gojko commented

you can create the extra lambda using claudia.js as another project. check out https://github.com/claudiajs/claudia/blob/master/getting_started.md

gojko commented

We implemented much better support for delayed responses in Bot Builder 1.4.0. here's how you can use it to achieve the trick with async calls easily, with a single lambda: https://github.com/claudiajs/example-projects/tree/master/slack-delayed-response

Nice!

gojko commented

Here's a full tutorial how to run delayed responses with Slack https://claudiajs.com/tutorials/slack-delayed-responses.html

Thanks. I'm going to play with it now.

@gojko playing with it, I get a permissions error in IAM.

I tore down, updated, redeployed. What am I missing?

 { [AccessDeniedException: U
ser: arn:aws:sts::-:assumed-role/pagerduty_bot_for_dev-executor/awslambda_71_20160721182
648621 is not authorized to perform: lambda:InvokeFunction on resource: arn:aws:lambda:us-east-1:72
-:function:pagerduty_bot_for_dev]
  message: 'User: arn:aws:sts::-:assumed-role/pagerduty_bot_for_dev-executor/awslambda_7
1_20160721182648621 is not authorized to perform: lambda:InvokeFunction on resource: arn:aws:lambda
:us-east-1:-:function:pagerduty_bot_for_dev',
  code: 'AccessDeniedException',
  time: Thu Jul 21 2016 18:26:50 GMT+0000 (UTC),
  requestId: 'b0d306da-4f70-11e6-bb01-c30f53add707',
  statusCode: 403,
  retryable: false,
  retryDelay: 24.02851819060743 }

Please use gitter for questions, issue tracker is for issues with the library itself.

For the error: check if your user has lambda:InvokeFunction permission, also check if you invoked claudia create with --allow-recursion option, it's required to allow lambda function to invoke itself.