Question

I am working on making my first Hubot script which would add a quick task to Asana.
I'm not looking at doing anything too crazy, or at least didn't think I was.

Currently I have

url  = 'https://app.asana.com/api/1.0'

WORKSPACE = "1111111111111"
user = "xxxxxx.xxxxxxxxxxxxxxxx"
pass = ""

module.exports = (robot) ->
  robot.respond /task (.*)/i, (msg) ->
    params = {name: "#{msg.match[1]}", workspace: "#{WORKSPACE}"}
    stringParams = JSON.stringify params
    auth = 'Basic ' + new Buffer("#{user}:#{pass}").toString('base64')
    msg.http("#{url}/tasks")
      .headers("Authorization": auth, "Content-Length": stringParams.length, "Accept": "application/json")
      .query(params)
      .post() (err, res, body) ->
        console.log(err)
        console.log(res)
        console.log(body)
        msg.send body

All I really want it to do is output that it's posting to the workspace. I know there is more to the Asana API to make it all work properly, but watching my log's tail, nothing is outputting, nothing is logging to console, nothing is happening.

If I do a console.log under the params, it will output the JSON and it's correct, but it seems like the post is never happening.

Any direction would be great!

Thanks.

EDIT

After some more tweaking, following Dan was a step in the right direction, dropping .query() and putting the string in .post() the output is finally correct.

module.exports = (robot) ->
  robot.respond /task (.*)/i, (msg) ->
    params = {data:{name: "#{msg.match[1]}", workspace: "#{WORKSPACE}"}}
    stringParams = JSON.stringify params
    auth = 'Basic ' + new Buffer("#{user}:#{pass}").toString('base64')
    msg.http("#{url}/tasks")
      .headers("Authorization": auth, "Content-Length": stringParams.length, "Accept": "application/json")
      .post(stringParams) (err, res, body) ->
        console.log(err)
        console.log(res)
        console.log(body)
        msg.send body
Was it helpful?

Solution

Submitting an answer to the question so stackoverflow won't show it as unanswered.

Copying from EDIT in OP.

dropping .query() and putting the string in .post() the output is finally correct.

module.exports = (robot) ->
  robot.respond /task (.*)/i, (msg) ->
    params = {data:{name: "#{msg.match[1]}", workspace: "#{WORKSPACE}"}}
    stringParams = JSON.stringify params
    auth = 'Basic ' + new Buffer("#{user}:#{pass}").toString('base64')
    msg.http("#{url}/tasks")
      .headers("Authorization": auth, "Content-Length": stringParams.length, "Accept": "application/json")
      .post(stringParams) (err, res, body) ->
        console.log(err)
        console.log(res)
        console.log(body)
        msg.send body

OTHER TIPS

I think the http client in Hubot expects an object for query(), not a string. Try passing the object directly in instead of calling JSON.stringify.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top