【问题标题】:Hubot Scripts integrating with AsanaHubot Scripts 与 Asana 集成
【发布时间】:2012-11-16 00:44:03
【问题描述】:

我正在制作我的第一个 Hubot 脚本,它将为 Asana 添加一个快速任务。
我不打算做任何太疯狂的事情,或者至少不认为我是。

目前我有

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

我真正想要它做的只是输出它发布到工作区。我知道 Asana API 有更多功能可以让它正常工作,但是看着我的日志尾部,没有任何输出,没有任何内容记录到控制台,没有任何事情发生。

如果我在参数下做一个console.log,它会输出JSON并且它是正确的,但似乎这个帖子永远不会发生。

任何方向都会很棒!

谢谢。

编辑

经过更多调整,跟随 Dan 是朝着正确方向迈出的一步,删除 .query() 并将字符串放入 .post() 中,输出终于正确了。

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

【问题讨论】:

  • 别忘了它必须在顶级对象的data 参数中。 {data:{parameters:values}}
  • 感谢@DanRedux,我试了一下,但没有改变任何东西。 :-/
  • 经过一番调整,终于成功了。谢谢指点!!

标签: api node.js coffeescript asana hubot


【解决方案1】:

提交问题的答案,以便 stackoverflow 不会将其显示为未回答。

从 OP 中的 EDIT 复制。

删除 .query() 并将字符串放入 .post() 输出最终是正确的。

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

【讨论】:

    【解决方案2】:

    我认为 Hubot 中的 http 客户端需要 query() 的对象,而不是字符串。尝试直接传入对象而不是调用JSON.stringify

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 2018-08-09
      相关资源
      最近更新 更多