Line-Bot-SDKを使わずrequests.postからLINE通知Botができたのでメモ
Pythonを使ってLineに通知と画像を送信するbotを作成
Line通知Botを利用するにはLineNotifyから通知用のAPIトークンを取得します
- Line Notify
- 普段使いのLine用のIDとパスワードでログインができます。
画像のURLはWEB上のものでも行けます。
ローカルにある画像はフルパスが良いです。
送信する画像サイズは1Mくらいまでに抑えておきましょう
import requests
import argparse
def lineNotify(message, *args):
line_notify_api = 'https://notify-api.line.me/api/notify'
line_notify_token = 'LineNotifyから取得したAPIトークンを入れる'
payload = {'message': message}
headers = {'Authorization': 'Bearer ' + line_notify_token}
if len(args) == 0:
# 画像なしテキストのみ送信
requests.post(line_notify_api, data=payload, headers=headers)
else:
# 画像ありテキスト+画像送信
files = {"imageFile": open(args[0], "rb")}
requests.post(line_notify_api, data=payload, headers=headers, files=files)
text = "通知内容"
image_url = r"C:\Users\***\Image\Line_sending_image.jpg"
lineNotify(text, image_url)
実行ファイルと同じディレクトリにある画像ファイルのフルパスを取得するには
import os
file_full_path = os.path.abspath("Line_sending_image.jpg")