iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 15
1
自我挑戰組

原來電腦可以這樣用!? 果蠅也懂的程式語言教學系列 第 15

Day15-Python Line 整合應用 -- Line Notify

Line 有一個通知推播機器人的功能,Line Notify 可以將通知利用 Line app 來傳遞,還蠻方便的!


登錄 Line Notify 服務

先到這個連結進行登錄,用自己的 Line 帳號就可以了。
https://notify-bot.line.me/my/services/

點選登錄服務
https://ithelp.ithome.com.tw/upload/images/20191001/20120282oJbNaks3o5.jpg

填完登錄資料後送出
https://ithelp.ithome.com.tw/upload/images/20191001/20120282O4NAtbUn3k.jpg

檢查一下資料是否正確,正確就可以下一步~
https://ithelp.ithome.com.tw/upload/images/20191001/20120282qMMI7CZUCo.jpg

接收 Email 認證信,完成驗證
https://ithelp.ithome.com.tw/upload/images/20191001/20120282sNrDwK5M3P.jpg

把 Client ID 和 Client Secret 複製下來等等會用到
https://ithelp.ithome.com.tw/upload/images/20191001/201202821teD8UsQir.jpg

再來到 https://notify-bot.line.me/my/ 準備發行權杖
https://ithelp.ithome.com.tw/upload/images/20191002/20120282XEBFWUWCC3.jpg

選擇透過1對1聊天接收LINE Notify的通知
https://ithelp.ithome.com.tw/upload/images/20191002/201202823RQKDf1bZd.jpg

Token 一定要記在安全的地方,他就只會顯示這麼一次,一生就這麼一次!所以一定要記好啊啊啊
https://ithelp.ithome.com.tw/upload/images/20191002/20120282pEQH7UK86V.jpg

Python 發送 Line Notify 推播

設定到剛剛就完成了,接下來開始編寫程式碼,Python 程式碼如下,第三方套件運用之前我們介紹過的 requests 來發請求,msg 改成你想推播的訊息,token 改成你的 token,然後加入 Line Notify 官方帳號好友,執行程式即可看看有沒有收到通知。

import requests

def lineNotifyMessage(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

msg = 'Day15 Line Notify'
token = 'YOUR_TOKEN'

lineNotifyMessage(token, msg)

延伸閱讀
https://bustlec.github.io/note/2018/07/10/line-notify-using-python/
https://blog.twtnn.com/2019/04/line-notify.html


上一篇
Day14-資料圖形化 Matplotlib 入門
下一篇
Day16-自動爬取匯率資料
系列文
原來電腦可以這樣用!? 果蠅也懂的程式語言教學30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言