'Respond to user interaction - How to update message?

I have created a message froma slash-command

Map<String, Object> data = new HashMap<>();
data.put("channel", command.getChannelId());
data.put("blocks", blocks);

which is just just some lines of text where each line has a subscribe button.

Now, the user can already click the button and the subscription gets stored on my back end but I can't figure out how to respond to the click.

What I want to do is to change the text of the button from "Subscribe" to "Unsubscribe" (and vice versa).

Since the documentation is pretty hard to get by, I couldn't find the page with an example on how to do that (the docs).

My guess was to just send back the same message again, but this time with the modification I am seeking:

    // ..

    String text;
    if (isSubscribed) {
        text = "Unsubscribe";
    } else {
        text = "Subscribe";
    }

    PlainTextObject plainTextObject = buttonElement.getText();
    plainTextObject.setText(text);
    buttonElement.setText(plainTextObject);

    // ..

List<LayoutBlock> blocks = message.getBlocks();

Map<String, Object> data = new HashMap<>();
data.put("channel", channel.getId());
data.put("blocks", blocks);

But for that I am just getting a HTTP 500 back. So.. what is the righ way to do this?

This is he JSON I am sending as a response (copy of the original message):

{
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "plain_text",
        "text": "Gasthof",
        "emoji": true
      },
      "blockId": "Ohsb8",
      "accessory": {
        "type": "button",
        "text": {
          "type": "plain_text",
          "text": "Subscribe",
          "emoji": true
        },
        "actionId": "LV\u003dBj",
        "value": "1"
      }
    }
  ],
  "channel": "CLD12342",
  "replace_original": "true"
}

What I get is

HTTP 404 Not found

but I am definitely using the provided responseUrl?!


Fyi I am using the Java library jslack (https://github.com/seratch/jslack) .



Solution 1:[1]

You have to send the response on the 'response_url' that is included in the actions response from Slack.

If your app received an interaction payload after an interactive component was used inside of a message, you can use response_url to update that source message.

Include an attribute replace_original and set it to true:

POST https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
Content-type: application/json
{
    "replace_original": "true",
    "text": "Thanks for your request, we'll process it and get back to you."
}

Feel free to include blocks in your response_url update.

Non-ephemeral messages can also be updated using chat.update.

A message's type cannot be changed from ephemeral to in_channel. Once a message is issued, it retains its visibility quality for life.

You cannot use replace_original to modify the user-posted message that invokes a slash command.

Read more about it here: Slack Documentation: Updating message response

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prakul