5
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

LineNotifyを使ってメッセージ、画像を送信する

LineNotifyを使って Fortniteの画像とメッセージを送ってみる。

まず、LINE Notifyのサイトでログインしてトークンを発行する。
https://notify-bot.line.me/ja/

・Mac
・python

(1)ディレクトリ構成
imagesの中に、gazo.jpegを保存。

test
├test.py
└images
  └gazo.img

(2)test.pyを記述
コードは以下の通り。

test.py
#coding:UTF-8
import requests,os



#------画像を送る場合----------------------------
def main_gazo():
    url = "https://notify-api.line.me/api/notify"
    token = "*********************************"
    headers = {"Authorization" : "Bearer "+ token}

    message = 'Fortnite!'
    payload = {"message" :  message}
    #imagesフォルダの中のgazo.jpg
    files = {"imageFile":open('images/gazo.jpeg','rb')}
  #rbはバイナリファイルを読み込む
    post = requests.post(url ,headers = headers ,params=payload,files=files)


#------メッセージを送る場合----------------------------
def main():
    url = "https://notify-api.line.me/api/notify"
    token = "*********************************"
    headers = {"Authorization" : "Bearer "+ token}

    message = 'Fortnite!'
    payload = {"message" :  message}

    r = requests.post(url ,headers = headers ,params=payload)


if __name__ == '__main__':
    main_gazo()#画像を送るmain_gazo()を動かしてみる(メッセージの場合はmain())
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
5
Help us understand the problem. What are the problem?