2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LINEのMessaging APIからのデータをBeebotteに垂れ流すNode.jsサーバ

2
Last updated at Posted at 2019-08-28

はじめに

※この投稿は、LINEのメッセージをGoogle Homeで読み上げる(機能強化版)を実装済みで更に機能強化を求む人向けの投稿です。

LINEのメッセージをGoogle Homeで読み上げる(機能強化版)を使って、個人的に機能強化も加えたりして遊んでいたんですが、ついにMicrosoft flowの無料で750回のAPIコール/月の上限に達しました。

むしゃくしゃして課金しようとしたんですが、

  • アカウント作るのに独自ドメインのメールアドレスが必要
  • USD $5.00/月と、個人で楽しむにしては高いコストがかかる

ので、やっぱりむしゃくしゃしてLINE Messaging APIからのWebhook呼び出しをBeebotteに垂れ流すnodejsサーバを書きました。っていう話。

この話を理解するには以下の知識が必要になります。

特徴・できること

  • LINE Messaging APIからのWebhook呼び出しをBeebotteに垂れ流すサーバ
    • 「LINEのWebhookリクエストの内容は{events:[{<メッセージ内容>}]となっていますが、BeebotteのREST APIは{data:{}}の形式しか受け付けない」ため、要素名を書き換えてBeebotteに送りつけるサーバ。
  • Microsoft Flowの無料750回APIコール/月を突破し、4000回/時のアクセスまでできる。およそ月に約28万アクセスできる計算。どのご家庭にもあるbot込の家族lineグループなら安心の帯域量!
  • Microsoft Flowのときはチェックしていなかった、X-Line-Signature(LINE developersを参照)を検証する。確実にLINEサーバからのメッセージしか通さない。

ソースちょーだい

プロジェクト自体は、githubで公開しています。※ただ、まだ動くだけの書き捨てコード状態。

インストール手順

Glitchで新規プロジェクトを作成。
# npm i crypto body-parser で必要なパッケージをインストール。

.envにLINEのCHANNEL_SECRETとBEEBOTTEのTOKENを書いて、server.jsもコピペする。

LINE_CHANNEL_SECRET='[SECRET]'
BEEBOTTE_TOKEN='[TOKEN]'
server.js

const express = require('express');
const crypto = require("crypto");
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.json({verify: function (req, res, buf, encoding) {
  
  function validate_signature(signature, body) {
    const LINE_CHANNEL_SECRET = process.env.LINE_CHANNEL_SECRET;
    return signature == crypto.createHmac('sha256', LINE_CHANNEL_SECRET).update(body).digest('base64');
  }
  
  //test signature
  if (!validate_signature(req.headers['x-line-signature'], buf)) {
    throw new Error('Invalid signature.');
  }
}}));

//リクエストがあったら応答
app.post('/api/lineToBeebotte', function(req, res, err) {
  const events = req.body.events;
  const resJson = {
      data: req.body.events
  };
  const request = require('request-promise');
  
  console.log(resJson);
  
  request({
    url: 'https://api.beebotte.com/v1/data/publish/line/message?token=' + process.env.BEEBOTTE_TOKEN,
    method: 'POST',
    form : resJson
  }).then(()=>{
      res.json({result:'sucsess'});
  }).catch(()=>{
      res.json({result:'fail'});
  });
});
const listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});

LINEのコンソールに設定する

LINEのデベロッパツールの中のコンソールに入り、Webhook URLhttps://youredoamin.glitch.me/api/lineToBeebotte を設定する。※youredoaminは置き換え

まとめ

以上、ただ、垂れ流すだけのサーバですが、nodejsの勉強になって楽しかったです。ただ、朝4時までかけて突貫工事ですることではなかった←

2
0
1

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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?