Chenyo.

TW 🇹🇼 • Front-end Developer

0%

提醒這種小事情交給 Line Notify


透過 Line NotifyPythonCrontab 實現行動助理

  • 運用 Line Notify 來達到通知的需求
  • 使用 Python 作為開發語言
  • 使用 Crontab 達成排程執行

發行 Line Notify 權杖

Line Notify > 登入後右上角 > 個人頁面 > 滑到最底下的 發行存取權杖(開發人員用)

點擊 『 發行權杖 』


設定 Line Notify 權杖

輸入權杖名稱 > 選擇接收通知的聊天室 > 發行


複製好 Token 並關閉


可以發現已連動的服務多了剛剛設定的 TEST !!


撰寫 Python Script

安裝 Python

安裝 requests

寫 Python !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import requests

def line_notify_message(token, msg):
headers = {
'Authorization': 'Bearer ' + token,
'content-Type': 'application/x-www-form-urlencoded'
}

payload = {'message': msg}
r = requests.post('https://notify-api.line.me/api/notify', headers=headers, params=payload)
return r.status_code



if __name__ == "__main__":
# 這邊放 Token
token = 'HERE IS YOUR TOKEN'
# 這邊輸入顯示的內容
message = '\n 你好呀 \n 我是 Line Notify \n 123 !'
line_notify_message(token, message)

執行 / 測試 script

run 看看這個 script 看看效果


透過 Crontab 設定排程

撰寫排程

1
crontab -e

開啟編輯器後輸入以下語法:

1
*/1 * * * * /Library/Frameworks/Python.framework/Versions/3.11/bin/python3 /Users/yochen/Code/Python/TEST_PYTHON/LineNotify.py

分 時 日 月 週 執行語法 執行檔案
以上語法代表我要在無時無刻,每一分鐘,透過 Python 執行我指定的 Python 檔

編輯成功後,檢視排程

1
crontab -l

補充:

  • vi 操作:( i 改為 Insert Mode,ESC 後 :wq 儲存退出,點我看更詳細教學 )
  • 尋找本機 Python 安裝連結: which python3

靜待時間到來,就可以發現 ~~ 搞定嚕!


參考文章: