Skip to content

Commit

Permalink
Basic webhook functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
scrummer committed Nov 26, 2018
1 parent ad0088d commit c1e12d8
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/ISSUE_TEMPLATE.md
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.
-->
12 changes: 12 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
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.
-->
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.idea/
/tmp/
/src/vendor/
/composer.lock
9 changes: 9 additions & 0 deletions README.md
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
```
26 changes: 26 additions & 0 deletions composer.json
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/"
}
}
}
174 changes: 174 additions & 0 deletions src/DiscordWebhook/Webhook.php
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;
}
}

0 comments on commit c1e12d8

Please sign in to comment.