Skip to content

Instantly share code, notes, and snippets.

@deton
Last active September 1, 2019 11:09
Show Gist options
  • Select an option

  • Save deton/15cf5024099d53f37ecb9e92028b9205 to your computer and use it in GitHub Desktop.

Select an option

Save deton/15cf5024099d53f37ecb9e92028b9205 to your computer and use it in GitHub Desktop.
A hubot script that notifies rain using YOLP API
# Description
# A hubot script that notifies rain
#
# Configuration:
# HUBOT_AME_LONGLAT
# HUBOT_AME_YOLP_APPID
# HUBOT_AME_HTTPPROXY
# HUBOT_AME_ROOM
#
# Commands:
# ame - get rainfall [mm/h] value.
#
# Notes:
# hubot changes IRC nickname to uppercase on raining.
#
# Author:
# KIHARA Hideto <deton@m1.interq.or.jp>
#
# Uses Web Services by Yahoo! JAPAN
# http://developer.yahoo.co.jp/about
#
# See Also:
# http://qiita.com/takashyx/items/ccd5ccab7007d43f1355
# http://engineering.otobank.co.jp/entry/2014/10/23/hubot-yahoo-amagumo-on-slack
# http://shokai.org/blog/archives/10542
appid = process.env.HUBOT_AME_YOLP_APPID
unless appid?
console.log "Missing HUBOT_AME_YOLP_APPID in environment: please set and try again"
process.exit(1)
longlat = process.env.HUBOT_AME_LONGLAT
unless longlat?
console.log "Missing HUBOT_AME_LONGLAT in environment: please set and try again"
process.exit(1)
room = process.env.HUBOT_AME_ROOM
rainThreshold = 0.6 # [mm/h]
stopRainThreshold = 0.3 # [mm/h]
interval = 300000 # 5[min]
lunchHour = 12
lunchMinute = 20
getRainfall = (robot, cb) ->
# robot.http('http://weather.olp.yahooapis.jp/v1/place')
robot.http('https://map.yahooapis.jp/weather/V1/place')
.query('output': 'json', 'coordinates': longlat, 'appid': appid)
.get() (err, res, body) ->
if err
cb(err)
return
json = JSON.parse(body)
rainfalls = json.Feature[0].Property.WeatherList.Weather.map (w) -> w.Rainfall
cb(null, rainfalls)
isWorktime = () ->
now = new Date()
hour = now.getHours()
weekday = now.getDay()
1 <= weekday < 6 and 8 <= hour < 21 # monday(1) to friday(5)
isLunchtime = () ->
now = new Date()
hour = now.getHours()
min = now.getMinutes()
isWorktime() and hour is lunchHour and lunchMinute - interval/60000 < min <= lunchMinute
raining = false
updateNickByRainfall = (robot) ->
getRainfall robot, (err, rainfalls) ->
if err
robot.logger.warning "Failed to connect yahoo weather API: #{err}"
return
robot.logger.info "rainfall: #{rainfalls.join(',')}"
rainmsg = "now=#{rainfalls[0]} forecast=#{rainfalls[1]}, #{rainfalls[2]}, #{rainfalls[3]} [mm/h] (from YOLP)"
isSentMsg = false
if (rainfalls[0] > rainThreshold or rainfalls[1] > rainThreshold or rainfalls[2] > rainThreshold or rainfalls[3] > rainThreshold) and isLunchtime() and room
robot.messageRoom room, "RAINING: #{rainmsg}"
isSentMsg = true
if not raining and rainfalls[0] > rainThreshold
raining = true
robot.logger.info "START raining"
if robot.adapterName is 'irc'
robot.adapter.command 'NICK', robot.name.toUpperCase()
else if isWorktime() and not isSentMsg
robot.messageRoom room, "START raining: #{rainmsg}"
else if raining and rainfalls[0] <= stopRainThreshold and rainfalls[1] <= stopRainThreshold
raining = false
robot.logger.info "stop raining"
if robot.adapterName is 'irc'
robot.adapter.command 'NICK', robot.name.toLowerCase()
else if isWorktime() and not isSentMsg
robot.messageRoom room, "stop raining: #{rainmsg}"
module.exports = (robot) ->
if process.env.HUBOT_AME_HTTPPROXY?
proxy = require 'proxy-agent'
robot.globalHttpOptions.httpAgent = proxy(process.env.HUBOT_AME_HTTPPROXY)
robot.globalHttpOptions.httpsAgent = proxy(process.env.HUBOT_AME_HTTPPROXY)
updateNickByRainfall(robot)
# get rainfall with 5 minutes interval,
# and change IRC nickname uppercase if raining
setInterval () ->
updateNickByRainfall(robot)
, interval
robot.hear /^ame/i, (msg) ->
getRainfall robot, (err, rainfalls) ->
if err
msg.send "Failed to connect yahoo weather API: #{err}"
return
msg.send "rain: now=#{rainfalls[0]} forecast=#{rainfalls[1]}, #{rainfalls[2]}, #{rainfalls[3]} [mm/h] (from YOLP)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment