Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rjnienaber
Last active July 25, 2019 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjnienaber/31d716f34512562fd3b74f8f0ab73f3e to your computer and use it in GitHub Desktop.
Save rjnienaber/31d716f34512562fd3b74f8f0ab73f3e to your computer and use it in GitHub Desktop.
pick random user from support slack channel
module.exports = (robot) ->
robot.respond /get help/i, (res) ->
# get channel list from slack, might require some extra filtering if there's lot of them
robot.http("https://slack.com/api/channels.list?token=#{process.env.HUBOT_SLACK_TOKEN}")
.header('Accept', 'application/json')
.get() (err, response, body) ->
channels = JSON.parse(body).channels
supportChannelMembers = channels.filter((c) => c.name == 'support')[0].members
# filter users by channel and status
users = Object.entries(res.robot.adapter.client.rtm.dataStore.users)
activeUsers = users.filter((v) => supportChannelMembers.includes(v[0]) && v[1].presence == 'active')
.map((v) => v[1])
# pick random user
randomActiveUser = activeUsers[Math.floor(Math.random() * activeUsers.length)]
res.reply("Random developer for support request: @#{randomActiveUser.name}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment