slackapi / node-slack-sdk

Slack Developer Kit for Node.js
https://tools.slack.dev/node-slack-sdk/
MIT License
3.28k stars 661 forks source link

Using interactive-messages within lambda #896

Closed dannyshaw closed 4 years ago

dannyshaw commented 5 years ago

Description

I'm writing a serverless slack application and have been looking at using interactive-messages. It seems quite tightly coupled with running a server.

dispatch() is marked as an internal method, however it seems like there should be a way for a serverless setup to manually pass a payload to the dispatcher.

Is this use case documented anywhere?

What type of issue is this? (place an x in one of the [ ])

Requirements (place an x in each of the [ ])


Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Packages:

Select all that apply:

seratch commented 5 years ago

Unfortunately, there is no documentation for the use case at the moment.

If your Lambda functions directly use the dispatch method, the functions must remember to do request signature verification like this before dispatching requests.

Considering the circumstances, creating an Express instance and converting it to a Lambda handler (we can use this npm modle by AWS or other options) may be simpler. Also, it makes testing the function on a local machine pretty easy.

aoberoi commented 4 years ago

The @slack/interactive-messages package exposes a standard node request listener.

You simply called the .requestListener() method on the adapter. You'll find an example in the documentation under the section for "Using an existing HTTP server".

I believe that for most serverless environments, you can implement the function the environment expects by calling the function you get from the method. This is essentially how the popular aws-serverless-express package works. In fact, there's likely some way to use that package to wrap the request listener returned by the adapter in this package, to make it compatible with AWS Lambda. i'm not an expert on this, but it would be great if you shared what you found!

dannyshaw commented 4 years ago

Thanks all,

I managed to get around limitations using the requestListener, but I had to essentially write a mock Response object that @slack/interactive-mesages would call on as it is coupled to an http response object in the sendResponse() method

Here's the mock adaptor, I just instantiate it with the callback i'd like called when .end() is hit and the data being set by the sendResponse() is passed to that instead

// emulate a stripped down http response adapter for slack
class ResponseAdapter {
  constructor(callback) {
    this.callback = callback;
    this.headers = {};
    this._statusCode = 200;
  }

  set statusCode(val) {
    this._statusCode = val;
  }

  setHeader(key, val) {
    this.headers[key] = val;
  }
  end(content) {
    const data = {
      status: this._statusCode,
      json: content,
      headers: this.headers,
    };
    this.callback(data);
  }
}

and used something like this:

const handler = (req) {
  const interactions = createMessageAdapter(process.env.SIGNING_SECRET);  
  const listen = interactions.requestListener();

  // setup handlers
  interactions.action(
    /*...*/
  );

  const customResponseCreator = (status, json, headers) => {
    // create and process the response I actually need to send
  };

  const res = new ResponseAdapter(customResponseCreator);

  listen(req, res);
aoberoi commented 4 years ago

Whoa, that’s pretty neat. Thanks for sharing!

kyle-johnson commented 3 years ago

For others searching, you can directly use https://github.com/vendia/serverless-express if you include a middleware to populate rawBody:

const app = express();
app.use((req, res, next) => {
  // serverless-express populates `req.body` which makes interactive-messages error out unless
  // `req.rawBody` is populated.
  // As long as API Gateway is configured as a simple proxy to Lambda, the rawBody will be untouched
  // and interactive-messages will successfully verify signatures.
  if (req.body) {
    req.rawBody = req.body;
  }
  next();
});
app.use(createMessageAdapter(slackSigningSecret).requestListener());

// this app can now be used with serverless-express
export { app };
dsuresh-ap commented 3 years ago

Not sure if people are still having this issue but we had it with Azure functions. Turns out we needed to pass the content-type as application/json in the response. Hopefully that helps someone else.