Line Notify ∙ Python ∙ Crontab === > 運用 `Line Notify` 來達到通知的需求 > 使用 `Python` 作為開發語言 > 使用 `Crontab` 達成排程執行 ### Step 1. > 前往 [Line Notify](https://notify-bot.line.me) > 登入後點擊右上角 > 個人頁面 > 滑到最底下的 `發行存取權杖(開發人員用)` >  ### Step 2. > 輸入權杖名稱 > 選擇接收通知的聊天室 > 發行  ### Step 3. > 複製好 Token 並關閉  > 可以發現已連動的服務多了剛剛設定的 `TEST !!`  ### Step 4. > 安裝 Python > 安裝 requests > 寫 Python ! ```python 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) ``` ### Step 5. > run 看看這個 script 看看效果  ### Step 6. > 撰寫排程 ```shell crontab -e ``` > 開啟編輯器後輸入以下語法: ```shell */1 * * * * /Library/Frameworks/Python.framework/Versions/3.11/bin/python3 /Users/yochen/Code/Python/TEST_PYTHON/LineNotify.py ``` :::info 分 時 日 月 週 執行語法 執行檔案 以上語法代表我要在無時無刻,每一分鐘,透過 Python 執行我指定的 Python 檔 ::: > 編輯成功後,檢視排程 ```shell crontab -l ``` ##### 補充: :::info - vi 操作:( i 改為 Insert Mode,ESC 後 :wq 儲存退出,[點我](https://dywang.csie.cyut.edu.tw/dywang/linuxSystem/node50.html)看更詳細教學 ) - 尋找本機 Python 安裝連結: `which python3` ::: ### Step 7. > 搞定嚕!  --- ### 參考文章: - [Day 8 : LINE Notify 訊息推播通知 (Python版)](https://ithelp.ithome.com.tw/articles/10234115) - [鳥哥私房菜 - 第十五章、例行性工作排程(crontab)](https://linux.vbird.org/linux_basic/centos7/0430cron.php)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up