0%

Line Notify

Line Notify

最近接觸了幾個線上服務有提供Line Notify的機器人通知,目前為止覺得很方便,能有效的掌握相關訊息,所以查了一些資料並且做了一些初步的研究。
[TOC]

服務方流程

建立一個可外部存取的網站 ( 目前透過 ngrok )

Ngrok 安裝方法:

  1. 官網下載解壓縮包

解壓縮指令:

1
unzip /路徑/ngrok.zip
  1. 開啟port

終端機指令:

1
2
sudo apachectl start
ngrok http 80 (80為範例,可自訂)
  1. 啟動網頁

終端機指令:

1
ngrok http 80		(80為範例,可自訂)
Ngrok管理介面網址:

http://127.0.0.1:4040

Ngrok 外部連接網址:

http://xxxxxxxx.ngrok.io
https://xxxxxxxx.ngrok.io

進入 LINE Notify 網站並登入 LINE 帳號建立服務

  • Callback URL 為可外部連接網址
  • 取得登錄服務的 Client ID 與 Client Secret 資訊

授權通知方流程

登入 LINE Notify 訂閱通知介面

  1. 登入網址: https://notify-bot.line.me

  2. 建立通知服務

  1. 提供使用者網址:
    https://notify-bot.line.me/oauth/authorize
    method: GET
    1
    2
    3
    4
    5
    6
    scope: notify(固定數值)
    response_type: code(固定數值)
    response_mode: form_post(固定數值)
    redirect_uri: 外部連結網址
    client_id: 用戶帳號的clientId
    state: 自定義一組亂碼

    使用者點擊後

    選擇要接收通知的聊天室:

可以透過 http://127.0.0.1:4040/ 得知 HTTP POST 傳入的內容取得 code 欄位

取得存取權杖(Access Token)

路徑 https://notify-bot.line.me/oauth/token
method: POST

1
2
3
4
5
6
7
{
"grant_type":"authorization_code", (固定數值)
"redirect_uri":"可外部連接的網址",
"client_id":"xxxxxxx", (登錄服務的 Client ID 資訊)
"client_secret":"xxxx", (登錄服務的 Client Secret 資訊)
"code":"xxxxxx" (上個步驟取得的授權碼)
}

成功

1
2
3
4
5
{
"status": 200,
"message": "access_token is issued",
"access_token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

發送 LINE Notify 通知訊息

任何通知訊息都需要有 Access Token 才能發出。

路徑

https://notify-api.line.me/api/notify
method: POST

標頭

Authorization: Bearer xxxxxxx (xxxxxxx為access token)
Content-Type: application/x-www-form-urlencoded 或 multipart/form-data

Body

1
2
3
{
"message":"通知內容" (最長1000字元)
}

回傳成功

1
2
3
4
{
"status": 200,
"message": "ok"
}

相關資源

Line API Document

tags: Line 工具