LoginSignup
10
8

More than 1 year has passed since last update.

Node.jsでLINE Notifyに画像を送るサンプル #protoout

Last updated at Posted at 2020-05-03

Node.jsのaxiosを使ってLINE Notifyに画像を送るサンプルです。
探してもあんまりなかったので残しておきます。

準備

npm init -y
npm i axios

axiosのインストールを忘れずに。

コード

imageFullsizeとimageThumbnailの箇所に画像URLを指定すればOKです。
適宜利用したい画像に変更しましょう。

app.js
const axios = require('axios');
const qs = require('querystring');

const LINE_NOTIFY_API_URL = 'https://notify-api.line.me/api/notify';

// GitHub Actions で実行する際に Secret 値 LINE_TOKEN を利用する
// 
// 実際には、GitHub Actions の
// run: LINE_TOKEN=${{secrets.LINE_TOKEN}} node action.js
// という書き方で渡されています
const LINE_NOTIFY_TOKEN = 'LINE Notifyのトークン';

let config = {
    url: LINE_NOTIFY_API_URL,
    method: 'post',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + LINE_NOTIFY_TOKEN
    },
    data: qs.stringify({
      imageFullsize: `https://images.dog.ceo/breeds/airedale/n02096051_3443.jpg`,
      imageThumbnail: `https://images.dog.ceo/breeds/airedale/n02096051_3443.jpg`,
      message: 'ProtoOut Studioからの通知だよー! https://images.dog.ceo/breeds/airedale/n02096051_3443.jpg',
    })
}

async function getRequest() {

  ////// LINE Notify に送る ////////////////////////

  try {
    const responseLINENotify = await axios.request(config);
    console.log(responseLINENotify.data);
  } catch (error) {
    console.error(error);
  }

}

// getRequest を呼び出してデータを読み込む
getRequest();

実行

node app.js

結果

こんな感じで送られてきます。

今回の画像はhttps://dog.ceo/api/breeds/image/randomから取ってきてます。

スクリーンショット 2020-05-03 21.07.20.png

応用: APIで画像を取得してLINE Notifyへ

すぐにAPIを体験!public-apis 100以上のJavaScript axiosサンプル集
からRandomFoxのAPIを使って狐の画像を取ってきてLINE Notifyに送るのを@kisaichi07さんが書いてました。

この記事を参考にしてみましょう → GitHub Actionsから定期的にLINE Notifyで10分に一回狐の画像を送ってみた

10
8
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
10
8