Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 109 lines (98 sloc) 3.727 kB
0fd9560 @tombell Update "a" scripts help comments
tombell authored
1 # Description:
2 # A way to add tasks to Asana
3 #
4 # Dependencies:
5 # None
6 #
7 # Configuration:
8f88b4e @abh1nav Adding some documentation
abh1nav authored
8 # HUBOT_ASANA_API_KEY - find this in Account Settings -> API
a610f65 @rajiv update asana script to show task names.
rajiv authored
9 #
8f88b4e @abh1nav Adding some documentation
abh1nav authored
10 # HUBOT_ASANA_WORKSPACE_ID - list all workspaces using
11 # curl -u <api_key>: https://app.asana.com/api/1.0/workspaces
a610f65 @rajiv update asana script to show task names.
rajiv authored
12 # (note the colon after the api key)
13 #
8f88b4e @abh1nav Adding some documentation
abh1nav authored
14 # HUBOT_ASANA_PROJECT_ID - list all projects in the workspace using:
15 # curl -u <api_key>: https://app.asana.com/api/1.0/workspaces/<workspace id>/projects
0fd9560 @tombell Update "a" scripts help comments
tombell authored
16 #
17 # Commands:
18 # todo: @name? <task directive> - public message starting with todo: will add task, optional @name to assign task
19 # hubot todo users - Message the bot directly to list all available users in the workspace
a610f65 @rajiv update asana script to show task names.
rajiv authored
20 #
0fd9560 @tombell Update "a" scripts help comments
tombell authored
21 # Author:
22 # idpro
8f88b4e @abh1nav Adding some documentation
abh1nav authored
23 # abh1nav
a610f65 @rajiv update asana script to show task names.
rajiv authored
24 # rajiv
924e739 adding Asana task management script
idpro authored
25
26 url = 'https://app.asana.com/api/1.0'
27
2968643 @abh1nav Switching auth from username+pwd to API key
abh1nav authored
28 workspace = process.env.HUBOT_ASANA_WORKSPACE_ID
29 project = process.env.HUBOT_ASANA_PROJECT_ID
30 api_key = process.env.HUBOT_ASANA_API_KEY
31
924e739 adding Asana task management script
idpro authored
32
33 getRequest = (msg, path, callback) ->
34 msg.http("#{url}#{path}")
32e8cb5 @ngs Use ScopedClient:auth instead of building Authorization header
ngs authored
35 .headers("Accept": "application/json")
36 .auth(api_key, '')
924e739 adding Asana task management script
idpro authored
37 .get() (err, res, body) ->
38 callback(err, res, body)
39
40 postRequest = (msg, path, params, callback) ->
41 stringParams = JSON.stringify params
42 msg.http("#{url}#{path}")
32e8cb5 @ngs Use ScopedClient:auth instead of building Authorization header
ngs authored
43 .headers("Content-Length": stringParams.length, "Accept": "application/json")
44 .auth(api_key, '')
924e739 adding Asana task management script
idpro authored
45 .post(stringParams) (err, res, body) ->
46 callback(err, res, body)
47
48 addTask = (msg, taskName, path, params, userAcct) ->
49 postRequest msg, '/tasks', params, (err, res, body) ->
50 response = JSON.parse body
51 if response.data.errors
52 msg.send response.data.errors
53 else
54 projectId = response.data.id
55 params = {data:{project: "#{project}"}}
56 postRequest msg, "/tasks/#{projectId}/addProject", params, (err, res, body) ->
57 response = JSON.parse body
58 if response.data
59 if userAcct
60 msg.send "Task Created : #{taskName} : Assigned to @#{userAcct}"
61 else
62 msg.send "Task Created : #{taskName}"
63 else
64 msg.send "Error creating task."
65
66 module.exports = (robot) ->
67 # Add a task
68 robot.hear /^(todo|task):\s?(@\w+)?(.*)/i, (msg) ->
69 taskName = msg.match[3]
70 userAcct = msg.match[2] if msg.match[2] != undefined
71 params = {data:{name: "#{taskName}", workspace: "#{workspace}"}}
72 if userAcct
73 userAcct = userAcct.replace /^\s+|\s+$/g, ""
74 userAcct = userAcct.replace "@", ""
7706265 @buley allow uppercase names in asana task assignments
buley authored
75 userAcct = userAcct.toLowerCase()
924e739 adding Asana task management script
idpro authored
76 getRequest msg, "/workspaces/#{workspace}/users", (err, res, body) ->
77 response = JSON.parse body
78 assignedUser = ""
79 for user in response.data
80 name = user.name.toLowerCase().split " "
81 if userAcct == name[0] || userAcct == name[1]
82 assignedUser = user.id
83 if assignedUser != ""
84 params = {data:{name: "#{taskName}", workspace: "#{workspace}", assignee: "#{assignedUser}"}}
85 addTask msg, taskName, '/tasks', params, userAcct
86 else
87 msg.send "Unable to Assign User"
88 addTask msg, taskName, '/tasks', params, false
89 else
90 addTask msg, taskName, '/tasks', params, false
91
a610f65 @rajiv update asana script to show task names.
rajiv authored
92 # show task title
93 robot.hear /https:\/\/app\.asana\.com\/(\d+)\/(\d+)\/(\d+)/, (msg) ->
94 taskId = msg.match[3]
95 getRequest msg, "/tasks/#{taskId}", (err, res, body) ->
96 response = JSON.parse body
97 name = response.data.name
98 msg.send "#{taskId}: #{name}"
99
924e739 adding Asana task management script
idpro authored
100 # List all Users
101 robot.respond /(todo users)/i, (msg) ->
102 getRequest msg, "/workspaces/#{workspace}/users", (err, res, body) ->
103 response = JSON.parse body
104 userList = ""
105 for user in response.data
106 userList += "#{user.id} : #{user.name}\n"
107
0fd9560 @tombell Update "a" scripts help comments
tombell authored
108 msg.send userList
Something went wrong with that request. Please try again.