Last active
August 29, 2015 14:06
-
-
Save shokai/266f951fdd427d863b4c to your computer and use it in GitHub Desktop.
俺APIからのmoveイベントをhubotに通知する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
debug = require('debug')('hubot-ore-api') | |
config = | |
url: 'https://ore-api.herokuapp.com' | |
slack: | |
room: "#news" | |
module.exports = (robot) -> | |
socket = require('socket.io-client').connect config.url | |
## jawboneから動いたイベントがpushされてくる | |
socket.on 'move', (event) -> | |
debug "move - #{JSON.stringify event}" | |
if event.action is 'updation' | |
notify_move event | |
last_notify_at = {} | |
## 動いた事を通知する | |
notify_move = (event) -> | |
if Date.now() - (last_notify_at[event.screen_name] or 0) < 1000*60*60 # 1時間毎に間引く | |
debug "throttled #{event.screen_name}'s notify_move" | |
return | |
last_notify_at[event.screen_name] = Date.now() | |
get_activity "moves", event.screen_name, event.event_xid, (err, move) -> | |
if err or move.details?.steps < 1 | |
debug 'no steps data in event' | |
return | |
current_steps = move.details.steps | |
last_steps = robot.brain.get("steps_#{event.screen_name}") or 0 | |
robot.brain.set("steps_#{event.screen_name}", current_steps) | |
if last_steps > current_steps | |
last_steps = 0 | |
new_steps = current_steps - last_steps | |
if new_steps > 0 | |
txt = "@#{event.screen_name} が#{new_steps}歩運動しました (本日合計#{current_steps}歩 #{move.details.km}km)" | |
else | |
txt = "@#{event.screen_name} が活発に活動しています" | |
robot.send config.slack, txt | |
## ore-api.herokuappにあるJawbone APIプロキシを使う | |
get_activity = (type, screen_name, xid, callback = ->) -> | |
robot.http("#{config.url}/#{screen_name}/#{type}.json?xid=#{xid}").get() (err, res, body) -> | |
if err | |
callback err | |
return | |
try | |
data = JSON.parse body | |
catch err | |
callback err | |
return | |
debug data | |
callback null, data.data | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment