Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace the buttons (attachment) only w/ Slack interactive buttons response

Tags:

slack-api

I've managed to create a simple interactive button Slack app using a Google Apps Script (GAS).

I know how to replace the original message w/ the response, but I would like to replace only the buttons, as demonstrated (but not clearly explained) multiple places in the Slack Interactive Button documentation:
https://api.slack.com/docs/message-buttons#crafting_your_message

I'd like to do what's demonstrated here: https://a.slack-edge.com/dcb1/img/api/message_guidelines/Example_6.gif

Is this an update of the original message, a replacement of the original message with identical text but different attachment, ...?

My current interactive buttons message code looks like this:

function sendMsgWithButton() {

// slack channel url (where to send the message)
var slackUrl = "https://hooks.slack.com/services/...";

// message text  
var messageData = {
"text": "Here's your interactive buttons message.",
"attachments": [
    {
        "text": "Can you click the button?",
        "fallback": "Sorry, no support for buttons.",
        "callback_id": "ptNotificationButtonResponse",
        "color": "#3AA3E3",
        "attachment_type": "default",
        "actions": [
            {
                "name": "userResponse",
                "text": "OK",
                "style": "primary",
                "type": "button",
                "value": "ok"
            }
                   ]
    }
                ]
}

// format for Slack
var options = {
   'method' : 'post',
   'contentType': 'application/json',
   // Convert the JavaScript object to a JSON string.
   'payload' : JSON.stringify(messageData)
 };    

// post to Slack
UrlFetchApp.fetch(slackUrl, options);
}

My current action URL code right now looks like this:

function doPost() {

var replyMessage = {"replace_original": true,
                    "response_type": "in_channel",
                    "text": "I see you clicked the button."
                   };

 return ContentService.createTextOutput(JSON.stringify(replyMessage)).setMimeType(ContentService.MimeType.JSON);     
}

Instead of replacing the entire original message, I'd like to replace just the buttons with something like a checkbox and confirmation message as demonstrated in the gif above.

Thanks!

like image 737
Matt Avatar asked Mar 14 '17 17:03

Matt


People also ask

How do I disable a button in Slack?

Clicking on a button will always fire a request to your Slack app. If you want to remove the button after it was clicked you need to update your original message with a new one that reflects the changed state (e.g. button removed).

What is an interactive message?

Interactive messages are much like other messages, only they contain buttons, a variety of menus types, or they have some custom actions available. Rather than remaining mostly static, interactive messages evolve over time. Message buttons and menus may travel almost anywhere a message goes.


1 Answers

You can only replace the complete message, not just a part.

There are two options to update the original message:

  1. Respond to the Slack request with {"replace_original": true}

  2. Use chat.update

If your original message was not of type ephemeral you will get a copy of the original message as part of the payload from Slack in the original_message property, which can be helpful to update the exchange the original message.

See this page in the Slack documentation as reference.

like image 190
Erik Kalkoken Avatar answered Sep 29 '22 08:09

Erik Kalkoken