首页 > 解决方案 > Slack 没有为斜杠命令正确格式化我的 JSON

问题描述

我正在使用 Ruby 在 Slack 和 Build kite 之间设置自定义斜杠命令。我得到了正确的 API 调用,并且正在传递信息。一切似乎都正常,但是当我尝试设置收到的 json 有效负载时,我似乎无法使用 Slacks 的新格式。我让它使用旧格式,但是当我切换到新格式时,它会中断并给我一个字符串。

我尝试解析 json,确保它有效,并使用其他方法,如 response.body。

此 JSON 将生成一个字符串:

   [{
          "type":"section",
          "text":
              {
                "type":"mrkdwn",
                "text":"*Deploy History:* Deploys in the last 6 hours"
              }
    }]

此 JSON 将被正确格式化:

  {
    text: "",
    attachments: [{
      title: 'Deploy History',
      text: 'Deploys in the last 6 hours',
      :fields => [{
        :title => 'Message',
        :value => build_list[0].message,
        :short => true
      }, {
        :title => 'Name',
        :value => build_list[0].creator.name,
        :short => true
      }, {
        :title => 'Finished at',
        :value => build_list[0].finished_at,
        :short => true
      }],
        color: 'good'
    }]
  }

第一个代码的预期结果看起来很漂亮,但它只是把它吐出来: {"type":"section","text":{"type":"mrkdwn","text":" Deploy History: Deploys在过去六个小时内"}}]

工作底部代码的结果很好很漂亮

发送斜杠命令时调用的文件

module SlackLine::Commands
  module DeployHistory extend self

    def deploy_history
      build_list = buildkite.process_history_event
      presenter.deploy_history(build_list)
    end

    def presenter
      SlackLine::Presenters::DeployHistoryPresenter
    end

    def buildkite
      SlackLine::Services::BuildkiteHistoryService
    end
  end
end

这是我们对 buildkite 进行 api 调用的文件

    def deploy_stats
      finished_from = 6.hours.ago.to_time.iso8601
      options = {
        branch: 'master',
        finished_from: finished_from,
        state: 'finished'
      }
      build_list = client.pipeline_builds('calendly', 'calendly', options)
      build_list
    end

标签: ruby-on-railsjsonslackslack-api

解决方案


解决了!我所要做的就是添加一个块。感谢您对我的包容,我希望这对某人有所帮助!

      {
        "blocks": [
          {
            "type": "section",
            "text": {
              "type": "mrkdwn",
              "text": "Deploys in the last six hours"
            }
          }

推荐阅读