diff --git a/src/Discord.php b/src/Discord.php index 6f1438f..61b6d18 100644 --- a/src/Discord.php +++ b/src/Discord.php @@ -86,7 +86,7 @@ protected function request($verb, $endpoint, array $data) 'headers' => [ 'Authorization' => 'Bot '.$this->token, ], - 'json' => $data, + 'multipart' => $data, ]); } catch (RequestException $exception) { if ($response = $exception->getResponse()) { diff --git a/src/DiscordChannel.php b/src/DiscordChannel.php index 7364192..ffaddaa 100644 --- a/src/DiscordChannel.php +++ b/src/DiscordChannel.php @@ -36,9 +36,15 @@ public function send($notifiable, Notification $notification) } $message = $notification->toDiscord($notifiable); + $formData = $this->toFormData($message); + return $this->discord->send($channel, $formData); + } + + private function toFormData(DiscordMessage $message): array + { $data = [ - 'content' => $message->body + 'content' => $message->body, ]; if (count($message->embed) > 0) { @@ -49,6 +55,20 @@ public function send($notifiable, Notification $notification) $data['components'] = $message->components; } - return $this->discord->send($channel, $data); + $formData = [ + [ + 'name' => 'payload_json', + 'contents' => json_encode($data), + ] + ]; + + foreach ($message->files as $i => $file) { + $formData[] = [ + 'name' => 'files[' . $i . ']', + 'contents' => $file, + ]; + } + + return $formData; } } diff --git a/src/DiscordMessage.php b/src/DiscordMessage.php index 1985993..eab7c89 100644 --- a/src/DiscordMessage.php +++ b/src/DiscordMessage.php @@ -25,26 +25,36 @@ class DiscordMessage */ public $components; + /** + * The files to be attached to the message. + * + * @var array + */ + public $files; + /** * @param string $body * @param array|null $embed * * @return static */ - public static function create($body = '', $embed = [], $components = []) + public static function create($body = '', $embed = [], $components = [], $files = []) { - return new static($body, $embed, $components); + return new static($body, $embed, $components, $files); } /** * @param string $body * @param array $embed + * @param array $components + * @param array $files */ - public function __construct($body = '', $embed = [], $components = []) + public function __construct($body = '', $embed = [], $components = [], $files = []) { $this->body = $body; $this->embed = $embed; $this->components = $components; + $this->files = $files; } /** @@ -63,7 +73,7 @@ public function body($body) /** * Set a single embedded object. - * + * * TODO: Refactor to enable multiple embeds. * See https://discord.com/developers/docs/resources/channel#create-message * @@ -91,4 +101,18 @@ public function components($components) return $this; } + + /** + * Set the files object. + * + * @param array $files + * + * @return $this + */ + public function files($files) + { + $this->files = $files; + + return $this; + } }