Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8839

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

$
0
0

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

準備

npm init -y
npm i axios

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

コード

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

app.js
constaxios=require('axios');constqs=require('querystring');constLINE_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// という書き方で渡されていますconstLINE_NOTIFY_TOKEN='LINE Notifyのトークン';letconfig={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',})}asyncfunctiongetRequest(){////// LINE Notify に送る ////////////////////////try{constresponseLINENotify=awaitaxios.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分に一回狐の画像を送ってみた


Viewing all articles
Browse latest Browse all 8839

Trending Articles