昨日紹介したコードでURLパラメータの指定方法に対して納得していなかったので
「querystring」を利用して修正してみました。
※実行結果としては昨日のコードと同じです。
# Description:
# 今日の天気を教えてくれます。
#
# Commands:
# 今日の天気
#
request = require 'request'
qs = require 'querystring'
module.exports = (robot) ->
robot.hear /今日の天気/i, (msg) ->
url = "http://api.openweathermap.org/data/2.5/weather?"
auth = {
appid: 'API_KEY' # API_KEY
q: 'Tokyo,jp' # 都市
units: 'metric' # 摂氏
}
params = qs.stringify(auth)
request
url: url + params
, (err, response, body) ->
json = JSON.parse body
weatherName = json['weather'][0]['main']
icon = json['weather'][0]['icon']
temp = json['main']['temp']
temp_max = json['main']['temp_max']
temp_min = json['main']['temp_min']
msg.send "今日の東京の天気は「" + weatherName + "」です。\n気温:"+ temp + "℃ 最高気温:" + temp_max+ "℃ 最低気温:" + temp_min + "℃\nhttp://openweathermap.org/img/w/" + icon + ".png"
クラス化しようとしたけど、何か上手くいかなかった(:3_ヽ)_

