2

I created a slack app using botkit / node.js / express server.

My method:

    controller.on('slash_command', function(bot, message) { SOME_LOGIC } 

listens for all slash commands in every single channel. Now, my bot (slack app) has it's own direct channel. I want my slash command to only listen for slash commands in that particular channel. Is it possible to restrict slash commands liKE this? What would it look like?

I can't seem to find a unique identifier for speaking directly with slack app bot.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
Faye Hayes
  • 263
  • 3
  • 13

1 Answers1

4

Yes, that is possible.

In general slash commands will work in any channel / conversation and there is no option in the Slack API to restrict them to specific conversations. But, you can add the appropriate functionality to your app, so it only reacts to the command coming from the app channel. Here is how:

1. Detecting the right conversation

Every slash command request from Slack includes the channel ID from the conversation it was issues in and the user ID from the corresponding user. (see below for an example slash command). Your app just needs to look for the channel ID corresponding to the app channel and react accordingly.

Here is an example of a slash command (from the official documentation)

token=gIkuvaNzQIHg97ATvDxqgjtO
&team_id=T0001
&team_domain=example
&enterprise_id=E0001
&enterprise_name=Globular%20Construct%20Inc
&channel_id=C2147483705
&channel_name=test
&user_id=U2147483697
&user_name=Steve
&command=/weather
&text=94070
&response_url=https://hooks.slack.com/commands/1234/5678
&trigger_id=13345224609.738474920.8088930838d88f008e0

To get the channel ID corresponding with the current user you can call conversations.list with types set to IM, which will give you all direct message channels with their channel IDs of your app. The one where user equals the user ID from the current slash request is the correct one.

Here is an example response from channels.list for direct messages:

{
    "ok": true,
    "channels": [
        {
            "id": "D0G9QPY56",
            "created": 1449709280,
            "is_im": true,
            "is_org_shared": false,
            "user": "USLACKBOT",
            "is_user_deleted": false,
            "priority": 0
        },
        {
            "id": "D1KL59A72",
            "created": 1466692204,
            "is_im": true,
            "is_org_shared": false,
            "user": "U0G9QF9C6",
            "is_user_deleted": false,
            "priority": 0
        }
}

2. Responding to other conversations

In addition you will need to also respond to slash command request from other conversations or you will get a timeout error on Slack.

For those you can respond with an empty body and HTTP code 200, which will not create any output on Slack.

Alternatively, you could respond with a short message explaining to the user that he should use your slash command in the proper channel. (I would recommend this option since it is more user friendly)

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • thank you for your response! I'm specifically looking for when a user interacts directly with a slack app via direct message. When I use conversations.list it only shows public channels. Is there a way to identify that the user is interacting directly with the application? – Faye Hayes Dec 19 '18 at 05:07
  • I am happy to help you with that question too, but it seams to me to be a completely new topic. Please create a new question for it. Ty. – Erik Kalkoken Dec 19 '18 at 08:13
  • Hi Erik, thank you! How to detect whether the user is communicating directly with the slack bot application was my original question. – Faye Hayes Dec 19 '18 at 20:06
  • As far as I can see this question is only asking how how to restrict a slash command, which I think I answered comprehensively. If you also want to know about direct messages please ask another question. – Erik Kalkoken Dec 19 '18 at 21:08