ruby-on-rails - 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
解决方案
解决了!我所要做的就是添加一个块。感谢您对我的包容,我希望这对某人有所帮助!
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Deploys in the last six hours"
}
}
推荐阅读
- c# - 如何在服务创建期间将 Windows 服务启动类型设置为自动(延迟启动)?
- javascript - 更新开普勒地图中的数据 addDataToMap
- excel - Real-Statistics Excel Add-on LJUNG 公式
- azure - CLI 命令提供来自订阅的资源列表,其中用户是所有者角色
- google-cloud-vision - 谷歌对象本地化中使用的标签列表
- wordpress - 子域重定向到域
- google-cloud-platform - 无法打开当前工作目录的路径:尝试在 gcloud 中运行 rsync 命令两次时权限被拒绝
- java - 使用 SpringBoot 无法访问 H2 数据库控制台
- string - Bash 字符串操作
- angular - 为什么我的 observable 没有返回值?