Skip to content

Suggestion - Add message patch #5

@chrisbadley

Description

@chrisbadley

By adding this function to DiscordWebhook.php gives the ability to edit existing messages

public function patch(): bool
    {
        try {
            $ch = curl_init($this->webhookUrl);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $this->message->toJson());
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($this->message->toJson())
            ));
            $response = curl_exec($ch);
            $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
            if ($responseCode >= 200 && $responseCode < 300) {
                return true;
            } else {
                $decodedResponse = json_decode($response, true);
                throw new InvalidResponseException($decodedResponse["message"] ?? "", $responseCode);
            }
        } catch (\Exception $e) {
            echo $e->getMessage();
            return false;
        }
    }

Usage example:

    if ($postedID == '') {
        $webhook->setWebhookUrl($WebHookUrl);
        $webhook->send();
    } else {
        $webhook->setWebhookUrl($WebHookUrl . "/messages/" . $postedID);
        $webhook->patch();
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions