2017-06-29 86 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 

有關進一步信息如下裁判: