Customizing Slack messages
You can customize the Slack messages sent through IBM Cloud Event Notifications by using a template. For more information, see Creating an Event Notifications template.
Prereqs
Read the following information:
-
When a message of length greater than 3000 characters is sent to slack, the message text gets truncated with leaders [...].
-
If a call to the Slack webhook URL fails even after retry attempts, the notification is lost.
Create a template by using the API
To customize the Slack message, you can configure the template by using the IBM Cloud Event Notifications API.
The following is an example Slack message template:
You can include data.log_example in a section if the size of the payload is less than 3000 characters.
{
"attachments": [
{
{{#if (equal data.alert_definition.severity "Error")}}
"color": "#dc143c",
{{else}}
"color": "#097969",
{{/if}}
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*{{data.alert_definition.name}}*"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "```{{data.log_example}}```"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<{{data.links.view_alert}}|View> | <{{data.links.edit_alert}}|Edit>"
}
}
]
}
]
}
The following is a sample Slack template for Kubernetes logs including an errorMsg field.
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Alert name: {{data.alert_definition.name}}",
"emoji": true
}
},
{
"type": "context",
"elements": [
{
"type": "image",
"image_url": "https://image.freepik.com/free-photo/red-drawing-pin_1156-445.jpg",
"alt_text": "images"
},
{
"type": "plain_text",
"text": "Severity: {{data.alert_definition.severity}}"
}
]
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Error message:* {{data.log_example.errorMsg}}"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Cluster name:* {{data.log_example.[kubernetes.cluster_name]}}"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Namespace:* {{data.log_example.[kubernetes.namespace_name]}} \n \n"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Pod name:* {{data.log_example.[kubernetes.pod_name]}}"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*App label:* {{data.log_example.[kubernetes.labels.app]}}"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Incident information in Cloud Logs:* <{{data.links.view_alert}}|View> | <{{data.links.edit_alert}}|Edit>"
}
}
]
}
Before using the template with the API, it must be converted to base64. After converting the template to base64 it can be included in the body of the API call. The following example uses the previous template converted to base64.
curl --request POST \
--url https://<EVENT-NOTIFICATION-REGION>.event-notifications.cloud.ibm.com/event-notifications/v1/instances/<INSTANCE_ID>/templates \
--header 'Authorization: Bearer <Access_token>' \
--header 'Content-Type: application/json' \
--data '{
"name": "<NAME>",
"description": "<DESCRIPTION>",
"params": {
"body": "ewogICAgImF0dGFjaG1lbnRzIjogWwogICAgICAgIHsKICAgICAgICAgICAge3sjaWYgKGVxdWFsIGRhdGEuYWxlcnRfZGVmaW5pdGlvbi5zZXZlcml0eSAiRXJyb3IiKX19CiAgICAgICAgICAgICJjb2xvciI6ICIjZGMxNDNjIiwKICAgICAgICAgICAge3tlbHNlfX0KICAgICAgICAgICAgImNvbG9yIjogIiMwOTc5NjkiLAogICAgICAgICAgICB7ey9pZn19CiAgICAgICAgICAgICJibG9ja3MiOiBbCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgInR5cGUiOiAic2VjdGlvbiIsCiAgICAgICAgICAgICAgICAgICAgInRleHQiOiB7CiAgICAgICAgICAgICAgICAgICAgICAgICJ0eXBlIjogIm1ya2R3biIsCiAgICAgICAgICAgICAgICAgICAgICAgICJ0ZXh0IjogIip7e2RhdGEuYWxlcnRfZGVmaW5pdGlvbi5uYW1lfX0qIgogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0sCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgInR5cGUiOiAic2VjdGlvbiIsCiAgICAgICAgICAgICAgICAgICAgInRleHQiOiB7CiAgICAgICAgICAgICAgICAgICAgICAgICJ0eXBlIjogIm1ya2R3biIsCiAgICAgICAgICAgICAgICAgICAgICAgICJ0ZXh0IjogImBgYHt7ZGF0YS5sb2dfZXhhbXBsZX19YGBgIgogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0sCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgInR5cGUiOiAic2VjdGlvbiIsCiAgICAgICAgICAgICAgICAgICAgInRleHQiOiB7CiAgICAgICAgICAgICAgICAgICAgICAgICJ0eXBlIjogIm1ya2R3biIsCiAgICAgICAgICAgICAgICAgICAgICAgICJ0ZXh0IjogIjx7e2RhdGEubGlua3Mudmlld19hbGVydH19fFZpZXc+IHwgPHt7ZGF0YS5saW5rcy5lZGl0X2FsZXJ0fX18RWRpdD4iCiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICBdCiAgICAgICAgfQogICAgXQp9"
},
"type": "slack.notification"
}'
You will need to change <EVENT-NOTIFICATION-REGION>, <INSTANCE-ID>, <NAME>, and <DESCRIPTION> as appropriate for your environment.
Create a template by using Terraform
You can also use a Terraform script to create the template.
resource "ibm_en_slack_template" "slack_template" {
instance_guid = "<instance-id>"
name = "Notification Template"
type = "slack.notification"
description = "Slack template for event notification"
params {
body="ewogICAgImF0dGFjaG1lbnRzIjogWwogICAgICAgIHsKICAgICAgICAgICAge3sjaWYgKGVxdWFsIGRhdGEuYWxlcnRfZGVmaW5pdGlvbi5zZXZlcml0eSAiRXJyb3IiKX19CiAgICAgICAgICAgICJjb2xvciI6ICIjZGMxNDNjIiwKICAgICAgICAgICAge3tlbHNlfX0KICAgICAgICAgICAgImNvbG9yIjogIiMwOTc5NjkiLAogICAgICAgICAgICB7ey9pZn19CiAgICAgICAgICAgICJibG9ja3MiOiBbCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgInR5cGUiOiAic2VjdGlvbiIsCiAgICAgICAgICAgICAgICAgICAgInRleHQiOiB7CiAgICAgICAgICAgICAgICAgICAgICAgICJ0eXBlIjogIm1ya2R3biIsCiAgICAgICAgICAgICAgICAgICAgICAgICJ0ZXh0IjogIip7e2RhdGEuYWxlcnRfZGVmaW5pdGlvbi5uYW1lfX0qIgogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0sCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgInR5cGUiOiAic2VjdGlvbiIsCiAgICAgICAgICAgICAgICAgICAgInRleHQiOiB7CiAgICAgICAgICAgICAgICAgICAgICAgICJ0eXBlIjogIm1ya2R3biIsCiAgICAgICAgICAgICAgICAgICAgICAgICJ0ZXh0IjogImBgYHt7ZGF0YS5sb2dfZXhhbXBsZX19YGBgIgogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0sCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgInR5cGUiOiAic2VjdGlvbiIsCiAgICAgICAgICAgICAgICAgICAgInRleHQiOiB7CiAgICAgICAgICAgICAgICAgICAgICAgICJ0eXBlIjogIm1ya2R3biIsCiAgICAgICAgICAgICAgICAgICAgICAgICJ0ZXh0IjogIjx7e2RhdGEubGlua3Mudmlld19hbGVydH19fFZpZXc+IHwgPHt7ZGF0YS5saW5rcy5lZGl0X2FsZXJ0fX18RWRpdD4iCiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICBdCiAgICAgICAgfQogICAgXQp9"
}
provider = ibm.ibm-en
}
provider.tf
provider "ibm" {
alias = "ibm-en"
region = "jp-tok"
}
version.tf
terraform {
required_version = ">= 1.5.0"
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
version = "1.70.0-beta0"
}
}
}
For more information about implementing Slack templates, see the IBM Cloud Event Notifications documentation about Slack notification templates.