When a matched rule in a notification policy or event is triggered, a notification message is sent via email, Feishu, DingTalk, phone, Webhook, and other methods based on the content defined in the notification template. The system provides a preset notification title and content for each notification type by default. If the preset templates do not meet your requirements, you can customize a notification template based on your actual scenario. This topic describes the syntax supported for writing notification templates.
The notification title and content in a notification template are written and rendered using Markdown and Go Template syntax. Before configuring a custom notification template, you need to first understand basic Go Template syntax. tip
The template writing syntax is universal, but the supported extension functions and variables vary by notification type. For more information, see:
Common Go template syntax
The . character is used to render the specified object within the current scope.
Example 1: Obtain all contents within the top-level scope, which are all contents in an alerting notification.
Expected response result: The output contains all content in the alert notification.
Example 2: Query the Region field of the alert policy.
The following sample code shows the expected response result:
Variables are used to store input data, and after data is passed into variables, the entire template can access the data by using the variables. For example, in {{$rulename := .RuleName}}, use $rulename as a variable to store the alert policy name, and then use {{$rulename}} to access the variable.
Example: Store the alert policy name in $rulename and then output $rulename.
The following sample code shows the expected response result:
The conditional statement if/else uses if to check the data. If the condition is not met, the content of else is executed. The if statement returns false when the evaluation condition is a null value, 0, nil, an empty string, or a zero-length string.
Example: If the value of .Level in the alert information equals warning, the output is Warning.
The following sample code shows the expected response result:
The comparison function is used to compare the data size. It is often used together with if/else statements to determine the execution of statements. The following table shows commonly used comparison functions.
Example: If the value of .Level is equal to warning, the output is Warning.
The following sample code shows the expected response result:
Logical functions primarily include and, or, and not, which are used to perform logical negation, conjunction, and disjunction across multiple conditions.
Example: If the value of Region is cn-beijing and the value of Level is warning, the output is Warning level alert from Beijing.
The following sample code shows the response result:
Common built-in functions
The following table describes the common Go template built-in functions.