2017-03-14 26 views
8

我已經設法使用Google Apps腳本(GAS)創建簡單的交互式按鈕Slack應用程序。如何僅替換按鈕(附件)w/Slack交互式按鈕響應

我知道如何更換原來的消息瓦特/響應,但我想只更換按鈕,這表現(但沒有明確解釋的)多個地方的時差交互式按鈕文檔中:
https://api.slack.com/docs/message-buttons#crafting_your_message

我想要做什麼這裏展示'S: https://a.slack-edge.com/dcb1/img/api/message_guidelines/Example_6.gif

這是原始消息,具有相同的文字,但不同的附件替換原來的消息,...的更新?

我目前的交互式按鈕消息的代碼如下所示:

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); 
} 

我當前的操作URL代碼現在看起來是這樣的:

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);  
} 

而不是更換整個原始消息的,我會喜歡用上面gif中顯示的複選框和確認信息來代替按鈕。

謝謝!

+0

這應該是鬆懈的功能,用一些有意義的東西代替按鈕 – deepdive

回答

4

您只能替換完整的消息,而不只是一個部分。

有兩種選擇更新原有消息:如果你的原始消息是類型不

  1. 迴應滯緩要求與{"replace_original": true}

  2. 使用chat.update

ephemeral您將獲得原始消息的副本,作爲Slack在中的有效載荷的一部分屬性,這可以有助於更新交換原始消息。

請參閱this page在Slack文檔中作爲參考。

+0

感謝您的幫助 - 我們將帶有選項1. – Matt

+0

@Matt你可以分享你的工作'doPost'代碼嗎?當你發佈這個問題時,我仍然遇到同樣的問題 – baskInEminence

+0

@baskInEminence - 我只是最終替換了所有測試,然後我從未實現它,所以我沒有任何代碼可以共享。 – Matt