概要
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
とざっくり書きました。
結果
発展
都市名も指定できるように拡張する