LoginSignup
19
17

More than 5 years have passed since last update.

【Hubot】天気APIを叩いて今日・明日・明後日の天気を返すスクリプト

Last updated at Posted at 2015-03-12

概要

BOTに今日・明日・明後日の天気を問うと答えてくれるようにする

livedoorが提供するお天気情報「Weather Hacks」を使ってます。

※ここでは大阪の天気を取得しています

基本URL + 大阪のID(270000)
http://weather.livedoor.com/forecast/webservice/json/v1?city=270000

リクエストする地域とidの対応は 全国の地点定義表(RSS)

全体コード

weather.coffee

module.exports = (robot) ->   
  robot.hear /(.*)の天気教えて/i, (msg) ->
   switch msg.match[1]
      when '今日'
        day = 0
      when '明日'
        day = 1
      when '明後日'
        day = 2
      else
        day = 3
        break
    request = msg.http('http://weather.livedoor.com/forecast/webservice/json/v1?city=270000')
    .get()
    request (err, res, body) ->
      json = JSON.parse body
      if day == 3 then forecast = 'わからんす' else forecast = json['forecasts'][day]['telop']
      msg.reply forecast


とざっくり書きました。

結果

スクリーンショット 2015-03-12 10.36.41.png

発展

都市名も指定できるように拡張する

19
17
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
19
17