【问题标题】:How to get JSON output with Coffeescript?如何使用 Coffeescript 获取 JSON 输出?
【发布时间】:2017-12-03 06:48:09
【问题描述】:

我已将 Hubot 与 elasticsearch 和 slack 集成。 当我们在 API 上查询时,当我们使用 postman 时,我们得到的是 JSON 格式的输出。

当我们使用 coffeescript 从 slack 查询时,我们得到的是简单的输出。 这是代码

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}```")

这是在 slack 中打印我的普通输出。 能否请任何人帮助我如何更改代码以将输出打印为 JSON 格式?

【问题讨论】:

    标签: json coffeescript slack-api hubot


    【解决方案1】:

    我相信您需要指定"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
    

    有关更多信息,请参阅以下参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多