iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 4
0
自我挑戰組

用 Google Apps Script 搭起各服務的橋樑系列 第 4

和 Slack 的初次交流

https://ithelp.ithome.com.tw/upload/images/20171226/20066061dc1nr6mq7j.png

取得 Slack Incoming WebHooks

需要取得 Webhook URL 才能做後續的送值,詳細的步驟可以參考 incoming-webhooks

Incoming WebHooks 頁後點選 Add Configuration 會看到這畫面

https://ithelp.ithome.com.tw/upload/images/20171226/20066061KSy4WAjE6I.png

其中的 Webhook URL 就是我們需要的,格式會像這樣https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX

接著結合上一章學到的 URL Fetch Service 試著丟點資料過去

function SlackIncomingWebHooks() {
  var webHookUrl = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX';
  var params = {
    method: 'POST',
    payload: JSON.stringify({
      channel: "#general",
      username: "熊俠",
      icon_url: "https://ithelp.ithome.com.tw/storage/image/ironman9theditor.png",
      text: "有人發了新文章啦~",
      attachments:[{
        color: "#00a0e9",
        fields: [{
          title: "標題",
          value: "<https://ithelp.ithome.com.tw/articles/10193412/|和 Slack 的初次交流>",
          short: true
        }, {
          title: "作者",
          value: "培根",
          short: true
        }]
      }]
    })
  };
  UrlFetchApp.fetch(webHookUrl, params);
}

如果在 Slack 看到這樣的訊息就代表成功啦~

https://ithelp.ithome.com.tw/upload/images/20171226/20066061veq3Nm2LLG.png


上一篇
URL Fetch Service 能做到什麼?
下一篇
加上觸發器,讓你的應用自己動起來
系列文
用 Google Apps Script 搭起各服務的橋樑7
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言