|
# Description |
|
# A Hubot script that bitches at people who haven't said anything within 12 hours of the time limit |
|
# Only tested with slack... |
|
# |
|
# Configuration: |
|
# Hardcoded below |
|
# |
|
# Commands: |
|
# None |
|
# |
|
# Author: |
|
# gwalker |
|
# |
|
util = require('util') |
|
|
|
settings = [ |
|
{ |
|
Team: "ScrumChannel", |
|
room: "scrum-updates", |
|
excludedUsers: [ |
|
"hubot", |
|
"gwalker" |
|
], |
|
dueTime: { |
|
hour: 10, |
|
minute: 50 |
|
} |
|
} |
|
] |
|
checkEverySeconds = 60 |
|
minimumMessageSize = 50 |
|
cutoffHours = 12 |
|
|
|
|
|
_roomsToWatch = null |
|
_heardMessageFrom = {} |
|
|
|
initalize = () -> |
|
roomsArray = (teamSettings.room for teamSettings in settings) |
|
_roomsToWatch = {} |
|
for room in roomsArray |
|
_roomsToWatch[room] = true |
|
return |
|
|
|
getUsersFromGroup = (client, groupName) -> |
|
members = client.dataStore.getGroupByName(groupName).members; |
|
|
|
users = []; |
|
for id in members |
|
do (id) -> |
|
member = client.dataStore.getUserById(id); |
|
if member && !member.deleted |
|
users.push(member) |
|
return users |
|
|
|
|
|
getUsersFromChannel = (client, channel) -> |
|
members = client.dataStore.getChannelByName(channel).members; |
|
|
|
users = []; |
|
for id in members |
|
do (id) -> |
|
member = client.dataStore.getUserById(id); |
|
if member && !member.deleted |
|
users.push(member) |
|
return users |
|
|
|
module.exports = (robot) -> |
|
|
|
# Hear messages that are at least 50 chars long (an update should have detail) |
|
robot.hear /[\s\S]{50,}/i, (msg) -> |
|
roomObj = robot.adapter.client.rtm.dataStore.getGroupById(msg.message.user.room) |
|
if !roomObj |
|
return |
|
room = roomObj.name |
|
console.log "heard long message from room #{room}" |
|
if _roomsToWatch && _roomsToWatch[room] |
|
console.log "listened to jira update from room #{room}" |
|
username = msg.message.user.name |
|
console.log "listened to jira update from room #{room} user #{username}" |
|
_heardMessageFrom[room] = _heardMessageFrom[room] || {} |
|
_heardMessageFrom[room][username] = new Date() |
|
|
|
robot.hear /omgbbq/i, (msg) -> |
|
robot.send {room: "gwalker"}, "Whatever brah: #{msg.message.user.name}" |
|
robot.send {room: "gwalker"}, util.inspect(msg.message.user) |
|
#robot.send {room: "gwalker"}, "Whatever brah: #{msg}" |
|
#console.log msg.robot.adapter.client.getChannelGroupOrDMByName("hubot-test").members |
|
buildUsersToMentionReport({ room:"hubot-test" }) |
|
|
|
robot.respond /lookup (.*)/i, (res) -> |
|
console.log robot.adapter.client.getUserByName(res.match[1]) |
|
|
|
robot.respond /gaveUpdate (.*)/i, (res) -> |
|
#TODO add security: |
|
console.log "Marking as gave update: #{res.match[1]}" |
|
thisSettings = settings[0] |
|
room = thisSettings.room |
|
_heardMessageFrom[room] = _heardMessageFrom[room] || {} |
|
_heardMessageFrom[room][res.match[1]] = new Date() |
|
usersToNag = buildUsersToMentionReport(thisSettings) |
|
robot.send {room: "gwalker"}, "Still left to give update: " + ("<@#{user.id}|#{user.name}>" for user in usersToNag).join(", ") |
|
|
|
|
|
# GLOBALish |
|
buildUsersToMentionReport = (teamSettings) -> |
|
members = getUsersFromGroup(robot.adapter.client.rtm, teamSettings.room) |
|
memberNames = ({ name: user?.name, id: user?.id } for user in members) |
|
console.log "===memberNames===" |
|
console.log memberNames |
|
console.log "===teamSettings.excludedUsers===" |
|
console.log teamSettings.excludedUsers |
|
console.log "===heardUpdate===" |
|
console.log heardUpdate |
|
|
|
# Filter the good children |
|
heardUpdate = _heardMessageFrom[teamSettings.room] || {} |
|
cutoffDate = new Date() |
|
cutoffDate.setHours(cutoffDate.getHours() - cutoffHours) |
|
for k,v of heardUpdate |
|
if new Date(v) < cutoffDate |
|
delete heardUpdate[k] |
|
memberNames.filter (x) -> x.name not of heardUpdate && x.name not in teamSettings.excludedUsers |
|
|
|
updateDueDateIfNull = (teamSettings) -> |
|
if !teamSettings._dueDate |
|
teamSettings._dueDate = new Date() |
|
teamSettings._dueDate.setHours(teamSettings.dueTime.hour) |
|
teamSettings._dueDate.setMinutes(teamSettings.dueTime.minute) |
|
if teamSettings._dueDate < new Date() |
|
teamSettings._dueDate.setDate(teamSettings._dueDate.getDate()+1) |
|
robot.send {room: "gwalker"}, ":totoro: Updated time to #{teamSettings._dueDate}" |
|
|
|
checkTime = () -> |
|
resetTimeout() |
|
## make sure everything has a dueDate set, updated in place |
|
##(updateDueDateIfNull(item) for item in settings) |
|
# for any dates in the past |
|
# See who hasn't messaged the channel |
|
for teamSettings in settings |
|
#Make sure we have a dueDate set |
|
updateDueDateIfNull(teamSettings) |
|
dateNow = new Date() |
|
if teamSettings._dueDate < dateNow |
|
# if today is NOT the weekend, send the message |
|
if dateNow.getDay() != 0 && dateNow.getDay() != 6 |
|
usersToNag = buildUsersToMentionReport(teamSettings) |
|
robot.send {room: "gwalker"}, "-----" |
|
robot.send {room: "gwalker"}, "if #{teamSettings._dueDate} < #{dateNow}" |
|
# ##robot.send {room: "gwalker"}, "Timeout Info: #{DEBUGResetTimes} --- #{util.inspect(DEBUGTimeoutIds, {breakLength: 180})}" |
|
robot.send {room: "gwalker"}, "TODO will be messaging #{teamSettings.Team}, in the room #{teamSettings.room} now" |
|
robot.send {room: "gwalker"}, "Users to nag: " + util.inspect(usersToNag) |
|
robot.send {room: "gwalker"}, "Users seen talk: " + util.inspect(_heardMessageFrom[teamSettings.room]) |
|
# ##robot.send {room: "gwalker"}, "-----" |
|
# ##robot.send {room: "gwalker"}, if usersToNag.length > 0 then "Have anything to update us with " + ("<@#{user.id}|#{user.name}>" for user in usersToNag).join(", ") + "?" else "Everybody gave an update! Good job #{teamSettings.Team}!" |
|
robot.send {room: "gwalker"}, "-----" |
|
robot.send {room: "#{teamSettings.room}"}, if usersToNag.length > 0 then "Have anything to update us with " + ("<@#{user.id}|#{user.name}>" for user in usersToNag).join(", ") + "?" else "Everybody gave an update! Good job #{teamSettings.Team}!" |
|
# Reset this teamSettings: |
|
delete _heardMessageFrom[teamSettings.room] |
|
teamSettings._dueDate = null |
|
|
|
DEBUGResetTimes = ["","","","",""] |
|
DEBUGTimeoutIds = ["","","","",""] |
|
|
|
timeoutId = null |
|
resetTimeout = () -> |
|
timeoutId = setTimeout(checkTime, checkEverySeconds * 1000) |
|
DEBUGTimeoutIds.shift() |
|
DEBUGTimeoutIds.push(timeoutId.toString()) |
|
DEBUGResetTimes.shift() |
|
DEBUGResetTimes.push(new Date()) |
|
|
|
|
|
# ON-STARTUP INITALIZATION |
|
|
|
initalize() |
|
resetTimeout() |
|
robot.send {room: "gwalker"}, "I just restarted!" |
|
robot.send {room: "gwalker"}, "Rooms To Watch: " + util.inspect(_roomsToWatch) |