'use strict';

const rp = require('minimal-request-promise');
const qs = require('querystring');
const slackReply = require('./reply');
const slackParse = require('./parse');

module.exports = function slackSetup(api, bot, logError) {
  api.post('/slack/slash-command', request => {
    if (request.post.token === request.env.slackToken)
      return bot(slackParse(request.post))
        .then(slackReply)
        .catch(logError);
    else
      return slackReply('unmatched token' + ' ' + request.post.token + ' ' + request.env.slackToken);
  });

  api.get('/slack/landing', request => {
    return rp({
      method: 'POST',
      hostname: 'slack.com',
      port: 443,
      path: '/api/oauth.access',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: qs.encode({
        client_id: request.env.slackClientId,
        client_secret: request.env.slackClientSecret,
        code: request.queryString.code,
        redirect_uri: request.env.slackRedirectUrl
      })
    })
      .then(() => `<p>Thanks for installing the app.</p>`);

  }, {
    success: { contentType: 'text/html' }
  });
};

@@ -8,16 +8,50 @@
 rict';%0A%0A
+const prompt = require('prompt');%0A
 const rp
@@ -1169,11 +1169,1091 @@
 %7D%0A  %7D);%0A
+  %0A  api.addPostDeployStep('slackSlashCommand', (options, lambdaDetails, utils) =%3E %7B%0A    return utils.Promise.resolve().then(() =%3E %7B%0A      if (options%5B'configure-slack-slash-command'%5D) %7B%0A        console.log(%60Your Slack redirect URL is $%7BlambdaDetails.apiUrl%7D/slack/landing%60);%0A%0A        utils.Promise.promisifyAll(prompt);%0A%0A        prompt.start();%0A        return prompt.getAsync(%5B'Slack Client ID', 'Slack Client Secret', 'Slack token'%5D)%0A          .then(results =%3E %7B%0A            const deployment = %7B%0A              restApiId: lambdaDetails.apiId,%0A              stageName: lambdaDetails.alias,%0A              variables: %7B%0A                slackClientId: results%5B'Slack Client ID'%5D,%0A                slackClientSecret: results%5B'Slack Client Secret'%5D,%0A                slackToken: results%5B'Slack token'%5D,%0A                slackRedirectUrl: %60$%7BlambdaDetails.apiUrl%7D/slack/landing%60%0A              %7D%0A            %7D;%0A%0A            return utils.apiGatewayPromise.createDeploymentPromise(deployment);%0A          %7D);%0A      %7D%0A    %7D)%0A    .then(() =%3E %60$%7BlambdaDetails.apiUrl%7D/slack/slash-command%60);%0A  %7D);%0A
 %7D;%0A

Add interactive prompt for Slack slash commands