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)