LoginSignup
4
3

More than 3 years have passed since last update.

obnizとLINE botで好きな女優に腹筋を応援してもらう

Last updated at Posted at 2019-06-26

前回はobnizと距離センサーで、腹筋カウンターを作りました。
今回は好きな女優が腹筋を応援してくれるっていうのを作ります。
と言ってもBingの画像検索APIを利用して画像を取得して
腹筋する毎に取得した画像をボットから流すというやつです。

作る

画像はBing Image Searchを利用して取得します。
最初ccという検索結果を取得する国を指定していなかったせいか
全然知らないおじさんの画像とか出てきて困りました。

実装したファイルは以下3点

  • server.js(ボットサーバー)
  • Fukkin.js(obnizを利用した腹筋カウンター)
  • searchImg.js(画像検索用)

server.js
長いのでexpressでlineからのwebhookを受けた時の処理のみ抜粋。
「○○○でふっきん」をトリガーに「○○○」を画像検索しています。
腹筋回数は10としています。

async function handleEvent(event) {
  if (event.type !== 'message' || event.message.type !== 'text') {
    return Promise.resolve(null);
  }

  const message = event.message.text
  const replyToken = event.replyToken
  const userId = event.source.userId

  if (/^.+でふっきん$/.test(message)) {
    await pushMsg(userId, 'OK準備するね!')
    const q = message.replace(/(.+)でふっきん$/, '$1')
    const images = await searchImg(q, 10)
    await wait(3000)
    await pushMsg(userId, '腹筋スタート!')
    await wait(1000)
    new Fukkin(10, async count => {
      const img = images.pop()
      await pushMsg(userId, `${count}!`)
      await pushMsg(userId, countReply[count - 1])
      console.log(img)
      await client.pushMessage(userId, {
        type: 'image',
        originalContentUrl: img,
        previewImageUrl: img
      })
    }, async count => {
      await pushMsg(userId, `おつ!`)
    })
    return Promise.resolve()
  }

  if (message !== 'ふっきん') return reply(replyToken, '腹筋しようよ〜')

  await pushMsg(userId, '待ってました!')
  return reply(replyToken, '誰に応援してもらう?')
}

Fukkin.js
こちらは前回と同じです。

searchImg.js

const request = require('request')
const _  = require('lodash') 
const subscriptionKey = 'サブスクリプションキー'

function searchImage(q, howManyGet){
  let options = {
    method: 'GET',
    baseUrl: 'https://api.cognitive.microsoft.com',
    url: '/bing/v7.0/images/search',
    qs: {
      'q': q,
      'cc': 'JP',
    },
    headers: {
      'Accept-Language': 'ja,en',
      'Ocp-Apim-Subscription-Key': subscriptionKey
    },
    json: true
  }
  return req(options, howManyGet)
}

function req(options, howManyGet) {
  return new Promise(resolve => {
    request(options, (err, res, body) => {
      const images = body.value.map(item => item.contentUrl).filter(url => /^https/.test(url))
      const shuffled = _.shuffle(images).slice(0, howManyGet)
      console.log(shuffled)
      resolve(shuffled)
    })
  })
}
module.exports = searchImage

成果

IMAGE ALT TEXT HERE

まとめ

もう少し応援されている感が欲しい。
あと、Face APIを使って、顔の表情でソートして、腹筋の回数が増すごとに徐々に笑顔になっていくっていうのもやりたかったのですが、今回は時間ぎれとなったので見送ります。。
それから、今さらだけど腹筋のカウントするなら加速度センサーを使うべきだったかも。。そういうアプリ結構あるようだし。
今回は以上です!

4
3
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
4
3