Skip to content

Commit bed8c37

Browse files
committed
#10 Simple Webhooks
1 parent d31d0fa commit bed8c37

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

src/DiscordWebhook/EmbedColor.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
namespace DiscordWebhook;
55

6+
/**
7+
* Enum EmbedColor
8+
*
9+
* @author Scrummer <[email protected]>
10+
* @package DiscordWebhook
11+
*/
612
enum EmbedColor: int
713
{
814
case DEFAULT = 0;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace DiscordWebhook\Exception;
5+
6+
use RuntimeException;
7+
8+
/**
9+
* Class DiscordWebhookException
10+
*
11+
* @package DiscordWebhook\Exception
12+
*/
13+
class DiscordWebhookException extends RuntimeException
14+
{
15+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace DiscordWebhook;
5+
6+
use DiscordWebhook\Exception\DiscordWebhookException;
7+
use GuzzleHttp\Exception\GuzzleException;
8+
use Symfony\Component\Serializer\Exception\ExceptionInterface;
9+
10+
/**
11+
* Class SimpleWebhook
12+
*
13+
* @author Scrummer <[email protected]>
14+
* @package DiscordWebhook
15+
*/
16+
class SimpleWebhook extends Webhook
17+
{
18+
private static ?string $defaultUrl = null;
19+
20+
private static ?SimpleWebhook $instance = null;
21+
22+
/**
23+
* Send a message instantly.
24+
*
25+
* @param string $message The message to be sent
26+
* @param string|null $url A specific url if you want to override the default one
27+
*
28+
* @throws ExceptionInterface
29+
* @throws GuzzleException
30+
*/
31+
public static function sendMessage(string $message, string $url = null): bool
32+
{
33+
if (self::$instance === null) {
34+
self::$instance = self::createInstance($url);
35+
}
36+
37+
return self::$instance->setMessage($message)->send();
38+
}
39+
40+
private static function createInstance(?string $url = null): SimpleWebhook
41+
{
42+
self::$defaultUrl = $url;
43+
44+
if (self::$defaultUrl === null && array_key_exists('DWH_DEFAULT_URL', $_ENV)) {
45+
self::$defaultUrl = $_ENV['DWH_DEFAULT_URL'] ?? null;
46+
}
47+
48+
if (self::$defaultUrl === null) {
49+
throw new DiscordWebhookException('No default URL has been set. Either add the `DWH_DEFAULT_URL` env variable or pass it to the `sendMessage` method.');
50+
}
51+
52+
return new self(self::$defaultUrl);
53+
}
54+
}

tests/test.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
exit(1);
1616
}
1717

18+
// TEST simple webhook
19+
\DiscordWebhook\SimpleWebhook::sendMessage('Test simple WH', 'https://canary.discord.com/api/webhooks/422789288565014530/DbUOQNLXS8-BN9eFkVKCAdvnp2dNpRMebRAOXZEWU0bLx70t689t7QYrG5S4oibNIWI0');
20+
21+
exit;
22+
1823
// CONFIGURE your Webhook below to test
1924
$wh = new \DiscordWebhook\Webhook([
2025
$_ENV['DISCORD_TEST_URL']

0 commit comments

Comments
 (0)