Search code examples
c#slackslack-api

How to delete ephemeral Slack message


When a user uses a slash command, I post an ephemeral message to just that user. When they click a button I want that ephemeral message to either delete or update. Right now I'm just trying to get the delete part working.

In the Slack API documentation it says to send a HTTP POST to the response_url. The only response_url I get when a user clicks a button is something like this: https://hooks.slack.com/actions/XXXXXXXXX/YYYYYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ

When I send a POST to that url, I get an error(I'm not sure what the error is, I just know that my code fails on a try/catch).

The JSON I'm sending to that URL looks like this:

        {
            "response_type" = "ephemeral",
            "replace_original" = "true",
            "delete_original" = "true",
            "text" = ""
        };

I see in the Slack API documentation it says I should be sending that JSON to a URL that begins with https://hooks.slack.com/services/ however I'm not receiving any response_url that has the /services/.

Here is the C# code I am using to send the response:

       var replaceMsg = new NameValueCollection();
       replaceMsg["delete_original"] = "true";
       replaceMsg["replace_original"] = "true";
       replaceMsg["response_type"] = "ephemeral";
       replaceMsg["text"] = "";                    
       var responseReplace = client.UploadValues(button.response_url, "POST", replaceMsg);

Edit: It looks like I'm getting a 404 error when I try to send that

Exception: System.Net.WebException: The remote server returned an error: (404) Not Found.

However, when I paste the exact URL into my browser I don't get a 404, I see a JSON.

Can anyone guide me in the right direction? Thanks.


Solution

  • I did something very similar not too long ago and was able to get it working, though it was tricky.

    This post helped me big time: How to remove Ephemeral Messages

    I would do two things:

    1) make sure you know what that POST response failure is as it may contain useful information.

    2) It looks like you're sending in the string "true" instead of the boolean values, I'm guessing you probably want to send the boolean values instead:

    {
       "response_type" = "ephemeral",
       "replace_original" = true,
       "delete_original" = true,
       "text" = ""
    };