Python Line-Notify

參考網站:
steam教學網:https://steam.oxxostudio.tw/category/python/spider/line-notify.html

發送 LINE Notify 通知

LINE Notify 是 LINE 所提供的一項非常方便的服務,用戶可以透過 LINE,接收各種網站、服務或應用程式 ( GitHub、IFTTT 及 Python...等 ) 的提醒通知,與網站服務連動完成後,LINE所 提供的官方帳號「LINE Notify」將會傳送通知,不僅可與多個服務連動,也可透過 LINE 群組接收通知。

LINE Notify 網址: https://notify-bot.line.me/zh_TW/

01.申請 LINE Notify 權杖

打開 LINE Notify 的網站 ( https://notify-bot.line.me/zh_TW/ ) 後,使用自己的 LINE 帳號登入,登入後從上方個人帳號,選擇「個人頁面」。

進入個人頁面後,點選下方「發行權杖」,權杖 ( token ) 的作用在於讓「連動的服務」可以透過 LINE Notify 發送訊息通知。

點選「發行權杖」後,必須要定義權杖的名稱,以及選擇這個 LINE Notify 所在的聊天群組,通常直接選擇「透過 1 對 1 聊天接收 LINE Notify 通知」。

發行權杖後,會出現一串權杖代碼,點擊下方綠色的「複製」就可複製權杖代碼。

注意,權杖代碼只會出現一次,複製後自行找地方留存。

02.發送 LINE Notify 訊息

import requests

url = 'https://notify-api.line.me/api/notify'
token = '剛剛複製的權杖'
headers = {
    'Authorization': 'Bearer ' + token    # 設定權杖
}
data = {
    'message':'測試一下!'     # 設定要發送的訊息
}
data = requests.post(url, headers=headers, data=data)   # 使用 POST 方法

03.透過 LINE Notify 傳送圖片 (只限線上URL)

import requests

url = 'https://notify-api.line.me/api/notify'
token = '剛剛複製的權杖'
headers = {
    'Authorization': 'Bearer ' + token
}
data = {
    'message':'測試一下!',
    'imageThumbnail':'https://steam.oxxostudio.tw/downlaod/python/line-notify-demo.png',
    'imageFullsize':'https://steam.oxxostudio.tw/downlaod/python/line-notify-demo.png'
}
data = requests.post(url, headers=headers, data=data)

04.使用 LINE Notify 傳送螢幕截圖

參考網站:https://steam.oxxostudio.tw/category/python/example/send-screenshot.html

import pyautogui
import requests

myScreenshot = pyautogui.screenshot()   # 截圖
myScreenshot.save('./test.png')         # 儲存為 test.png

url = 'https://notify-api.line.me/api/notify'
token = '你的權杖'
headers = {
    'Authorization': 'Bearer ' + token    # 設定 LINE Notify 權杖
}
data = {
    'message':'測試一下!'      # 設定 LINE Notify message ( 不可少 )
}
image = open('./test.png', 'rb')    # 以二進位方式開啟圖片
imageFile = {'imageFile' : image}   # 設定圖片資訊
data = requests.post(url, headers=headers, data=data, files=imageFile)   # 發送 LINE Notify

05.加入時間資訊、定時截圖

import pyautogui
import requests
import time

# 定義截圖的函式
def screenshot():
    myScreenshot = pyautogui.screenshot()
    myScreenshot.save('./test.png')

    t = time.time()           # 取得到目前為止的秒數
    t1 = time.localtime(t)    # 將秒數轉換為 struct_time 格式的時間
    now = time.strftime('%Y/%m/%d %H:%M:%S',t1)  # 輸出為特定格式的文字
    sendLineNotify(now)       # 執行發送 LINE Notify 的函式,發送的訊息為時間

# 定義發送 LINE Notify 的函式
def sendLineNotify(msg):
    url = 'https://notify-api.line.me/api/notify'
    token = '你的權杖'
    headers = {
      'Authorization': 'Bearer ' + token
    }
    data = {
      'message':msg
    }
    image = open('./test.png', 'rb')
    imageFile = {'imageFile' : image}
    data = requests.post(url, headers=headers, data=data, files=imageFile)

# 使用for 迴圈,每隔五秒截圖發送一次
for i in range(5):
    screenshot()
    time.sleep(5)

我的程式碼

import line_notify
import requests


def check_response_Line():
    url = "https://notify-api.line.me/api/notify"
    token = "token"
    headers = {
        "Authorization": f"Bearer {token}"
    }
    message = "checking response" #提醒已啟動
    payload = {
        "message": message
    }
    
    image = open('HSB.jpg', 'rb')    # 以二進位方式開啟圖片
    imageFile = {'imageFile' : image}   # 設定圖片資訊

    # 做 API 呼叫
    response = requests.post(url, headers=headers, data=payload , files=imageFile)

    # 檢查 HTTP 狀態碼
    if response.status_code == 200:
        print("API 呼叫成功!")
    else:
        print("API 呼叫失敗,狀態碼:", response.status_code)

    # 打印API回應
    print(response.text)
    
check_response_Line()

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以邮件至 kimfei2014@gmail.com
github