2018-02-23 16:13:27 +01:00
|
|
|
<?php
|
2018-08-05 00:05:14 +02:00
|
|
|
namespace App\Webhook\Connector;
|
2018-02-23 16:13:27 +01:00
|
|
|
|
2018-08-05 00:05:14 +02:00
|
|
|
use App\Entity;
|
2018-09-21 14:04:05 +02:00
|
|
|
use App\Event\SendWebhooks;
|
2018-02-23 16:13:27 +01:00
|
|
|
use GuzzleHttp\Exception\TransferException;
|
2018-02-27 07:28:09 +01:00
|
|
|
use Monolog\Logger;
|
2018-02-23 16:13:27 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* https://discordapp.com/developers/docs/resources/webhook#execute-webhook
|
|
|
|
*
|
|
|
|
* JSON/Form Params
|
|
|
|
* content string the message contents (up to 2000 characters) one of content, file, embeds
|
|
|
|
* username string override the default username of the webhook false
|
|
|
|
* avatar_url string override the default avatar of the webhook false
|
|
|
|
* tts bool true if this is a TTS message false
|
|
|
|
* file file contents the contents of the file being sent one of content, file, embeds
|
|
|
|
* embeds array of embed objects embedded rich content one of content, file, embeds
|
|
|
|
*
|
|
|
|
* Embed Structure
|
|
|
|
* title string title of embed
|
|
|
|
* type string type of embed (always "rich" for webhook embeds)
|
|
|
|
* description string description of embed
|
|
|
|
* url string url of embed
|
|
|
|
* timestamp ISO8601 timestamp timestamp of embed content
|
|
|
|
* color integer color code of the embed
|
|
|
|
* footer embed footer object footer information
|
|
|
|
* image embed image object image information
|
|
|
|
* thumbnail embed thumbnail object thumbnail information
|
|
|
|
* video embed video object video information
|
|
|
|
* provider embed provider object provider information
|
|
|
|
* author embed author object author information
|
|
|
|
* fields array of embed field objects fields information
|
|
|
|
*
|
|
|
|
* Embed Footer Structure
|
|
|
|
* text string footer text
|
|
|
|
* icon_url string url of footer icon (only supports http(s) and attachments)
|
|
|
|
* proxy_icon_url string a proxied url of footer icon
|
|
|
|
*
|
|
|
|
* Embed Thumbnail Structure
|
|
|
|
* url string source url of thumbnail (only supports http(s) and attachments)
|
|
|
|
* proxy_url string a proxied url of the thumbnail
|
|
|
|
* height integer height of thumbnail
|
|
|
|
* width integer width of thumbnail
|
|
|
|
*
|
|
|
|
* Embed Provider Structure
|
|
|
|
* name string name of provider
|
|
|
|
* url string url of provider
|
|
|
|
*
|
|
|
|
* Embed Footer Structure
|
|
|
|
* text string footer text
|
|
|
|
* icon_url string url of footer icon (only supports http(s) and attachments)
|
|
|
|
* proxy_icon_url string a proxied url of footer icon
|
|
|
|
*
|
|
|
|
* Embed Field Structure
|
|
|
|
* name string name of the field
|
|
|
|
* value string value of the field
|
|
|
|
* inline bool whether or not this field should display inline
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
class Discord extends AbstractConnector
|
|
|
|
{
|
2018-09-21 14:04:05 +02:00
|
|
|
public function dispatch(SendWebhooks $event, array $config): void
|
2018-02-23 16:13:27 +01:00
|
|
|
{
|
2018-02-26 04:07:40 +01:00
|
|
|
$webhook_url = $this->_getValidUrl($config['webhook_url'] ?? '');
|
|
|
|
|
|
|
|
if (empty($webhook_url)) {
|
2018-08-12 08:35:48 +02:00
|
|
|
$this->logger->error('Webhook '.$this->_getName().' is missing necessary configuration. Skipping...');
|
2018-02-23 16:13:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$raw_vars = [
|
|
|
|
'content' => $config['content'] ?? '',
|
|
|
|
'title' => $config['title'] ?? '',
|
|
|
|
'description' => $config['description'] ?? '',
|
|
|
|
'url' => $config['url'] ?? '',
|
|
|
|
'author' => $config['author'] ?? '',
|
|
|
|
'thumbnail' => $config['thumbnail'] ?? '',
|
|
|
|
'footer' => $config['footer'] ?? '',
|
|
|
|
];
|
|
|
|
|
2018-09-21 14:04:05 +02:00
|
|
|
$vars = $this->_replaceVariables($raw_vars, $event->getNowPlaying());
|
2018-02-23 16:13:27 +01:00
|
|
|
|
|
|
|
// Compose webhook
|
|
|
|
$embed = [
|
|
|
|
'title' => $vars['title'] ?? '',
|
|
|
|
'description' => $vars['description'] ?? '',
|
2018-02-26 04:07:40 +01:00
|
|
|
'url' => $this->_getValidUrl($vars['url']) ?? '',
|
2018-02-23 16:13:27 +01:00
|
|
|
'color' => 2201331, // #2196f3
|
|
|
|
];
|
|
|
|
$embed = array_filter($embed);
|
|
|
|
|
|
|
|
if (!empty($vars['author'])) {
|
|
|
|
$embed['author'] = [
|
|
|
|
'name' => $vars['author'],
|
|
|
|
];
|
|
|
|
}
|
2018-02-26 04:07:40 +01:00
|
|
|
if (!empty($vars['thumbnail']) && $this->_getValidUrl($vars['thumbnail'])) {
|
2018-02-23 16:13:27 +01:00
|
|
|
$embed['thumbnail'] = [
|
2018-02-26 04:07:40 +01:00
|
|
|
'url' => $this->_getValidUrl($vars['thumbnail']),
|
2018-02-23 16:13:27 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
if (!empty($vars['footer'])) {
|
|
|
|
$embed['footer'] = [
|
|
|
|
'text' => $vars['footer'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-03-10 01:30:55 +01:00
|
|
|
$webhook_body = [];
|
|
|
|
$webhook_body['content'] = $vars['content'] ?? '';
|
|
|
|
|
|
|
|
// Don't include an embed if all relevant fields are empty.
|
|
|
|
if (count($embed) > 1) {
|
|
|
|
$webhook_body['embeds'] = [$embed];
|
|
|
|
}
|
2018-02-23 16:13:27 +01:00
|
|
|
|
|
|
|
// Dispatch webhook
|
2018-02-27 07:28:09 +01:00
|
|
|
$this->logger->debug('Dispatching Discord webhook...');
|
2018-02-23 16:13:27 +01:00
|
|
|
|
|
|
|
try {
|
2018-09-07 03:08:47 +02:00
|
|
|
$response = $this->http_client->request('POST', $webhook_url, [
|
2018-02-23 16:13:27 +01:00
|
|
|
'headers' => [
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
],
|
|
|
|
'json' => $webhook_body,
|
|
|
|
]);
|
|
|
|
|
2018-02-27 07:28:09 +01:00
|
|
|
$this->logger->addRecord(
|
|
|
|
($response->getStatusCode() !== 204 ? Logger::ERROR : Logger::DEBUG),
|
2018-08-12 08:35:48 +02:00
|
|
|
sprintf('Webhook %s returned code %d', $this->_getName(), $response->getStatusCode()),
|
2018-02-27 07:28:09 +01:00
|
|
|
['message_sent' => $webhook_body, 'response_body' => $response->getBody()->getContents()]
|
|
|
|
);
|
2018-02-23 16:13:27 +01:00
|
|
|
} catch(TransferException $e) {
|
2018-02-27 07:28:09 +01:00
|
|
|
$this->logger->error(sprintf('Error from Discord (%d): %s', $e->getCode(), $e->getMessage()));
|
2018-02-23 16:13:27 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-05 00:05:14 +02:00
|
|
|
}
|