Search code examples
javascriptjsonnode.jsslackslack-api

How to pass a value from slack channel to `Options Load URL` in Slack App


I have a slack app implemented in node.js where I am dynamically displaying a drop-down menu from an Options Load URL to a channel on the Slack group.

The drop down is getting displayed correctly based on the options JSON that I am returning form the external URL,

but now the problem is that I need to have separate items in the drop-down menu based on what the user has entered on the slack channel.

For example:

If the user says: give me choices for option 1: then the value 1 should be passed to the Options Load URL and the code that I have implemented at that URL will reply with the appropriate JSON based on the input value 1.

Next, when the User says give me choices for option 2: then the value 2 should be passed to the Options Load URL and the code implemented there will reply the options based on the value 2 that it receives.

The code at the Options Load URL is already implemented. The code for extracting the number 1 or 2 from the user message is also implemented.

The values 1 or 2 ... etc. are not constant or fixed. These can by random and the API at Options Load URL will be able to correctly handle these values.

I just need to figure out a way to send these values to the Options Load URL somehow.

Is it possible to do this somehow in Slack?


Solution

  • Use the name property in your request to pass a custom value to the part of your app that handles the options requests from Slack ("Options Load URL"). I use it usually to select which predefined options list to return, but you can also use it to dynamically create a new option list based on the value.

    For reference here is the example Slack request for creating a dynamic menu (from the offical documentation), where you can see the name property under action. In this example it has the value "bugs_list":

    {
        "text": "What's bugging you?",
        "response_type": "in_channel",
        "attachments": [
            {
                "fallback": "Upgrade your Slack client to use messages like these.",
                "color": "3AA3E3",
                "attachment_type": "default",
                "callback_id": "select_remote_1234",
                "actions": [
                    {
                        "name": "bugs_list",
                        "text": "Which random bug do you want to resolve?",
                        "type": "select",
                        "data_source": "external",
                        "min_query_length": 3,
                    }
                ]
            }
        ]
    }
    

    And here is what your Options Load URL will receive. Notice the name parameter.

    {
        "name": "bugs_list",
        "value": "bot",
        "callback_id": "select_remote_1234",
        "team": {
            "id": "T012AB0A1",
            "domain": "pocket-calculator"
        },
        "channel": {
            "id": "C012AB3CD",
            "name": "general"
        },
        "user": {
            "id": "U012A1BCJ",
            "name": "bugcatcher"
        },
        "action_ts": "1481670445.010908",
        "message_ts": "1481670439.000007",
        "attachment_id": "1",
        "token": "verification_token_string"
    }
    

    The name parameter is a common parameter in a HTTP request, so you can put pretty much everything in there, even data structures, as long as they are encoded as strings. Also see my other answer that talks about the limits of passing data in Slack parameters.