Reputation: 10466
I'm working with the slack slash commands API, and it works swimmingly with my bot (https://github.com/jesseditson/slashbot) so far, except for one thing:
In other slash integrations (for instance giphy), when a user types a slash command, the command is output to the public chat, then the response is posted:
(source: pxfx.io)
However when I use a custom slash command, the original command is not output at all:
(source: pxfx.io)
I'm currently using the Incoming Webhooks API to post messages back to the channel, which works OK, but the responses are disembodied and lacking context without the original request.
/command
This appears to be possible via whatever giphy uses to integrate, which leaves me with some questions:
Is giphy using a private API, or have I missed the correct API to emulate this behavior?
Is there a setting I missed to allow this?
I'm using node.js, but I'm more interested in if this is possible at all, language aside.
As a side note, I realize I could use the Bot API or Real Time Messaging API to achieve something similar, but without the slash - however, I really like the documentation options and autocomplete that comes with the slash commands, so that's what I'm after with this question.
Upvotes: 8
Views: 4118
Reputation: 31
I believe it has to do with the difference between 'immediate response' and 'delayed response'.
When I send my messages through cURL, it doesn't show the slash command 'in channel'. I found that if I merely included header('content-type header: application/json'), and echo'd my response with a json payload including 'in_channel', the slash command showed up. No API required!
Example:
$reply = "Whatever you want";
$data = array(
"response_type"=>"in_channel",
"text"=>$repy,
);
header('Content-Type: application/json');
echo json_encode($data);
Hope this helps, this is my first post, so be gentle if I broke any unspoken rules of stackoverflow.
Upvotes: 3
Reputation: 5264
I was having this problem recently and even came across this question. The answer is spread across two places in the slash command documentation. Under the '"In Channel" vs "Ephemeral" responses' heading:
By default, the response messages sent to commands will only be visible to the user that issued the command (we call these "ephemeral" messages). However, if you would like the response to be visible to all members of the channel in which the user typed the command, you can add a response_type of
in_channel
to the JSON response, like this.
Then later, when talking about delayed responses (which a request from a node server will be):
The only user-facing difference between immediate responses and delayed responses is that "in channel" delayed responses will not include the initial command sent by the user. To echo the command back to the channel, you'll still need to provide a response to Slack's original visit to your invocation URL.
So at the end of your initial request to the server the bot is on, you'll want to send a response looking something like
response.send({"response_type": "in_channel"});
which will echo back /command
. Shortly after, when the response from the image fetching request comes back, posting to the response_url
will send the payload back to slack and it will be printed under the echoed out /command
.
Upvotes: 4
Reputation: 41
From Slack's /Command API Docs:
In Channel" vs "Ephemeral" responses
By default, the response messages sent to commands will only be visible to the user that issued the command (we call these "ephemeral" messages). However, if you would like the response to be visible to all members of the channel in which the user typed the command, you can add a
response_type
ofin_channel
to the JSON response, like this:
{
"response_type": "in_channel",
"text": "It's 80 degrees right now.",
"attachments": [
{
"text":"Partly cloudy today and tomorrow"
}
]
}
I think you need to set response_type
to "in_channel"
to allow other users to see the response.
Upvotes: 4
Reputation: 34
Unfortunately Slack doesn't offer an option to echo the /command
to the channel at the moment, /giphy
is a unique in-house integration.
The only option for now is to create a Slack API app and have your users individually auth. Following the use of /command
post the original /command message
back to the channel with chat.postMessage
, then post your Incoming webhook message.
Upvotes: 1