Discord Slack 兼容网络钩子

Discord Slack compatible webhook

作为 per Discord's documentation 你可以使用 Slack 的 webhook 格式。

在 Laravel 应用程序中,我目前拥有:

$blocks = [
  [
    'type' => 'section',
    'text' => [
      'type' => 'plain_text',
      'text' => 'test',
    ]
  ]
];

$response = Http::post('https://discordapp.com/api/webhooks/.../.../slack', [
  'text' => 'Title',
  'blocks' => json_encode($blocks),
]);

但只有 'Title' 出现在消息中,而不是 $blocks 内容。 Discord 文档没有说明这不受支持,他们参考了 the Slack webhook docs,其中清楚地说明了如何使用块构建消息。我错过了什么?

Http 会自动将对象/数组转换为有效的 JSON。所以删除 json_encode() 应该可以解决你的问题。

$response = Http::post('https://discordapp.com/api/webhooks/.../.../slack', [
  'text' => 'Title',
  'blocks' => $blocks,
]);