Pretty up Slack formatting. (#15898)

* Pretty up Slack formatting.

* Normalize spaces by turning every instance of two or more spaces into
  one space.
* Allow for custom URL links even when strip_tags() is in effect by
  transformaing "standard" markdown syntax into Slack link markdown
  after strip_tags() is run.

* Document the changes to the Slack transport.

While here, flesh out the Slack docs to match the configuration options
one currently sees in the transport.

* Use backticks to escape things from markdown rendering.

* We don't need this other escaping now.

* Fix custom link due to line break.

* Fix spelling typo.

---------

Co-authored-by: Joe Clarke <jclarke@cisco.com>
This commit is contained in:
Joe Clarke 2024-04-16 12:28:10 -04:00 committed by GitHub
parent 83fe4b10c4
commit d7737b9889
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 2 deletions

View File

@ -36,6 +36,20 @@ class Slack extends Transport
$icon = $this->config['slack-icon_emoji'] ?? $slack_opts['icon_emoji'] ?? null;
$slack_msg = html_entity_decode(strip_tags($alert_data['msg'] ?? ''), ENT_QUOTES);
/*
* Normalize spaces since you might want to do logic in your template, and Slack is
* very sensitive to spaces. This turns every instance of two or more spaces into
* one space.
*/
$slack_msg = preg_replace('/ {2,}/', ' ', $slack_msg);
/*
* Replace "standard" markdown links with Slack-specific markdown.
* This has to be done after strip_tags() because these are actually tags.
* So [Target](https://mysite.example.com) becomes <https://mysite.example.com|Target>
*/
$slack_msg = preg_replace('/\[([^\]]+)\]\(((https?|mailto|ftp):[^\)]+)\)/', '<$2|$1>', $slack_msg);
$data = [
'attachments' => [
0 => [

View File

@ -774,16 +774,36 @@ only required value is for url, without this then no call to Slack will be made
We currently support the following attachment options:
`author_name`
- `author_name`
We currently support the following global message options:
- `channel_name` : Slack channel name (without the leading '#') to which the alert will go
- `icon_emoji` : Emoji name in colon format to use as the author icon
[Slack docs](https://api.slack.com/docs/message-attachments)
The alert template can make use of
[Slack markdown](https://api.slack.com/reference/surfaces/formatting#basic-formatting).
In the Slack markdown dialect, custom links are denoted with HTML angled
brackets, but LibreNMS strips these out. To support embedding custom links in alerts,
use the bracket/parentheses markdown syntax for links. For example if you would
typically use this for a Slack link:
`<https://www.example.com|My Link>`
Use this in your alert template:
`[My Link](https://www.example.com)`
**Example:**
| Config | Example |
| ------ | ------- |
| Webhook URL | <https://slack.com/url/somehook> |
| Slack Options | author_name=Me |
| Channel | network-alerts |
| Author Name | LibreNMS Bot |
| Icon | `:scream:` |
## SMSEagle