Search code examples
phpslackslack-api

Avoiding echo of slash command for in_channel responses


i'm creating a really small slash command for my channel right now. I send the command to a PHP script using HTTPS 200, manipulate it and send it back as JSON payload, basically like this:

<?php
header('Content-Type: application/json');

$text = $_POST['text'];

if ($text === "keyword") $answer = "Hello World";
else $answer = "Nothing in here";

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

echo json_encode($response);

Now I don't want to post the intial slash commend in the channel as already mentioned in the title but only the answer of the server should be visible for everyone in the channel. I can't find a suitable article for this, so I ask myself: Is it even possible?

Anybody have an idea?

Edit: I dont want that slack also post my command as message. So that only the second message is post in the channel: see a screenshot

Update: I have now implemented the suggestion of Erik Kalkoken but I don't get a signal from the curl function. I also successfully checked if my webserver supports the curl functions. My code now looks like this (the code has been reduced to the necessary):

<?php
header('Content-Type: application/json');

$response = array(
  'response_type' => 'in_channel',
  'text' => "> \\M2\Kunden\_Tutorial"
);

echo json_encode(array('text' => 'One moment...'));

$ch = curl_init($_POST['response_url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $response);
$exec = curl_exec($ch);
curl_close($ch);

Working Solution: After taking Erik Kalkoken's advice into account again, it still didn't work. Which doesn't mean that he said something wrong. On the contrary, I forgot something. I found out that my $response array needs to be encoded as json string. Below you can see a working version. Thanks to Erik Kalkoken!

    <?php
    $response = array(
      'response_type' => 'in_channel',
      'text' => '> \\M2\Kunden\_Tutorial'
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $_POST['response_url']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response));
    curl_setopt($ch, CURLOPT_POST, 1);
    $headers = array();
    $headers[] = "Content-Type: application/json";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    curl_close ($ch);

Solution

  • When you use response_type set to in_channel its standard behavior for slash commands to echo your slash command. This usually makes sense, so people in that channel can see that a slash command was issues before they see its result.

    There is no direct way to turn this echo feature off, but there is a simple workaround.

    Instead of replying directly to the Slack request, send your response as new request to the response_url, which you got from Slack in the initial request. Then you only get your response in the channel and the slash command itself will not be echoed.

    Btw. this behavior is also explained in the documentation in the section "Sending delayed responses":

    As with immediate response messages, you can include the response_type field. However, when you use a value of in_channel with this delayed method, the original text that invoked the command is not included.

    I would recommend using curl to send the request to Slack.

    Edit after question has been updated with curl call

    You are still returning a direct response to the Slack request, which is causing Slack to echo the user command. You must not return anything except 200 OK (which your PHP script will do automatically). So please remove this line:

    echo json_encode(array('text' => 'One moment...'));

    I would also advise to include the correct content header in the curl call by adding this line:

    curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);

    The header() php command in the beginning does not affect the curl call and can be removed too btw.