LINE Notify+node.jsで通知を行う
LINE Notifyを使ってnode.jsから通知をしてみます。
「ログイン」を押します。
id,pw入力して「ログイン」を押します。
LINEアプリを起動して、本人確認します。本人確認出来たらLINE Notifyの画面がログイン後の画面に変わります。
「マイページ」をクリックします。
「トークンを発行する」をクリックします。
「トークン名」は適当で良いです。ここでは「test」にしています。
「1:1でLINE Notifyから通知を受け取る」を選択し、「発行する」をクリックします。
「コピー」をクリックしてトークンをコピーします。クリップボードにコピーしたら「閉じる」をクリックします。
このトークンを使用してnode.jsプロジェクトからLINE通知を行います。
nodeプロジェクト新規作成
コマンドプロンプトから以下実行してプロジェクト新規作成します。
C:\lineproject>npm init -y C:\lineproject>npm i --save axios
index.jsファイルを作成します。中身は以下です。
index.js
'use strcit';
const axios = require('axios');
const qs = require('querystring');
const BASE_URL = 'https://notify-api.line.me';
const PATH = '/api/notify';
const LINE_TOKEN = `EYWxHS3hjKVjoh3fRxUQPWWOILucbZM7sd2quJiba24`; //先ほど発行したトークン
const config = {
baseURL: BASE_URL,
url: PATH,
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Bearer ${LINE_TOKEN}`
},
data: qs.stringify({
message: `line通知`,
})
};
(async function(){
const response = await axios.request(config);
console.log(response)
})();
これで完了です。index.jsを実行すればLINEに通知が行きます。
c:\lineproject>node index.js
画像を送信する
画像を送信するにはmessageの他にimageThumbnail、imageFullsizeを指定します。
index.js
'use strcit';
const axios = require('axios');
const qs = require('querystring');
const BASE_URL = 'https://notify-api.line.me';
const PATH = '/api/notify';
const LINE_TOKEN = `Q1E3q7oVPCvb29NsQlbSv3XEI8KctpCALdw4wlrdQDi`;
const config = {
baseURL: BASE_URL,
url: PATH,
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Bearer ${LINE_TOKEN}`
},
data: qs.stringify({
message: `line通知`,
imageFullsize: `https://example.com/58a5da1f54905724008b509d-w1280.jpg`, // ★
imageThumbnail: `https://example.com/58a5da1f54905724008b509d-w1280.jpg` // ★
})
};
(async function(){
const response = await axios.request(config);
console.log(response)
})();
スタンプを送信する
スタンプを送信することができます。
stickerPackageIdとstickerIdを指定する必要があります。
index.js
'use strcit';
const axios = require('axios');
const qs = require('querystring');
const BASE_URL = 'https://notify-api.line.me';
const PATH = '/api/notify';
const LINE_TOKEN = `fuL6q205Lc1Vh4hkmrVNt6Opv6WBwSYiaEvafaYQ1xF`;
let config = {
baseURL: BASE_URL,
url: PATH,
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Bearer ${LINE_TOKEN}`
},
data: qs.stringify({
message: `hey boy`,
stickerPackageId: 8522, // ★
stickerId: 16581266 // ★
})
};
(async function(){
const response = await axios.request(config);
console.log(response)
})();
これでindex.jsを実行すればLINEにスタンプ通知が行きます。
c:\lineproject>node index.js
送信可能なスタンプリスト一覧です。
グループLINEに通知
グループラインに通知する際はグループラインのトークンを取得し、LINE Notifyをグループラインに招待しておく必要があります。
LINE Notify API Document

KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES20xx),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^










コメント
コメント失礼します。プログラムを学んでいる学生です。webサイトの更新時にLINE Notifyで通知するサービスを作って配布したいと考えているのですが、このページで書かれていることはドメインの取得が必要でしょうか?
ここに書いていることはドメイン不要かと思います。ただ勉強するのであれば独自ドメインは1つは持っておかれた方が良いですよ!^^
頑張ってください☆