About cookies on this site Our websites require some cookies to function properly (required). In addition, other cookies may be used with your consent to analyze site usage, improve the user experience and for advertising. For more information, please review your options. By visiting our website, you agree to our processing of information as described in IBM’sprivacy statement. To provide a smooth navigation, your cookie preferences will be shared across the IBM web domains listed here.
Customizing Slack messages using the API
You can customize the Slack messages sent through IBM Cloud Event Notifications by using the API to create a template.
To customize the Slack message you need to configure the template using the IBM Cloud Event Notifications API not the UI.
The following is an example Slack message template.
{
"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.
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.