小编use*_*326的帖子

使用块布局在 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);
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

{"blocks":{"type":"mrkdwn","text":"Danny Torrence left the following review for your property"}}
Run Code Online (Sandbox Code Playgroud)

但是,Slack 需要以下格式的 JSON:

{"blocks":["type":"mrkdwn","text":"Danny Torrence left the following review for your property"]}
Run Code Online (Sandbox Code Playgroud)

我只需要最后将一个 '{' 转换为 '[' 和一个 '}' 到 ']'。我将不胜感激任何帮助。

谢谢

php json slack-api

1
推荐指数
1
解决办法
6481
查看次数

标签 统计

json ×1

php ×1

slack-api ×1