LoginSignup
6
7

More than 5 years have passed since last update.

PythonでLINEメッセージ送信

Posted at

背景

ただなんとなく…。

設定

まずアクセストークンを取得する必要があります。
1. LINE Notifyにログインします。
2. マイページの「アクセストークンの発行(開発者向け)」のトークンを発行します。
3. 通知の際に表示されるトークン名とトークルームを選択し発行ボタンを押します。
4. 発行されたトークンが表示されるのでメモします。
5. LINE Notify を連携するグループに招待します。

下準備はここまでです。

コード

簡易的なコードを記載します。

import requests

url = 'https://notify-api.line.me/api/notify'
token = #アクセストークンを入力
headers = {'Authorization' : 'Bearer ' + token}
message =  #メッセージを入力
payload = {'message' : message}
r = requests.post(url, headers=headers, params=payload)

備考

実は先にC#で作っちゃいました。

using System.Net;

var token = //アクセストークンを入力;
var url = "https://notify-api.line.me/api/notify";
var line_enc = Encoding.UTF8;
var payload = "message=" + //メッセージを入力;
using (var wc = new WebClient())
{
    wc.Encoding = enc;
    wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    wc.Headers.Add("Authorization", "Bearer " + token);
    var response = wc.UploadString(url, payload);
}

参考サイト

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7