'use strict';

/*
This function handles a Slack slash command and echoes the details back to the user.

Follow these steps to configure the slash command in Slack:

  1. Navigate to https://<your-team-domain>.slack.com/services/new

  2. Search for and select "Slash Commands".

  3. Enter a name for your command and click "Add Slash Command Integration".

  4. Copy the token string from the integration settings and use it in the next section.

  5. After you complete this blueprint, enter the provided API endpoint URL in the URL field.


Follow these steps to encrypt your Slack token for use in this function:

  1. Create a KMS key - http://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html.

  2. Encrypt the token using the AWS CLI.
     $ aws kms encrypt --key-id alias/<KMS key name> --plaintext "<COMMAND_TOKEN>"

  3. Copy the base-64 encoded, encrypted key (CiphertextBlob) to the kmsEncyptedToken variable.


Follow these steps to complete the configuration of your command API endpoint

  1. When completing the blueprint configuration select "Open" for security
     on the "Configure triggers" page.

  2. Enter a name for your execution role in the "Role name" field.
     Your function's execution role needs kms:Decrypt permissions. We have
     pre-selected the "KMS decryption permissions" policy template that will
     automatically add these permissions.

  3. Update the URL for your Slack slash command with the invocation URL for the
     created API resource in the prod stage.
*/

const AWS = require('aws-sdk');
const qs = require('querystring');

const token = '===TOKEN===';

function processEvent(event, callback) {
    const params = qs.parse(event.body);
    const requestToken = params.token;
    if (requestToken !== token) {
        console.error(`Request token (${requestToken}) does not match expected`);
        return callback('Invalid request token');
    }

    const user = params.user_name;
    const command = params.command;
    const channel = params.channel_name;
    const commandText = params.text;

    callback(null, `${user} invoked ${command} in ${channel} with the following text: ${commandText}`);
}


exports.handler = (event, context, callback) => {
    const done = (err, res) => callback(null, {
        statusCode: err ? '400' : '200',
        body: err ? (err.message || err) : JSON.stringify(res),
        headers: {
            'Content-Type': 'application/json',
        },
    });

    if (token) {
        processEvent(event, done);
        return;
    }

    done('Token has not been set.');
};


@@ -12,1519 +12,8 @@
 ';%0A%0A
-/*%0AThis function handles a Slack slash command and echoes the details back to the user.%0A%0AFollow these steps to configure the slash command in Slack:%0A%0A  1. Navigate to https://%3Cyour-team-domain%3E.slack.com/services/new%0A%0A  2. Search for and select %22Slash Commands%22.%0A%0A  3. Enter a name for your command and click %22Add Slash Command Integration%22.%0A%0A  4. Copy the token string from the integration settings and use it in the next section.%0A%0A  5. After you complete this blueprint, enter the provided API endpoint URL in the URL field.%0A%0A%0AFollow these steps to encrypt your Slack token for use in this function:%0A%0A  1. Create a KMS key - http://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html.%0A%0A  2. Encrypt the token using the AWS CLI.%0A     $ aws kms encrypt --key-id alias/%3CKMS key name%3E --plaintext %22%3CCOMMAND_TOKEN%3E%22%0A%0A  3. Copy the base-64 encoded, encrypted key (CiphertextBlob) to the kmsEncyptedToken variable.%0A%0A%0AFollow these steps to complete the configuration of your command API endpoint%0A%0A  1. When completing the blueprint configuration select %22Open%22 for security%0A     on the %22Configure triggers%22 page.%0A%0A  2. Enter a name for your execution role in the %22Role name%22 field.%0A     Your function's execution role needs kms:Decrypt permissions. We have%0A     pre-selected the %22KMS decryption permissions%22 policy template that will%0A     automatically add these permissions.%0A%0A  3. Update the URL for your Slack slash command with the invocation URL for the%0A     created API resource in the prod stage.%0A*/%0A%0A
 cons

Remove aws guide