Search code examples
laraveldiscordslack

Discord Slack compatible webhook


As per Discord's documentation you can use Slack's webhook format.

In a Laravel app I currently have:

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

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

But only 'Title' ends up in the message, not the $blocks content. The Discord documentation doesn't state that this is not supported, and they refer to the Slack webhook docs where it's clearly stated how you build a message using blocks. What am I missing?


Solution

  • Http will automatically convert objects / arrays to valid JSON. So removing json_encode() should help your problem.

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