
先前有撰寫一篇透過uptime-kuma 來監控站台服務是不是正常運作的文章,後來發現他有支援 line message 通知,此篇順便將申請的過程記錄一下。


 送出








最後,我們可以透過閱讀 uptime-kuma 的程式碼來學習,各種社群推播訊息的功能串接,從程式碼中了解,只要將你想傳遞的訊息傳遞給 LINE 的 API,就會幫你推送消息出去了, 但因 Line 有限制 CORS ,若直接從後端發送可以避免 cors 問題,或你可以開啟瀏覽器到 Line api 的網站 ,並打開主控台輸入以下指令
fetch("https://api.line.me/v2/bot/message/push", {
    body: JSON.stringify( {
                    "to": 'your line bot id', // linebot id
                    "messages": [
                        {
                            "type": "text",
                            "text": "Test Successful!"
                        }
                    ]
                }), // must match 'Content-Type' header
    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
    credentials: 'same-origin', // include, same-origin, *omit
    headers: {      
      'content-type': 'application/json',
      'Authorization':'Bearer api token' // api token
    },
    method: 'POST', // *GET, POST, PUT, DELETE, etc.           
  })
  .then(response => response.json()) // 輸出成 json
此時你的 line app 就收得到 line bot 傳送給你的聊訊息了