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

LINEBotからRaspberryPiで写真を撮ってLINEにおくる!

$
0
0

はじめに

LINEBotでRaspberryPiで写真を撮って送るという、カメラ装置です。
ハッカソンで作成したねこしぇるじゅ-自慢の猫をとにかく拡散!- | ProtoPediaの一部をupdateしたものです。

概要

LINEBotからRaspberryPiを動かして写真を撮り、Gyazoに送って写真をLINEに送るものです。
LINEBotでRaspberryPiを動かしています。
RaspberryPi node.js ngrok Gyazo API という感じです。

できたもの

使い方

RaspberryPiを猫がよくいる場所に置きます。
猫が見たいので LINEBotに「ねこ」と入れます。
(「ねこ」以外のワードには「何が見たいの?」と返してきます。)
写真を撮るのに時間がかかるので、「写真撮ってくるから待っててねー」、とつなぎのワードが入ります。
10秒ほどすると写真が送られてきます。

環境

MacBook Pro macOS Mojave
 Visual Studio Code 1.44.0

RaspberryPi 3B
 Release: 10
 Codename: buster
 Node.js v12.16.3
 npm v6.14.4
Pi camera
洗濯バサミ

Raspberry Piの準備

こちらを参考にnode.jsとnpmの最新版を入れましょう。
Raspberry PiにNode.jsとnpmの最新版をインストールする - Qiita
そのほかで必要なnpmはこちら

//Gyazo API
$ npm i gyazo-api 
// LINEbot SDK
$ npm i @line/bot-sdk express
//ngrok
$ wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip 
//ngrokのzipを解凍する
$ unzip ngrok-stable-linux-arm.zip 

コード

node.js
'use strict';constexpress=require('express');constline=require('@line/bot-sdk');constPORT=process.env.PORT||3000;constGyazo=require('gyazo-api');constgyazoclient=newGyazo('Your access token');constconfig={channelSecret:'***',channelAccessToken:'***'};constapp=express();app.get('/',(req,res)=>res.send('Hello LINE BOT!(GET)'));//ブラウザ確認用app.post('/webhook',line.middleware(config),(req,res)=>{console.log(req.body.events);Promise.all(req.body.events.map(handleEvent)).then((result)=>res.json(result));});constclient=newline.Client(config);asyncfunctionhandleEvent(event){if(event.type!=='message'||event.message.type!=='text'){returnPromise.resolve(null);}letmes=event.message.text;if(event.message.text.match("ねこ")){awaitclient.replyMessage(event.replyToken,{type:"text",text:'写真を撮ってくるからちょっと待っててね'});awaitnyanpi();returnnyancoPic(event.source.userId);}else{returnclient.replyMessage(event.replyToken,{type:"text",text:'何が見たいのかな?'});}}//picameraで写真を撮って送るconstnyanpi=async(userId)=>{constPiCamera=require('pi-camera');constmyCamera=newPiCamera({mode:'photo',output:`${__dirname}/nyan.jpg`,width:640,height:480,nopreview:true,});awaitmyCamera.snap().then((result)=>{console.log("ok");gyazoclient.upload('./nyan.jpg',{title:"my picture",desc:"upload from nodejs"}).then((res)=>{console.log("送った");}).catch((err)=>{console.error(err);console.log("送レナカッタ");});}).catch((error)=>{// Handle your error});}//Gyazoの最新の写真のURLをとってくるconstnyancoPic=async(userId)=>{letgyazoimgUrl='';awaitgyazoclient.list().then(function(response){console.log(response.data[0]);gyazoimgUrl=`${response.data[0].url}`;console.log(gyazoimgUrl);}).catch(function(err){console.error(err);});awaitclient.pushMessage(userId,{type:'image',originalContentUrl:gyazoimgUrl,previewImageUrl:gyazoimgUrl});}// const finpic = async () => {//   await nyanpi();//   await nyancoPic(event.source.userId);// }app.listen(PORT);console.log(`Server running at ${PORT}`);

動いていないところ

最新じゃなくて1個前の写真しか持ってこれない。
await/asyncがうまく使えていない感じ。。。
OKと送ったというconsole.logの間でGyazoに取りに行っちゃう
(ログ↓)

ok
{
  image_id: '***',
  permalink_url: 'https://api.gyazo.com/***',
  url: 'https://i.gyazo.com/f***',
  metadata: {
    app: null,
    title: 'my picture',
    url: null,
    desc: 'upload from nodejs'
  },
  type: 'jpg',
  thumb_url: 'https://thumb.gyazo.com/thumb/200/***ApieIALnjo-jpg.jpg',
  created_at: '2020-05-07T01:06:27+0000'
}
https://i.gyazo.com/f6ab3a37fce7260c19e8f0f49a934a8a.jpg
送った

参考サイト

20200504 raspberrypi handson_honpen
Node.jsでラズパイのカメラモジュールを使う – plog
gyazo-api - npm
raspbian(Raspberry Pi)のバージョン確認方法

感想

Piカメラ音がしないのでお猫様をこっそり撮影できて良い〜
スマホのシャッター音に慣れすぎか。

promise難しい。。。


Viewing all articles
Browse latest Browse all 9229