2017-06-29 58 views
0

我已将Hubot与elasticsearch和slack集成。 当我们在API上查询时,我们使用邮递员时以JSON格式获取输出。如何使用Coffeescript获取JSON输出?

当我们使用coffeescript从懈怠中查询时,我们得到的是简单的输出。 下面是代码

showHealth = (msg) -> 
     msg.send("Getting the health for the cluster: ") 
     msg.http("http://show-acc.com/_cluster/health/") 
     .get() (err, res, body) -> 
      lines = body.split("\n") 
      header = lines.shift() 
      list = [header].concat(lines.sort().reverse()).join("\n") 
      msg.send("/code \n ```#{list}```") 

这是印刷我在农闲纯输出。 请问任何人都可以帮助我如何更改代码以JSON格式打印输出?

回答

0

我相信你需要指定"mrkdwn": true,这将允许你使用反引号代码块。然而,IMO实现格式化信息的最好方法是使用附件,其结构与性能的哈希数组...

我也得到了使用robot.messageRoom更多的里程,而不是msg.send,这样的事情:

# Create attachment 
msg = { 
    attachments: [ 
    { 
     fallback: 'Getting the health for the cluster: http://show-acc.com/_cluster/health/' 
     title: 'Getting the health for the cluster:' 
     title_link: 'http://show-acc.com/_cluster/health/' 
     text: '/code \n ```#{list}```' 
     mrkdwn_in: ['text'] 
    } 
    ] 
} 
# Assign channel 
channel = process.env.NOTIFY_ROOM 

# Send it! 
robot.messageRoom channel, msg 

有关进一步信息如下裁判: