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

北城余情 提交于 2019-11-26 11:39:21

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/42793220/how-to-replace-the-buttons-attachment-only-w-slack-interactive-buttons-respon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!