Created
March 22, 2016 18:56
-
-
Save cha55son/df86c36f1fcbb3c9055d to your computer and use it in GitHub Desktop.
Hubot: Post to a channel when a steam account starts/ends a game.
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
# Description: | |
# Post to a channel when a steam account starts a game. | |
# | |
# Commands: | |
# | |
# Configuration: | |
# STEAM_API_KEY: Steam community api key to access steam accounts. | |
# STEAM_USER_ID: The steam user id in which to monitor. | |
# STEAM_POST_CHANNEL_NAME: The channel to post to when the user starts playing. | |
# | |
Promise = require 'promise' | |
STEAM_API_KEY = process.env.STEAM_API_KEY | |
STEAM_USER_ID = process.env.STEAM_USER_ID | |
STEAM_CHANNEL = process.env.STEAM_POST_CHANNEL_NAME | |
STEAM_BROADCAST_URL = "http://steamcommunity.com/broadcast/watch/#{STEAM_USER_ID}" | |
STEAM_URL = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=#{STEAM_API_KEY}&steamids=#{STEAM_USER_ID}" | |
POLL_MINUTES = 2 | |
BRAIN_KEY = 'steam.userData' | |
INTROS = [ | |
"Grab your controllers!", | |
"Someone is obviously not working.", | |
"It's happening!", | |
"OMFG!!!", | |
"Go go go!", | |
"It's about time!", | |
"Wish I could play :(..." | |
] | |
module.exports = (robot) -> | |
if (!STEAM_API_KEY || STEAM_API_KEY.length is 0) || | |
(!STEAM_USER_ID || STEAM_USER_ID.length is 0) || | |
(!STEAM_CHANNEL || STEAM_CHANNEL.length is 0) | |
robot.logger.error "Invalid configuration for 'steam-game-status.coffee'. Skipping!" | |
return | |
userData = robot.brain.get(BRAIN_KEY) or { | |
userId: STEAM_USER_ID, | |
gameName: null | |
} | |
robot.brain.set BRAIN_KEY, userData | |
requestSteamUser = -> | |
robot.logger.debug "Requesting steam user data." | |
new Promise (resolve, reject) -> | |
robot.http(STEAM_URL).get() (err, res, body) -> | |
return reject(err) if err | |
# robot.logger.debug "Received steam user data:" | |
# robot.logger.debug body | |
payload = null | |
try | |
payload = JSON.parse body | |
catch error | |
robot.logger.error body | |
return reject "Failed to parse json" | |
if payload.response && payload.response.players && payload.response.players.length == 1 | |
robot.logger.debug "Received steam user data." | |
resolve payload.response.players[0] | |
else | |
robot.logger.error body | |
reject "Invalid data structure received while requesting steam user." | |
handleUserData = (user) -> | |
robot.logger.debug "Handling steam user data." | |
# User is currently playing a game | |
if typeof user.gameid is 'string' | |
robot.logger.debug "#{user.personaname} is currently playing #{user.gameextrainfo}." | |
# Notify the channel if the userData does not have a game set | |
if userData.gameName is null | |
userData.gameName = user.gameextrainfo | |
notifyChannel user | |
else | |
robot.logger.debug "#{user.personaname} is not currently in a game." | |
unless userData.gameName is null | |
robot.messageRoom STEAM_CHANNEL, "_#{user.personaname} has quit playing #{userData.gameName}._" | |
userData.gameName = null | |
robot.brain.set BRAIN_KEY, userData | |
notifyChannel = (user) -> | |
robot.logger.debug "Notifying #{STEAM_CHANNEL} that #{user.personaname} is playing #{user.gameextrainfo}." | |
intro = INTROS[Math.floor(Math.random() * INTROS.length)] | |
robot.messageRoom STEAM_CHANNEL, "<!here> #{intro} #{user.personaname} is now playing #{user.gameextrainfo}!" | |
robot.messageRoom STEAM_CHANNEL, "Check out the stream here #{STEAM_BROADCAST_URL}." | |
# Kick off the polling | |
pollMS = POLL_MINUTES * 60 * 1000 | |
robot.logger.info "Polling steam every #{pollMS / 1000} seconds." | |
setInterval -> | |
requestSteamUser().then (user) -> | |
handleUserData user | |
, (err) -> | |
robot.logger.error "Issue polling steam user info. [#{err}]" | |
, POLL_MINUTES * 60 * 1000 | |
robot.hear /.*/, (res) -> | |
console.log "Room: " + res.message.room |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment