php - 使用块布局在 Slack 中格式化消息
问题描述
我想使用块布局格式向我的 slack 应用程序发送消息。我在 PHP 中创建了一个关联数组,然后使用 json_encode() 将其转换为 JSON。问题是它没有转换为 slack 所期望的 JSON 格式,并且我收到错误“无效的块格式”。这是我的代码,输出和 slack 期望的输出。
$data = array(
'blocks' => array(
'type' => 'mrkdwn',
'text' => 'Danny Torrence left the following review for your property'
),
);
$data = json_encode($data);
我得到以下输出:
{"blocks":{"type":"mrkdwn","text":"Danny Torrence left the following review for your property"}}
但是,Slack 期望 JSON 采用以下格式:
{"blocks":["type":"mrkdwn","text":"Danny Torrence left the following review for your property"]}
最后,我只需要将一个 '{' 转换为 '[' 和一个 '}' 转换为 ']'。我将不胜感激任何帮助。
谢谢
解决方案
我没有足够的声誉,但我相信这是重复的:没有方括号 json 数组
此外,这不是有效的 json,您可以查看https://jsonlint.com/?code=
{"blocks":["type":"mrkdwn","text":"Danny Torrence left the following review for your property"]}
总结这篇文章,你真正需要做的就是用另一个数组包装你的内部数组
$data = array(
'blocks' => array(array(
'type' => 'mrkdwn',
'text' => 'Danny Torrence left the following review for your property'
)),
);
这返回:
{"blocks":[{"type":"mrkdwn","text":"Danny Torrence left the following review for your property"}]}
推荐阅读
- javascript - 初始化 PouchDB 失败
- sql - 将日期时间转换为 UTC 开始时间 - Oracle db
- android - Android 内置电话计时器应用程序(时钟)在代码中的使用
- javascript - 在反应路由器路由路径道具中使用正则表达式
- angular - 角度前端中的 JSON
- python - 将 keras h5 转换为 tensorflow pb 以进行批量推理
- wheelnav.js - 如何使用wheelnav js沿路径弯曲navtitle
- javascript - 使用 javascript 捕获 html 标记值
- java - 在 JUnit 测试用例中使用 Try Catch
- r - 找不到对象“pkgInfo”,r