LINE Notify+node.jsで通知を行う

LINE Notify+node.jsで通知を行う

LINE Notifyを使ってnode.jsから通知をしてみます。

LINE Notify

LINE Notify+node.jsで通知を行う

「ログイン」を押します。

LINE Notify+node.jsで通知を行う

id,pw入力して「ログイン」を押します。

LINE Notify+node.jsで通知を行う

LINEアプリを起動して、本人確認します。本人確認出来たらLINE Notifyの画面がログイン後の画面に変わります。

LINE Notify+node.jsで通知を行う

「マイページ」をクリックします。

LINE Notify+node.jsで通知を行う

「トークンを発行する」をクリックします。

LINE Notify+node.jsで通知を行う

「トークン名」は適当で良いです。ここでは「test」にしています。

「1:1でLINE Notifyから通知を受け取る」を選択し、「発行する」をクリックします。

LINE Notify+node.jsで通知を行う

「コピー」をクリックしてトークンをコピーします。クリップボードにコピーしたら「閉じる」をクリックします。

このトークンを使用して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 Notify+node.jsで通知を行う

送信可能なスタンプリスト一覧です。

送信可能なスタンプリスト
LINE Developersサイトは開発者向けのポータルサイトです。LINEのさまざまな開発者向けプロダクトを利用するための、管理ツールやドキュメントを利用できます。LINEログインやMessaging APIを活用して、アプリやサービス...

グループLINEに通知

グループラインに通知する際はグループラインのトークンを取得し、LINE Notifyをグループラインに招待しておく必要があります。

LINE Notify API Document

LINE Notify
LINE Notify allows you to send web notifications from GitHub, IFTTT, Mackerel, and more directly to your LINE chats.

コメント

  1. abc より:

    コメント失礼します。プログラムを学んでいる学生です。webサイトの更新時にLINE Notifyで通知するサービスを作って配布したいと考えているのですが、このページで書かれていることはドメインの取得が必要でしょうか?

    • 高橋 より:

      ここに書いていることはドメイン不要かと思います。ただ勉強するのであれば独自ドメインは1つは持っておかれた方が良いですよ!^^
      頑張ってください☆

株式会社CONFRAGE ITソリューション事業部をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

タイトルとURLをコピーしました