getConfig(); $webhook_url = $this->getValidUrl($config['webhook_url'] ?? ''); if (empty($webhook_url)) { $this->logger->error('Webhook ' . self::NAME . ' is missing necessary configuration. Skipping...'); return false; } $raw_vars = [ 'content' => $config['content'] ?? '', 'title' => $config['title'] ?? '', 'description' => $config['description'] ?? '', 'url' => $config['url'] ?? '', 'author' => $config['author'] ?? '', 'thumbnail' => $config['thumbnail'] ?? '', 'footer' => $config['footer'] ?? '', ]; $vars = $this->replaceVariables($raw_vars, $np); // Compose webhook $embed = array_filter( [ 'title' => $vars['title'] ?? '', 'description' => $vars['description'] ?? '', 'url' => $this->getValidUrl($vars['url']) ?? '', 'color' => 2201331, // #2196f3 ] ); if (!empty($vars['author'])) { $embed['author'] = [ 'name' => $vars['author'], ]; } if (!empty($vars['thumbnail']) && $this->getImageUrl($vars['thumbnail'])) { $embed['thumbnail'] = [ 'url' => $this->getImageUrl($vars['thumbnail']), ]; } if (!empty($vars['footer'])) { $embed['footer'] = [ 'text' => $vars['footer'], ]; } $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]; } // Dispatch webhook $this->logger->debug('Dispatching Discord webhook...'); try { $response = $this->httpClient->request( 'POST', $webhook_url, [ 'headers' => [ 'Content-Type' => 'application/json', ], 'json' => $webhook_body, ] ); $this->logger->addRecord( ($response->getStatusCode() !== 204 ? Level::Error : Level::Debug), sprintf('Webhook %s returned code %d', self::NAME, $response->getStatusCode()), ['message_sent' => $webhook_body, 'response_body' => $response->getBody()->getContents()] ); } catch (TransferException $e) { $this->logger->error(sprintf('Error from Discord (%d): %s', $e->getCode(), $e->getMessage())); return false; } return true; } /** @noinspection HttpUrlsUsage */ protected function getImageUrl(?string $url = null): ?string { $url = $this->getValidUrl($url); if (null !== $url) { return str_replace('http://', 'https://', $url); } return null; } }