- fixed HTML tags in RallyPoke messages (Slack/Discord), closed #715

This commit is contained in:
Mark Friedrich
2018-11-17 11:09:15 +01:00
parent 703091949b
commit 68b47f0488
10 changed files with 296 additions and 220 deletions

View File

@@ -36,8 +36,8 @@ class Monolog extends \Prefab {
'mail' => 'Monolog\Handler\SwiftMailerHandler',
'slackMap' => 'lib\logging\handler\SlackMapWebhookHandler',
'slackRally' => 'lib\logging\handler\SlackRallyWebhookHandler',
'discordMap' => 'lib\logging\handler\SlackMapWebhookHandler', // use Slack handler for Discord
'discordRally' => 'lib\logging\handler\SlackRallyWebhookHandler', // use Slack handler for Discord
'discordMap' => 'lib\logging\handler\DiscordMapWebhookHandler',
'discordRally' => 'lib\logging\handler\DiscordRallyWebhookHandler',
'zmq' => 'lib\logging\handler\ZMQHandler'
];

View File

@@ -0,0 +1,99 @@
<?php
/**
* Created by PhpStorm.
* User: Exodus 4D
* Date: 17.11.2018
* Time: 10:18
*/
namespace lib\logging\handler;
use lib\Util;
abstract class AbstractMapWebhookHandler extends AbstractWebhookHandler {
/**
* @param array $record
* @return array
*/
protected function getSlackData(array $record) : array{
$postData = parent::getSlackData($record);
$tag = (string)$record['context']['tag'];
$timestamp = (int)$record['datetime']->getTimestamp();
$text = '';
if (
$this->useAttachment &&
!empty( $attachmentsData = $record['context']['data'])
) {
// convert non grouped data (associative array) to multi dimensional (sequential) array
// -> see "group" records
$attachmentsData = Util::is_assoc($attachmentsData) ? [$attachmentsData] : $attachmentsData;
$thumbData = (array)$record['extra']['thumb'];
$postData['attachments'] = [];
foreach($attachmentsData as $attachmentData){
$channelData = (array)$attachmentData['channel'];
$characterData = (array)$attachmentData['character'];
$formatted = (string)$attachmentData['formatted'];
// get "message" from $formatted
$msgParts = explode('|', $formatted, 2);
// build main text from first Attachment (they belong to same channel)
if(!empty($channelData)){
$text = "*Map '" . $channelData['channelName'] . "'* _#" . $channelData['channelId'] . "_ *changed*";
}
$attachment = [
'title' => !empty($msgParts[0]) ? $msgParts[0] : 'No Title',
//'pretext' => '',
'text' => !empty($msgParts[1]) ? sprintf('```%s```', $msgParts[1]) : '',
'fallback' => !empty($msgParts[1]) ? $msgParts[1] : 'No Fallback',
'color' => $this->getAttachmentColor($tag),
'fields' => [],
'mrkdwn_in' => ['fields', 'text'],
'footer' => 'Pathfinder API',
//'footer_icon'=> '',
'ts' => $timestamp
];
$attachment = $this->setAuthor($attachment, $characterData);
$attachment = $this->setThumb($attachment, $thumbData);
// set 'field' array ----------------------------------------------------------------------------------
if ($this->includeExtra) {
$attachment['fields'][] = $this->generateAttachmentField('', 'Meta data:', false, false);
if(!empty($record['extra']['path'])){
$attachment['fields'][] = $this->generateAttachmentField('Path', $record['extra']['path'], true);
}
if(!empty($tag)){
$attachment['fields'][] = $this->generateAttachmentField('Tag', $tag, true);
}
if(!empty($record['level_name'])){
$attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name'], true);
}
if(!empty($record['extra']['ip'])){
$attachment['fields'][] = $this->generateAttachmentField('IP', $record['extra']['ip'], true);
}
}
$postData['attachments'][] = $attachment;
}
}
$postData['text'] = empty($text) ? $postData['text'] : $text;
return $postData;
}
}

View File

@@ -0,0 +1,156 @@
<?php
/**
* Created by PhpStorm.
* User: Exodus 4D
* Date: 17.11.2018
* Time: 10:09
*/
namespace lib\logging\handler;
use League\HTMLToMarkdown\HtmlConverter;
use lib\Util;
abstract class AbstractRallyWebhookHandler extends AbstractWebhookHandler {
/**
* @param array $record
* @return array
*/
protected function getSlackData(array $record) : array {
$postData = parent::getSlackData($record);
$tag = (string)$record['context']['tag'];
$timestamp = (int)$record['datetime']->getTimestamp();
$text = '';
if (
$this->useAttachment &&
!empty( $attachmentsData = $record['context']['data'])
){
// convert non grouped data (associative array) to multi dimensional (sequential) array
// -> see "group" records
$attachmentsData = Util::is_assoc($attachmentsData) ? [$attachmentsData] : $attachmentsData;
$thumbData = (array)$record['extra']['thumb'];
$postData['attachments'] = [];
foreach($attachmentsData as $attachmentData){
$characterData = (array)$attachmentData['character'];
$text = 'No Title';
if( !empty($attachmentData['formatted']) ){
$text = $attachmentData['formatted'];
}
$attachment = [
'title' => !empty($attachmentData['main']['message']) ? 'Message' : '',
//'pretext' => '',
'text' => !empty($attachmentData['main']['message']) ? sprintf('```%s```', $attachmentData['main']['message']) : '',
'fallback' => !empty($attachmentData['main']['message']) ? $attachmentData['main']['message'] : 'No Fallback',
'color' => $this->getAttachmentColor($tag),
'fields' => [],
'mrkdwn_in' => ['fields', 'text'],
'footer' => 'Pathfinder API',
//'footer_icon'=> '',
'ts' => $timestamp
];
$attachment = $this->setAuthor($attachment, $characterData);
$attachment = $this->setThumb($attachment, $thumbData);
// set 'field' array ----------------------------------------------------------------------------------
if ($this->includeContext) {
if(!empty($objectData = $attachmentData['object'])){
if(!empty($objectData['objAlias'])){
// System alias
$attachment['fields'][] = $this->generateAttachmentField('Alias', $objectData['objAlias']);
}
if(!empty($objectData['objName'])){
// System name
$attachment['fields'][] = $this->generateAttachmentField('System', $objectData['objName']);
}
if(!empty($objectData['objRegion'])){
// System region
$attachment['fields'][] = $this->generateAttachmentField('Region', $objectData['objRegion']);
}
if(isset($objectData['objIsWormhole'])){
// Is wormhole
$attachment['fields'][] = $this->generateAttachmentField('Wormhole', $objectData['objIsWormhole'] ? 'Yes' : 'No');
}
if(!empty($objectData['objSecurity'])){
// System security
$attachment['fields'][] = $this->generateAttachmentField('Security', $objectData['objSecurity']);
}
if(!empty($objectData['objEffect'])){
// System effect
$attachment['fields'][] = $this->generateAttachmentField('Effect', $objectData['objEffect']);
}
if(!empty($objectData['objTrueSec'])){
// System trueSec
$attachment['fields'][] = $this->generateAttachmentField('TrueSec', $objectData['objTrueSec']);
}
if(!empty($objectData['objCountPlanets'])){
// System planet count
$attachment['fields'][] = $this->generateAttachmentField('Planets', $objectData['objCountPlanets']);
}
if(!empty($objectData['objDescription'])){
// System description
$attachment['fields'][] = $this->generateAttachmentField('System description', '```' . $this->htmlToMarkdown($objectData['objDescription']) . '```', false, false);
}
if(!empty($objectData['objUrl'])){
// System deeeplink
$attachment['fields'][] = $this->generateAttachmentField('', $objectData['objUrl'] , false, false);
}
}
}
if($this->includeExtra){
if(!empty($record['extra']['path'])){
$attachment['fields'][] = $this->generateAttachmentField('Path', $record['extra']['path'], true);
}
if(!empty($tag)){
$attachment['fields'][] = $this->generateAttachmentField('Tag', $tag, true);
}
if(!empty($record['level_name'])){
$attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name'], true);
}
if(!empty($record['extra']['ip'])){
$attachment['fields'][] = $this->generateAttachmentField('IP', $record['extra']['ip'], true);
}
}
$postData['attachments'][] = $attachment;
}
}
$postData['text'] = empty($text) ? $postData['text'] : $text;
return $postData;
}
/**
* convert $html into Markdown
* @param $html
* @return string
*/
protected function htmlToMarkdown($html){
$converter = new HtmlConverter();
$converter->getConfig()->setOption('strip_tags', true);
$markdown = $converter->convert($html);
return $markdown;
}
}

View File

@@ -12,7 +12,7 @@ use lib\Config;
use Monolog\Handler;
use Monolog\Logger;
abstract class AbstractSlackWebhookHandler extends Handler\AbstractProcessingHandler {
abstract class AbstractWebhookHandler extends Handler\AbstractProcessingHandler {
/**
* @var string

View File

@@ -0,0 +1,14 @@
<?php
/**
* Created by PhpStorm.
* User: Exodus 4D
* Date: 17.11.2018
* Time: 10:23
*/
namespace lib\logging\handler;
class DiscordMapWebhookHandler extends AbstractMapWebhookHandler {
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* Created by PhpStorm.
* User: Exodus 4D
* Date: 17.11.2018
* Time: 10:13
*/
namespace lib\logging\handler;
class DiscordRallyWebhookHandler extends AbstractRallyWebhookHandler {
protected function htmlToMarkdown($html){
$markdown = parent::htmlToMarkdown($html);
// Discord supports syntax highlighting for MarkDown
$markdown = 'Markdown' . "\n" . $markdown;
return $markdown;
}
}

View File

@@ -8,94 +8,7 @@
namespace lib\logging\handler;
use lib\Util;
class SlackMapWebhookHandler extends AbstractSlackWebhookHandler {
/**
* @param array $record
* @return array
*/
protected function getSlackData(array $record) : array{
$postData = parent::getSlackData($record);
$tag = (string)$record['context']['tag'];
$timestamp = (int)$record['datetime']->getTimestamp();
$text = '';
if (
$this->useAttachment &&
!empty( $attachmentsData = $record['context']['data'])
) {
// convert non grouped data (associative array) to multi dimensional (sequential) array
// -> see "group" records
$attachmentsData = Util::is_assoc($attachmentsData) ? [$attachmentsData] : $attachmentsData;
$thumbData = (array)$record['extra']['thumb'];
$postData['attachments'] = [];
foreach($attachmentsData as $attachmentData){
$channelData = (array)$attachmentData['channel'];
$characterData = (array)$attachmentData['character'];
$formatted = (string)$attachmentData['formatted'];
// get "message" from $formatted
$msgParts = explode('|', $formatted, 2);
// build main text from first Attachment (they belong to same channel)
if(!empty($channelData)){
$text = "*Map '" . $channelData['channelName'] . "'* _#" . $channelData['channelId'] . "_ *changed*";
}
$attachment = [
'title' => !empty($msgParts[0]) ? $msgParts[0] : 'No Title',
//'pretext' => '',
'text' => !empty($msgParts[1]) ? sprintf('```%s```', $msgParts[1]) : '',
'fallback' => !empty($msgParts[1]) ? $msgParts[1] : 'No Fallback',
'color' => $this->getAttachmentColor($tag),
'fields' => [],
'mrkdwn_in' => ['fields', 'text'],
'footer' => 'Pathfinder API',
//'footer_icon'=> '',
'ts' => $timestamp
];
$attachment = $this->setAuthor($attachment, $characterData);
$attachment = $this->setThumb($attachment, $thumbData);
// set 'field' array ----------------------------------------------------------------------------------
if ($this->includeExtra) {
$attachment['fields'][] = $this->generateAttachmentField('', 'Meta data:', false, false);
if(!empty($record['extra']['path'])){
$attachment['fields'][] = $this->generateAttachmentField('Path', $record['extra']['path'], true);
}
if(!empty($tag)){
$attachment['fields'][] = $this->generateAttachmentField('Tag', $tag, true);
}
if(!empty($record['level_name'])){
$attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name'], true);
}
if(!empty($record['extra']['ip'])){
$attachment['fields'][] = $this->generateAttachmentField('IP', $record['extra']['ip'], true);
}
}
$postData['attachments'][] = $attachment;
}
}
$postData['text'] = empty($text) ? $postData['text'] : $text;
return $postData;
}
class SlackMapWebhookHandler extends AbstractMapWebhookHandler {
}

View File

@@ -8,138 +8,10 @@
namespace lib\logging\handler;
use lib\Util;
class SlackRallyWebhookHandler extends AbstractSlackWebhookHandler {
class SlackRallyWebhookHandler extends AbstractRallyWebhookHandler {
/**
* @param array $record
* @return array
*/
protected function getSlackData(array $record) : array{
$postData = parent::getSlackData($record);
$tag = (string)$record['context']['tag'];
$timestamp = (int)$record['datetime']->getTimestamp();
$text = '';
if (
$this->useAttachment &&
!empty( $attachmentsData = $record['context']['data'])
){
// convert non grouped data (associative array) to multi dimensional (sequential) array
// -> see "group" records
$attachmentsData = Util::is_assoc($attachmentsData) ? [$attachmentsData] : $attachmentsData;
$thumbData = (array)$record['extra']['thumb'];
$postData['attachments'] = [];
foreach($attachmentsData as $attachmentData){
$characterData = (array)$attachmentData['character'];
$text = 'No Title';
if( !empty($attachmentData['formatted']) ){
$text = $attachmentData['formatted'];
}
$attachment = [
'title' => !empty($attachmentData['main']['message']) ? 'Message' : '',
//'pretext' => '',
'text' => !empty($attachmentData['main']['message']) ? sprintf('```%s```', $attachmentData['main']['message']) : '',
'fallback' => !empty($attachmentData['main']['message']) ? $attachmentData['main']['message'] : 'No Fallback',
'color' => $this->getAttachmentColor($tag),
'fields' => [],
'mrkdwn_in' => ['fields', 'text'],
'footer' => 'Pathfinder API',
//'footer_icon'=> '',
'ts' => $timestamp
];
$attachment = $this->setAuthor($attachment, $characterData);
$attachment = $this->setThumb($attachment, $thumbData);
// set 'field' array ----------------------------------------------------------------------------------
if ($this->includeContext) {
if(!empty($objectData = $attachmentData['object'])){
if(!empty($objectData['objAlias'])){
// System alias
$attachment['fields'][] = $this->generateAttachmentField('Alias', $objectData['objAlias']);
}
if(!empty($objectData['objName'])){
// System name
$attachment['fields'][] = $this->generateAttachmentField('System', $objectData['objName']);
}
if(!empty($objectData['objRegion'])){
// System region
$attachment['fields'][] = $this->generateAttachmentField('Region', $objectData['objRegion']);
}
if(isset($objectData['objIsWormhole'])){
// Is wormhole
$attachment['fields'][] = $this->generateAttachmentField('Wormhole', $objectData['objIsWormhole'] ? 'Yes' : 'No');
}
if(!empty($objectData['objSecurity'])){
// System security
$attachment['fields'][] = $this->generateAttachmentField('Security', $objectData['objSecurity']);
}
if(!empty($objectData['objEffect'])){
// System effect
$attachment['fields'][] = $this->generateAttachmentField('Effect', $objectData['objEffect']);
}
if(!empty($objectData['objTrueSec'])){
// System trueSec
$attachment['fields'][] = $this->generateAttachmentField('TrueSec', $objectData['objTrueSec']);
}
if(!empty($objectData['objCountPlanets'])){
// System planet count
$attachment['fields'][] = $this->generateAttachmentField('Planets', $objectData['objCountPlanets']);
}
if(!empty($objectData['objDescription'])){
// System trueSec
$attachment['fields'][] = $this->generateAttachmentField('System description', '```' . $objectData['objDescription'] . '```', false, false);
}
if(!empty($objectData['objUrl'])){
// System deeeplink
$attachment['fields'][] = $this->generateAttachmentField('', $objectData['objUrl'] , false, false);
}
}
}
if($this->includeExtra){
if(!empty($record['extra']['path'])){
$attachment['fields'][] = $this->generateAttachmentField('Path', $record['extra']['path'], true);
}
if(!empty($tag)){
$attachment['fields'][] = $this->generateAttachmentField('Tag', $tag, true);
}
if(!empty($record['level_name'])){
$attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name'], true);
}
if(!empty($record['extra']['ip'])){
$attachment['fields'][] = $this->generateAttachmentField('IP', $record['extra']['ip'], true);
}
}
$postData['attachments'][] = $attachment;
}
}
$postData['text'] = empty($text) ? $postData['text'] : $text;
return $postData;
}
}

View File

@@ -32,6 +32,7 @@
"monolog/monolog": "1.*",
"websoftwares/monolog-zmq-handler": "0.2.*",
"swiftmailer/swiftmailer": "^6.0",
"league/html-to-markdown": "4.8.*",
"exodus4d/pathfinder_esi": "dev-develop as 0.0.x-dev"
}
}

View File

@@ -32,6 +32,7 @@
"monolog/monolog": "1.*",
"websoftwares/monolog-zmq-handler": "0.2.*",
"swiftmailer/swiftmailer": "^6.0",
"league/html-to-markdown": "4.8.*",
"exodus4d/pathfinder_esi": "dev-master#v1.2.5"
}
}