Skip to content

Instantly share code, notes, and snippets.

@srenatus
Created June 13, 2014 08:57
Show Gist options
  • Select an option

  • Save srenatus/3cd5d3cd78901494ff7c to your computer and use it in GitHub Desktop.

Select an option

Save srenatus/3cd5d3cd78901494ff7c to your computer and use it in GitHub Desktop.
# Description:
# Add entries to trello directly from hubot
#
# Dependencies:
# "node-trello": "0.1.2"
#
# Configuration:
# HUBOT_TRELLO_KEY - your trello developer key
#
# Commands:
# hubot trello all the users - which users do we know about trello for
# hubot trello get token - provides instructions on acquiring a token
# hubot trello set token <token> - set the authentication token
# hubot trello forget me - deletes the authentication token
# hubot trello boards - list your trello boards
# hubot trello set my board to <board> - set your default board
# hubot trello lists - list your trello lists on the default board
# hubot trello cards on <list> - list cards of <list> (of the default board)
# hubot trello move <card> to <list> - move card to <list> (of the default board)
# hubot trello comment <card> <comment> - add <comment> to card <card> (of the default board)
# hubot trello add me to <card> - add you to card <card> (of the default board)
# hubot trello remove me from <card> - add you to card <card> (of the default board)
# hubot trello [show] my cards - list your cards
# hubot trello add card <title> to <list> - add a new card <title> to you <list>
#
# Notes:
# Currently cards can only be added to your default list/board although
# this can be changed
#
# Authors:
# beezly
# srenatus
module.exports = (robot) ->
Trello = require 'node-trello'
trello_key = process.env.HUBOT_TRELLO_KEY
robot.respond /trello all the users/i, (msg) ->
theReply = "Here is who I know:\n"
for own key, user of robot.brain.data.users
if(user.trellotoken)
theReply += user.name + "\n"
msg.send theReply
robot.respond /trello get token/, (msg) ->
msg.reply "Get a token from https://trello.com/1/connect?key=#{trello_key}&name=cicsbot&response_type=token&scope=read,write&expiration=never"
msg.reply "Then send it back to me as \"trello add token <token>\""
robot.respond /trello add token ([a-f0-9]+)/i, (msg) ->
trellotoken = msg.match[1]
msg.message.user.trellotoken = trellotoken
msg.reply "Ok, your token is registered"
robot.respond /trello forget me/i, (msg) ->
user = msg.message.user
user.trellotoken = null
msg.reply "Ok, I have no idea who you are anymore."
robot.respond /trello boards/i, (msg) ->
user = msg.message.user
trellotoken = msg.message.user.trellotoken
t = new Trello trello_key, trellotoken
t.get '/1/members/me/boards/', (err,data) ->
console.log board for board in data
msg.send board.name for board in data
robot.respond /trello set my board to (.*)/i, (msg) ->
board_name = msg.match[1]
user = msg.message.user
trellotoken = msg.message.user.trellotoken
t = new Trello trello_key, trellotoken
t.get '/1/members/me/boards/', (err, data) ->
for board in data
if board.name == board_name
user.trelloboard = board.id
msg.reply "Your trello board is set to #{board.name}"
robot.respond /trello lists/i, (msg) ->
withDefaults msg, (msg, token, board) ->
t = new Trello trello_key, token
t.get "/1/boards/#{board}/lists", (err, data) ->
msg.send list.name for list in data
robot.respond /trello add card (.*) to (.*)/, (msg) ->
content = msg.match[1]
withDefaults msg, (msg, token, board) ->
withListByName msg.match[2], msg, token, board, (err, list) ->
t = new Trello trello_key, token
t.post "/1/lists/#{list.id}/cards", { name: content }, (err, data) ->
msg.reply "Added card " + data.idShort + " to list " + list.name + ": " + data.url
robot.respond /trello (.*)my(.*) cards/i, (msg) ->
withDefaults msg, (msg, token, board) ->
user = msg.message.user
t = new Trello trello_key, token
t.get "/1/members/my/cards", (err, data) ->
if err
msg.reply "There was an error showing your cards."
return
msg.send "#{user.name}'s cards:"
msg.send card.idShort + " " + card.name + " (" + card.url + ")" for card in data
robot.respond /trello comment ([^\ ]*) (.*)/i, (msg) ->
withDefaults msg, (msg, token, board) ->
withCardByNumber msg.match[1], msg, token, board, (err, card) ->
t = new Trello trello_key, token
t.post "/1/cards/#{card.id}/actions/comments", { text: msg.match[2] }, (err, data) ->
if err
msg.reply "There was an error commenting the card."
return
msg.reply "Added comment to card \"#{card.name}\""
robot.respond /trello move (.*) to (.*)/i, (msg) ->
withDefaults msg, (msg, token, board) ->
withCardByNumber msg.match[1], msg, token, board, (err, card) ->
withListByName msg.match[2], msg, token, board, (err, list) ->
# msg.send "Got list #{list.id} (#{list.name})"
t = new Trello trello_key, token
t.put "/1/cards/#{card.id}/idList", { value: list.id }, (err, data) ->
if err
msg.reply "There was an error moving the card."
return
msg.reply "Moved card \"#{card.name}\" to #{list.name}."
robot.respond /trello add me to (.*)/i, (msg) ->
withDefaults msg, (msg, token, board) ->
withUser token, (err, user) ->
withCardByNumber msg.match[1], msg, token, board, (err, card) ->
t = new Trello trello_key, token
t.post "/1/cards/#{card.id}/idMembers", { value: user.id }, (err, data) ->
if err
msg.reply "There was an error adding you to the card."
return
msg.reply "Added you to card \"#{card.name}\"."
robot.respond /trello remove me from (.*)/i, (msg) ->
withDefaults msg, (msg, token, board) ->
withUser token, (err, user) ->
withCardByNumber msg.match[1], msg, token, board, (err, card) ->
t = new Trello trello_key, token
t.del "/1/cards/#{card.id}/idMembers/#{user.id}", { value: user.id }, (err, data) ->
if err
msg.reply "There was an error adding you to the card."
return
msg.reply "Removed you from card \"#{card.name}\"."
robot.respond /trello cards on (.*)/i, (msg) ->
withDefaults msg, (msg, token, board) ->
withListByName msg.match[1], msg, token, board, (err, list) ->
t = new Trello trello_key, token
t.get "/1/lists/#{list.id}", { cards: "open" }, (err, data) ->
if err
msg.send "There was an error showing the list (#{list.id})"
return
msg.send "Cards in " + data.name + ":"
msg.send card.idShort + " " + card.name + " (" + card.url + ")" for card in data.cards
withDefaults = (msg, cb) ->
user = msg.message.user
trelloboard = user.trelloboard
trellotoken = user.trellotoken
if !trellotoken
msg.reply "You don't seem to have a trello token registered. Use \"trello get token\"."
else if !trelloboard
msg.reply "You don't seem to have a default trello board configured. Use \"trello set my board to\" to do that"
else
cb msg, trellotoken, trelloboard
withListByName = (name, msg, token, board, cb) ->
# msg.send "Looking for list #{name}"
t = new Trello trello_key, token
t.get "/1/boards/#{board}/lists", (err, data) ->
if err
cb err, null
for list in data
# msg.send "Looking at #{list.name}"
cb null, list if list.name == name
withCardByNumber = (number, msg, token, board, cb) ->
t = new Trello trello_key, token
t.get "/1/boards/#{board}/cards/#{number}", cb
withUser = (token, cb) ->
t = new Trello trello_key, token
t.get "/1/members/me", cb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment