申請 Line messaging api for uptime-kuma 服務監控異常通知
解決問題
先前有撰寫一篇透過uptime-kuma 來監控站台服務是不是正常運作的文章,後來發現他有支援 line message 通知,此篇順便將申請的過程記錄一下。
那麼,如果要在 uptime-kum 使用 line 通知,我們需要申請,line messaging api 的 user id 及金鑰
登入 / 註冊 Line Bot 開發者帳號
建立新的聊天群 > Create a new channel
依據畫面所需的資料輸入 > Channel type > Messaging API
送出
切到 Basic settings tab 頁
捲到頁面最下方,去複製之後要用的 user id
切到 Messaging API Tab 頁
捲到頁面最下方, 點擊 Issue 按鈕
點擊 Issue 後會產生 Line bot 的金鑰,複製其金鑰
回到 Messaging API Tab 上方,用手機掃描 Messaging API Tab 頁提供的 Qrcode ,並將其加為好友
將前面取得的 user id 及 金鑰貼到 uptime-kuma 的通知設定裡,並按下測試,此時推播測試訊息,會傳到你的 Line app 中
Line push 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 傳送給你的聊訊息了