【问题标题】:Slack is not correctly formatting my JSON for a slash commandSlack 没有为斜杠命令正确格式化我的 JSON
【发布时间】:2019-07-02 23:03:59
【问题描述】:

我正在使用 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":"部署历史:在过去六个小时内部署"}}]

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

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

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

【问题讨论】:

  • 您好,欢迎来到 SO。请将代码的相关部分添加到定义块样式消息并将其发送到 Slack 的问题中。
  • 嘿@ErikKalkoken 感谢您的回复!该文件是我们定义使用斜杠命令时要发送的内容的地方。 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
  • 这就是我们得到 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 end end
  • 你能把你的代码放在问题中吗?它真的很难在 cmets 中正确阅读,并且在 cmets 中限制格式也容易出错。谢谢
  • 好了!

标签: ruby-on-rails json slack slack-api


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    相关资源
    最近更新 更多