Skip to content

Commit

Permalink
Merge pull request #11 from electrobayan/version-110
Browse files Browse the repository at this point in the history
[new] Custom tags are implemented
  • Loading branch information
electrobayan authored Dec 23, 2021
2 parents 8fb3269 + 72594a5 commit 771925b
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 28 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ You will have to create a Telegram bot and a channel. Check the user guide <a hr
Then just do the following:
<pre>
$infoLogger = new \TelegramLogger\Creator\InfoPoster('Telegram Key Goes here', '@your_telegram_channel_name');
$infoLogger->post('test', ['key_1' => 'value_1', 'key_2' => 'value_2']);
$infoLogger->post('test', ['key_1' => 'value_1', 'key_2' => 'value_2'], ['tag1', 'tag2']);
</pre>

### Details
Expand All @@ -50,9 +50,10 @@ $infoLogger = new \TelegramLogger\Creator\InfoPoster('Telegram Key Goes here', '

After the poster created all is left is just to call `post` method.
<pre>
$errorLogger->post('Error Message', ['key_1' => 'value_1', 'key_2' => 'value_2']);
$warningLogger->post('Warning Message', ['key_1' => 'value_1', 'key_2' => 'value_2']);
$infoLogger->post('Info Message', ['key_1' => 'value_1', 'key_2' => 'value_2']);
$errorLogger->post('Error Message', ['key_1' => 'value_1', 'key_2' => 'value_2'], ['tag1', 'tag2']);
$warningLogger->post('Warning Message', ['key_1' => 'value_1', 'key_2' => 'value_2'], ['tag1', 'tag2']);
$infoLogger->post('Info Message', ['key_1' => 'value_1', 'key_2' => 'value_2'], ['tag1', 'tag2']);
</pre>

Also, you can pass <b>optional</b> extra params if needed as an array `key => value`. Both key and value will be posted into the channel.
You can pass <b>optional</b> tags if needed as an array `key => value`. Only values will be posted into the channel.
5 changes: 3 additions & 2 deletions src/Creator/AbstractPoster.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ abstract public function getMessageObject(): MessageInterface;
/**
* @param string $text
* @param array $params
* @param array $tags
* @return bool
*/
public function post(string $text, array $params = []): bool
public function post(string $text, array $params = [], array $tags = []): bool
{
$result = true;

$formattedPost = $this
->getMessageObject()
->getFormattedPost($text, $params);
->getFormattedPost($text, $params, $tags);

try {
$this->telegramConnector->sendMessage($this->channelId, $formattedPost, self::HTML_TELEGRAM_MODE);
Expand Down
3 changes: 2 additions & 1 deletion src/Creator/PosterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public function getMessageObject(): MessageInterface;
/**
* @param string $text
* @param array $params
* @param array $tags
* @return bool
*/
public function post(string $text, array $params = []): bool;
public function post(string $text, array $params = [], array $tags = []): bool;
}
76 changes: 62 additions & 14 deletions src/Message/AbstractMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,70 @@ protected function getLogTag(): string
/**
* @param string $text
* @param array $params
* @param array $tags
* @return string
*/
public function getFormattedPost(string $text, array $params = []): string
public function getFormattedPost(string $text, array $params = [], array $tags = []): string
{
$postFrame = $this->prepareFrame();
$postFrame = $this->prepareFrame($params);
$formattedParams = $this->prepareParams($params);
$formattedTags = $this->prepareTags($tags);

return sprintf($postFrame, $text, $formattedParams);
return sprintf($postFrame, $text, $formattedParams, $formattedTags);
}

/**
* @param array $params
* @return string
*/
protected function prepareFrame(array $params): string
{
$titleBlock = $this->prepareTitleBlock();
$paramsBlock = $this->prepareParamsBlock($params);
$tagsBlock = $this->prepareTagsBlock();

return $titleBlock . $paramsBlock . $tagsBlock;
}

/**
* @return string
*/
protected function prepareFrame(): string
protected function prepareTitleBlock(): string
{
$title = $this->getLogTitle();
$extraParamsKey = self::EXTRA_PARAMS_KEY;
return <<<TITLE
$title
<pre>%s</pre>
TITLE;
}

/**
* @param array $params
* @return string
*/
protected function prepareParamsBlock(array $params): string
{
$extraParamsKey = $params ? self::NEW_LINE_DIVIDER . self::EXTRA_PARAMS_KEY : '';
$extraParamsValue = $params ? '<pre>%s</pre>' . self::NEW_LINE_DIVIDER : '';

return <<<PARAMS
$extraParamsKey
$extraParamsValue
PARAMS;
}

/**
* @return string
*/
protected function prepareTagsBlock(): string
{
$tagsKey = self::TAGS_KEY;
$tags = $this->getLogTag();

return <<<FRAME
$title
<pre>%s</pre>
$extraParamsKey
<pre>%s</pre>
return <<<TAGS
$tagsKey
$tags
FRAME;
$tags %s
TAGS;
}

/**
Expand All @@ -73,4 +107,18 @@ protected function prepareParams(array $params): string

return $result;
}

/**
* @param array $tags
* @return string
*/
protected function prepareTags(array $tags): string
{
$result = '';

if ($tags) {
$result = self::TAG_PREFIX . implode(self::DIVIDER . self::TAG_PREFIX, $tags);
}
return $result;
}
}
9 changes: 6 additions & 3 deletions src/Message/MessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ interface MessageInterface
* @const string
*/
public const NEW_LINE_DIVIDER = "\r\n";
public const PARAMS_DIVIDER = ': ';
public const EXTRA_PARAMS_KEY = '<i>- Extra parameters</i>';
public const DIVIDER = ' ';
public const PARAMS_DIVIDER = ':' . self::DIVIDER;
public const EXTRA_PARAMS_KEY = '<i>- Params</i>';
public const TAGS_KEY = '<i>- Tags</i>';
public const TAG_PREFIX = '#';

/**
* Icon showing in the message
Expand All @@ -38,7 +40,8 @@ interface MessageInterface
/**
* @param string $text
* @param array $params
* @param array $tags
* @return string
*/
public function getFormattedPost(string $text, array $params = []): string;
public function getFormattedPost(string $text, array $params = [], array $tags = []): string;
}
3 changes: 2 additions & 1 deletion src/Tests/Creator/PosterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public function testPost(): void
'key_1' => 'value_1',
'key_2' => 'value_2'
];
$testTags = ['tag1', 'tag2'];

$infoPoster = new InfoPoster($testToken, $testChannelId);
$actualResult = $infoPoster->post($testText, $testParams);
$actualResult = $infoPoster->post($testText, $testParams, $testTags);

$this->assertIsBool($actualResult);
}
Expand Down
11 changes: 8 additions & 3 deletions src/Tests/Message/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class MessageTest extends TestCase
public function testGetFormattedPost(): void
{
$testText = 'Test text test text';
$testParamsAsString = "param_1: value_1\r\nparam_2: 2\r\n";
$testParamsAsArray = [
'param_1' => 'value_1',
'param_2' => 2
Expand All @@ -23,9 +22,15 @@ public function testGetFormattedPost(): void
$infoMessage = new InfoMessage();
$actualResult = $infoMessage->getFormattedPost($testText, $testParamsAsArray);

$expected = "ℹ️<b>INFO</b>ℹ️\r\n\r\n$testText\r\n\r\n<b>--- Extra parameters</b>\r\n\r\n$testParamsAsString\r\n\r\n<b>--- Tags</b>\r\n\r\n#info\r\n\r\nℹ️";
$expected = " ℹ️<b>INFO</b>
<pre>Test text test text</pre>
<i>- Params</i>
<pre>param_1: value_1
param_2: 2
</pre>
<i>- Tags</i>
#info ";

$this->assertEquals($expected, $actualResult);
}
}

0 comments on commit 771925b

Please sign in to comment.