用Python連動LINE Notify進行即時通知
LINE Notify
是LINE推出的一種通知服務,用戶可以透過它發送即時訊息,內容包括文字、圖片甚至貼圖。這種功能在許多情況下都非常實用,例如提醒自己重要的日程、或者與程式碼連動,當某項任務完成或出現問題時發送通知。
登入LINE Notify
到官方LINE Notify網頁: https://notify-bot.line.me/zh_TW/,並點擊登入
。

個人頁面設定
在個人頁面
,點擊發行權杖
。

發行權杖
權杖名稱
可以任意自訂。要發送訊息的聊天室,選擇1對1通知
或群組通知
都可以,之後點擊發行

記得把這串權杖token複製起來,不要外流!

回到剛才的頁面,發現此時已經順利連動成功了!

此時找到LINE Notify的對話視窗,會顯示連動設定完成。
接著邀請LINE Notify
至剛剛連動的群組中即可。

到這邊已經完成所有Line上的設定了。
用Python進行LINE Notify的連動
接下來,我們將使用Python進行連動,並進行實際的傳送訊息測試。
安裝requests模組
首先,我們需要安裝Python的requests模組。
使用Python傳送訊息
接下來我們用執行以下簡單的python程式碼範例測試看看。
token
請填入剛剛你複製的長長那串!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import requests
def line_notify(msg):
token = 'sCq7aGBxxxxxxxxxxxxxxxxxxxxxxx' # 填入你的token
url = 'https://notify-api.line.me/api/notify'
headers = {
'Authorization': 'Bearer ' + token
}
data = {
'message': msg
}
requests.post(url, headers=headers, data=data)
line_notify('這是一則測試通知')
|
執行以上程式碼,看看發生什麼事!

成功了,太興奮了!
LINE Notify進階使用方法
傳送貼圖
其實LINE Notify
不只能傳送文字訊息
,還能傳送貼圖
哦!
只要在data
中補上stickerPackageId
與stickerId
即可。
貼圖ID可以在 這裡 找到。
直接上程式碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import requests
def line_notify(msg):
token = 'sCq7aGBxxxxxxxxxxxxxxxxxxxxxxx' # 填入你的token
url = 'https://notify-api.line.me/api/notify'
headers = {
'Authorization': 'Bearer ' + token
}
data = {
'message': msg,
"stickerPackageId" : 789, # 貼圖包ID
"stickerId" : 10858 # 貼圖ID
}
requests.post(url, headers=headers, data=data)
line_notify('文字+貼圖測試')
|

傳送圖片
最後再補充一下,LINE Notify
除了能傳送文字
、貼圖
,還能傳送圖片
哦!
直接上程式碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import requests
def line_notify(msg):
token = 'sCq7aGBxxxxxxxxxxxxxxxxxxxxxxx' # 填入你的token
url = 'https://notify-api.line.me/api/notify'
headers = {
'Authorization': 'Bearer ' + token
}
data = {
'message': msg,
"stickerPackageId" : 8525, # 貼圖包ID
"stickerId" : 16581294 # 貼圖ID
}
files = {
'imageFile': open(r'C:\your_folder\dinner.jpg','rb') # 傳圖片檔案
}
requests.post(url, headers=headers, data=data, files=files)
line_notify('晚餐吃這個')
|
趕快來看看效果!

是不是很方便呢?
結語
其實能用python連動LINE Notify
後,就可以做更多有趣的事了。
舉個例子,我用python幫我老爸在幣安寫的自動交易,當成功觸發買賣時就用LINE Notify來通知,非常方便呢!

將來再分享一些好玩的應用,也歡迎大家跟我分享,謝謝觀看。
參考資料