Skip to content

Commit c1e12d8

Browse files
committed
Basic webhook functionality
1 parent ad0088d commit c1e12d8

File tree

6 files changed

+237
-0
lines changed

6 files changed

+237
-0
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
| Q | A
2+
| ---------------- | -----
3+
| Bug report? | yes/no
4+
| Feature request? | yes/no
5+
| BC Break report? | yes/no
6+
| RFC? | yes/no
7+
8+
<!--
9+
- Please fill in this template according to your issue.
10+
- For support request or how-tos, visit https://discordapp.com or https://labymod.net/devcord
11+
- Otherwise, replace this comment by the description of your issue.
12+
-->

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
| Q | A
2+
| ------------- | ---
3+
| Bug fix? | yes/no
4+
| New feature? | yes/no
5+
| BC breaks? | no
6+
| Deprecations? | yes/no
7+
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->
8+
9+
<!--
10+
Write a short README entry for your feature/bugfix here (replace this comment block.)
11+
This will help people understand your PR and can be used as a start of the Doc PR.
12+
-->

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.idea/
2+
/tmp/
3+
/src/vendor/
4+
/composer.lock

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Discord Webhook
2+
Send Discord messages directly from your PHP application. Even with embeds & files!
3+
4+
## Installation
5+
6+
**Composer installation**
7+
```bash
8+
composer require labymod/discord-webhook
9+
```

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "labymod/discord-webhook",
3+
"description": "A lightweight library for Discord™ Webhooks",
4+
"type": "library",
5+
"license": "GPL-3.0",
6+
"authors": [
7+
{
8+
"name": "LabyMod Scrummer",
9+
"email": "[email protected]",
10+
"homepage": "https://labymod.net",
11+
"role": "Developer"
12+
}
13+
],
14+
"require": {
15+
"php": ">=7.0",
16+
"guzzlehttp/guzzle": "~6.3.3"
17+
},
18+
"config": {
19+
"vendor-dir": "src/vendor"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"DiscordWebhook\\": "src/DiscordWebhook/"
24+
}
25+
}
26+
}

src/DiscordWebhook/Webhook.php

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace DiscordWebhook;
5+
6+
use GuzzleHttp\Client;
7+
use GuzzleHttp\Exception\GuzzleException;
8+
9+
/**
10+
* Class Webhook
11+
*
12+
* @author Scrummer <[email protected]>
13+
* @package DiscordWebhook
14+
*/
15+
class Webhook
16+
{
17+
/**
18+
* @var Client
19+
*/
20+
private $client;
21+
22+
/**
23+
* @var string
24+
*/
25+
private $username;
26+
27+
/**
28+
* @var string
29+
*/
30+
private $avatar;
31+
32+
/**
33+
* @var string
34+
*/
35+
private $message;
36+
37+
/**
38+
* @var bool
39+
*/
40+
private $tts;
41+
42+
/**
43+
* @var array
44+
*/
45+
private $file;
46+
47+
/**
48+
* Constructor.
49+
*
50+
* @param string $url
51+
*/
52+
public function __construct(string $url)
53+
{
54+
$this->client = new Client([
55+
'base_uri' => $url
56+
]);
57+
}
58+
59+
/**
60+
* Send the Webhook
61+
*
62+
* @return bool
63+
* @throws GuzzleException
64+
*/
65+
public function send(): bool
66+
{
67+
$response = $this->client->request(
68+
'POST',
69+
$this->client->getConfig('base_uri')->getPath(),
70+
$this->buildPayload()
71+
);
72+
73+
return $response->getStatusCode() === 200;
74+
}
75+
76+
private function buildPayload(): array
77+
{
78+
$fields = [
79+
'username' => 'username',
80+
'avatar' => 'avatar_url',
81+
'message' => 'content',
82+
'tts' => 'tts',
83+
'file' => 'file',
84+
'embeds' => 'embeds'
85+
];
86+
$payload = [
87+
'multipart' => []
88+
];
89+
90+
foreach ($fields as $field => $payloadField) {
91+
if (!property_exists($this, $field) || null === $this->$field) {
92+
continue;
93+
}
94+
95+
if (is_string($this->$field) || is_bool($this->$field)) { // add string and booloan values
96+
$payload['multipart'][] = [
97+
'name' => $payloadField,
98+
'contents' => $this->$field
99+
];
100+
} elseif ($this->$field instanceof \SplFileInfo) { // add file
101+
/** @var \SplFileInfo $file */
102+
$file = $this->$field;
103+
104+
$payload['multipart'][] = [
105+
'name' => $payloadField,
106+
'contents' => $file->openFile()->fread($file->getSize()),
107+
'filename' => $file->getFilename()
108+
];
109+
}
110+
}
111+
112+
return $payload;
113+
}
114+
115+
/**
116+
* @param bool $tts
117+
*
118+
* @return Webhook
119+
*/
120+
public function setTts(bool $tts = false): self
121+
{
122+
$this->tts = $tts;
123+
124+
return $this;
125+
}
126+
127+
/**
128+
* @param string $username
129+
*
130+
* @return Webhook
131+
*/
132+
public function setUsername(string $username): self
133+
{
134+
$this->username = $username;
135+
136+
return $this;
137+
}
138+
139+
/**
140+
* @param string $url
141+
*
142+
* @return Webhook
143+
*/
144+
public function setAvatar(string $url): self
145+
{
146+
$this->avatar = $url;
147+
148+
return $this;
149+
}
150+
151+
/**
152+
* @param string $message
153+
*
154+
* @return Webhook
155+
*/
156+
public function setMessage(string $message): self
157+
{
158+
$this->message = $message;
159+
160+
return $this;
161+
}
162+
163+
/**
164+
* @param \SplFileInfo $file
165+
*
166+
* @return Webhook
167+
*/
168+
public function setFile(\SplFileInfo $file): self
169+
{
170+
$this->file = $file;
171+
172+
return $this;
173+
}
174+
}

0 commit comments

Comments
 (0)