1

I have programmed several slash commands that show a response in public channels without any problems, but they don't show any response in private channels or direct messages.

As shown below, I am using the in_channel response type. Is there any other response type I can use or a workaround so that it works everywhere?

$data = array(
    "username" => "My_user",
    "channel" => $channel_id,
    "response_type" => "in_channel",
    "text" => $text,
    "mrkdwn" => true,
    "icon_url" => $icon_url

);

$json_string = json_encode($data);
$slack_call = curl_init($slack_webhook_url);
curl_setopt($slack_call, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($slack_call, CURLOPT_POSTFIELDS, $json_string);    
curl_setopt($slack_call, CURLOPT_CRLF, true);
curl_setopt($slack_call, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($slack_call, CURLOPT_HTTPHEADER, array(                                                                          
    "Content-Type: application/json",
    "Content-Length: " . strlen($json_string))
);                                                                                                                   
$result = curl_exec($slack_call);
curl_close($slack_call);

Thanks in advance!

Irina
  • 1,333
  • 3
  • 17
  • 37

2 Answers2

5

I talked to the Slack team, which was extremely helpful, and we figured out what the problem was. I am sharing it here in case anybody else runs into the same problem.

The problem was not about the commands being used in public or private channels. The person who created the webhook set it so that it would work on a private channel (our testing channel), so it would only work in that channel or in any channels that she was a part of (so, all the public channels). As soon as I added her to a private channel, it would work.

The solution was for the creator of the webhook to edit it (not the code, just the webhook) and set it by default to a public channel (any) instead of a private channel. This made it work in every channel, even direct messages.

This way, I was able to use my original code, which also allows me to change the user icon dynamically, instead of sending a message back.

I hope that helps other people as well!

Irina
  • 1,333
  • 3
  • 17
  • 37
2

That does not look like the right approach.

Responding to slash commands

For responding to a slash command you must not send a new message back (e.g. via webhook as in your code example). Instead just respond to the request from Slack with the content of your message.

Example

$message = array (
    'response_type' => 'in_channel',
    'text' => $text
    );

header ('content-type: application/json');
echo json_encode ($message);

that is all you need.

response_type defines if the response can be seen by all members of a channel "in_channel" or only by the issuer of the slash command "ephemeral"

Please see the official documentation for more details and options.

Sending additional messages

You can of course also send a message from your script in response to the slash command. However, if you want to send a message to a private channel please note that the slash command request from Slack will not include the correct channel ID if it is used in a non-public channel. I don't think there is currently any solution or workaround for this.

You can however always send a direct message to the user by using the ID of the user as channel ID.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • Thank you, Erik. This is great but there are two things I would need to fix: It also posts the original command back (which I can probably figure out how to get rid of) but, most importantly, I need to be able to set the icon from within the code. As you can see above, I have "icon_url" => $icon_url, which is an integral part of the slash command. I added that line to your approach, but it is not working. Do you know if this can be done? Thanks again! – Irina Jul 07 '17 at 15:22
  • Happy to help. 1. It will always respond the original command back if you use "in_channel". Only way to turn that off it to use "ephemeral", but then the response will only be visible by the issuer. 2. You can not set an icon in the response message for a slash command. instead you can should set the icon for your corresponding Slack app in app settings. – Erik Kalkoken Jul 07 '17 at 15:28
  • Thanks again, Erik. In that case, I am afraid I will have to go with my original approach, as I need to be able to set the icon dynamically and hide the command, but still post in the channel. My original code works great, but after doing some testing I realized it only works in channels that have at least 5 people. Do you know why this could be, or if there is any way to fix it? It seems like a bug to me. Thanks again! – Irina Jul 07 '17 at 15:54
  • yeah then sending a message is the best option. can't see a reason why it would only work in channel with more than 5 ppl. if that behavior is reproduce able I woudl advise sending a bug report – Erik Kalkoken Jul 07 '17 at 16:20
  • Yes, I can definitely reproduce it. I have tried with just two people, then three, etc. It only starts working when I add a fifth person. Thanks again! – Irina Jul 07 '17 at 19:11