Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

花粉ボット

Posted at

花粉の情報を教えてくれます。現状千代田区のみ

追加はこちら

コードはこちら

kahunbot.js
'use strict';

const express = require('express');
const line = require('@line/bot-sdk');
const axios = require('axios');
const PORT = process.env.PORT || 3000;

const config = {
    channelAccessToken: '',
    channelSecret: ''
};

const app = express();

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));
});

const client = new line.Client(config);

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

  let mes = ''
  if(event.message.text === '今日の花粉は?'){
    mes = 'ちょっとまってね'; //待ってねってメッセージだけ先に処理
    getNodeVer(event.source.userId); //スクレイピング処理が終わったらプッシュメッセージ
  }else{
    mes = event.message.text;
  }

  return client.replyMessage(event.replyToken, {
    type: 'text',
    text: mes
  });
}

const getNodeVer = async (userId) => {
    const res = await axios.get('https://tenki.jp/pollen/3/16/4410/13101/');
    const item = res.data;
    const splitText = item.split('今日の天気')[1];
    const result = splitText.match(/class="pollen-telop">(.*?)<\/span>/)[1]; //正規表現で(無理やり)取得
    console.log(result);

    await client.pushMessage(userId, {
        type: 'text',
        text: `今日の千代田区の花粉は${result}よ!`,
    });
}

app.listen(PORT);
console.log(`Server running at ${PORT}`);

元の情報はこちら

https://tenki.jp/pollen/3/16/4410/13101/ から抜き出してます(小声)。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?