-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
| Q | A | ||
| ---------------- | ----- | ||
| Bug report? | yes/no | ||
| Feature request? | yes/no | ||
| BC Break report? | yes/no | ||
| RFC? | yes/no | ||
|
||
<!-- | ||
- Please fill in this template according to your issue. | ||
- For support request or how-tos, visit https://discordapp.com or https://labymod.net/devcord | ||
- Otherwise, replace this comment by the description of your issue. | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
| Q | A | ||
| ------------- | --- | ||
| Bug fix? | yes/no | ||
| New feature? | yes/no | ||
| BC breaks? | no | ||
| Deprecations? | yes/no | ||
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | ||
|
||
<!-- | ||
Write a short README entry for your feature/bugfix here (replace this comment block.) | ||
This will help people understand your PR and can be used as a start of the Doc PR. | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/.idea/ | ||
/tmp/ | ||
/src/vendor/ | ||
/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Discord Webhook | ||
Send Discord messages directly from your PHP application. Even with embeds & files! | ||
|
||
## Installation | ||
|
||
**Composer installation** | ||
```bash | ||
composer require labymod/discord-webhook | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "labymod/discord-webhook", | ||
"description": "A lightweight library for Discord™ Webhooks", | ||
"type": "library", | ||
"license": "GPL-3.0", | ||
"authors": [ | ||
{ | ||
"name": "LabyMod Scrummer", | ||
"email": "scrummer@gmx.ch", | ||
"homepage": "https://labymod.net", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.0", | ||
"guzzlehttp/guzzle": "~6.3.3" | ||
}, | ||
"config": { | ||
"vendor-dir": "src/vendor" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"DiscordWebhook\\": "src/DiscordWebhook/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace DiscordWebhook; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
|
||
/** | ||
* Class Webhook | ||
* | ||
* @author Scrummer <scrummer@gmx.ch> | ||
* @package DiscordWebhook | ||
*/ | ||
class Webhook | ||
{ | ||
/** | ||
* @var Client | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $username; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $avatar; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $message; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $tts; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $file; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $url | ||
*/ | ||
public function __construct(string $url) | ||
{ | ||
$this->client = new Client([ | ||
'base_uri' => $url | ||
]); | ||
} | ||
|
||
/** | ||
* Send the Webhook | ||
* | ||
* @return bool | ||
* @throws GuzzleException | ||
*/ | ||
public function send(): bool | ||
{ | ||
$response = $this->client->request( | ||
'POST', | ||
$this->client->getConfig('base_uri')->getPath(), | ||
$this->buildPayload() | ||
); | ||
|
||
return $response->getStatusCode() === 200; | ||
} | ||
|
||
private function buildPayload(): array | ||
{ | ||
$fields = [ | ||
'username' => 'username', | ||
'avatar' => 'avatar_url', | ||
'message' => 'content', | ||
'tts' => 'tts', | ||
'file' => 'file', | ||
'embeds' => 'embeds' | ||
]; | ||
$payload = [ | ||
'multipart' => [] | ||
]; | ||
|
||
foreach ($fields as $field => $payloadField) { | ||
if (!property_exists($this, $field) || null === $this->$field) { | ||
continue; | ||
} | ||
|
||
if (is_string($this->$field) || is_bool($this->$field)) { // add string and booloan values | ||
$payload['multipart'][] = [ | ||
'name' => $payloadField, | ||
'contents' => $this->$field | ||
]; | ||
} elseif ($this->$field instanceof \SplFileInfo) { // add file | ||
/** @var \SplFileInfo $file */ | ||
$file = $this->$field; | ||
|
||
$payload['multipart'][] = [ | ||
'name' => $payloadField, | ||
'contents' => $file->openFile()->fread($file->getSize()), | ||
'filename' => $file->getFilename() | ||
]; | ||
} | ||
} | ||
|
||
return $payload; | ||
} | ||
|
||
/** | ||
* @param bool $tts | ||
* | ||
* @return Webhook | ||
*/ | ||
public function setTts(bool $tts = false): self | ||
{ | ||
$this->tts = $tts; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $username | ||
* | ||
* @return Webhook | ||
*/ | ||
public function setUsername(string $username): self | ||
{ | ||
$this->username = $username; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* | ||
* @return Webhook | ||
*/ | ||
public function setAvatar(string $url): self | ||
{ | ||
$this->avatar = $url; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* | ||
* @return Webhook | ||
*/ | ||
public function setMessage(string $message): self | ||
{ | ||
$this->message = $message; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param \SplFileInfo $file | ||
* | ||
* @return Webhook | ||
*/ | ||
public function setFile(\SplFileInfo $file): self | ||
{ | ||
$this->file = $file; | ||
|
||
return $this; | ||
} | ||
} |