Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...