Creating a Message Template
Function
This API is used to create a notification template. Currently, each account can create a maximum of 100 notification templates. After a notification template is created, its name cannot be changed.
Calling Method
For details, see Calling APIs.
URI
POST /v2/{project_id}/{domain_id}/lts/events/notification/templates
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
project_id |
Yes |
String |
Project ID. For details about how to obtain a project ID, see Obtaining the Project ID, Account ID, Log Group ID, and Log Stream ID. |
domain_id |
Yes |
String |
Account ID. For details about how to obtain an account ID, see Obtaining the Project ID, Account ID, Log Group ID, and Log Stream ID. |
Request Parameters
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
X-Auth-Token |
Yes |
String |
User token obtained from IAM. For details about how to obtain a user token, see Obtaining a User Token. |
Content-Type |
Yes |
String |
Set this parameter to application/json;charset=UTF-8. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
name |
Yes |
String |
Notification rule name, which is mandatory. The value can contain only digits, letters, underscores (), and hyphens (-), and cannot start or end with special characters such as underscores (). The value can contain 1 to 100 characters and cannot be changed after being created. |
type |
No |
Array of strings |
This field is reserved and optional. |
desc |
No |
String |
Template description, which is optional. Enter a maximum of 1,024 characters, including only digits, letters, and underscores (). Do not start or end with an underscore (). |
source |
Yes |
String |
Template source. Currently, this parameter must be set to LTS. Otherwise, the template cannot be filtered. |
locale |
Yes |
String |
Language, which is mandatory. The value can be zh-cn or en-us. |
templates |
Yes |
Array of SubTemplate objects |
Template body is an array. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
sub_type |
Yes |
String |
Template subtype. Only the following types are supported: sms, dingding, wechat, webhook, and email. |
content |
Yes |
String |
In the subtemplate body, the variables following the dollar sign ($) can only be the following variables. The supported variables vary according to the alarm type (keyword or SQL). Currently, the two alarm types have the following common variables: Severity: ${event_severity}; Occurred: ${starts_at}; Alarm source: $event.metadata.resource_provider; Resource type: $event.metadata.resource_type; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency; Variables specific to keyword alarms: Query time: $event.annotations.results[0].time; Query log: $event.annotations.results[0].raw_results; Variables specific to SQL alarms: Log group/stream name: $event.annotations.results[0].resource_id; Query statement: $event.annotations.results[0].sql; Query time: $event.annotations.results[0].time; Query URL: $event.annotations.results[0].url; Query log: $event.annotations.results[0].raw_results;
NOTE:
Each variable must be followed by an English semicolon (;). Otherwise, the template replacement fails. |
topic |
No |
String |
Email subject. This parameter is valid only when sub_type is set to email. |
Response Parameters
Status code: 200
Parameter |
Type |
Description |
---|---|---|
name |
String |
Notification rule name, which is mandatory. The value can contain only digits, letters, underscores (), and hyphens (-), and cannot start or end with special characters such as underscores (). The value can contain 1 to 100 characters and cannot be changed after being created. |
type |
Array of strings |
This field is reserved and optional. |
desc |
String |
Template description, which is optional. Enter a maximum of 1,024 characters, including only digits, letters, and underscores (). Do not start or end with an underscore (). |
source |
String |
Template source. Currently, this parameter must be set to LTS. Otherwise, the template cannot be filtered. |
locale |
String |
Language, which is mandatory. The value can be zh-cn or en-us. |
templates |
Array of SubTemplateResBody objects |
Template body, which is an array. |
Parameter |
Type |
Description |
---|---|---|
sub_type |
String |
Template subtype. Only the following types are supported: sms, dingding, wechat, webhook, and email. Value: |
content |
String |
In the subtemplate body, the variables following the dollar sign ($) can only be the following variables. The supported variables vary according to the alarm type (keyword or SQL). Currently, the two alarm types have the following common variables: Severity: ${event_severity}; Occurred: ${starts_at}; Alarm source: $event.metadata.resource_provider; Resource type: $event.metadata.resource_type; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency; Variables specific to keyword alarms: Query time: $event.annotations.results[0].time; Query log: $event.annotations.results[0].raw_results; Variables specific to SQL alarms: Log group/stream name: $event.annotations.results[0].resource_id; Query statement: $event.annotations.results[0].sql; Query time: $event.annotations.results[0].time; Query URL: $event.annotations.results[0].url; Query log: $event.annotations.results[0].raw_results;
NOTE:
Each variable must be followed by an English semicolon (;). Otherwise, the template replacement fails. |
topic |
String |
Email subject. This parameter is valid only when sub_type is set to email. |
Status code: 400
Parameter |
Type |
Description |
---|---|---|
error_code |
String |
Error code. |
error_msg |
String |
Error message. |
Status code: 500
Parameter |
Type |
Description |
---|---|---|
error_code |
String |
Error code. |
error_msg |
String |
Error message. |
Example Requests
Creating a message template
POST https://{endpoint}/v2/{project_id}/{domain_id}/lts/events/notification/templates { "name" : "xxx", "desc" : "xxxxxx", "type" : [ ], "source" : "LTS", "locale" : "zh-cn", "templates" : [ { "sub_type" : "sms", "content" : "Severity: ${event_severity};\nOccurred: ${starts_at};\nResource ID: ${resources};\nStatistical type: by keyword;\nExpression: $event.annotations.condition_expression;\nCurrent value: $event.annotations.current_value;\nStatistical period: $event.annotations.frequency;" }, { "sub_type" : "dingding", "content" : "Severity: ${event_severity};\nOccurred: ${starts_at};\nResource ID: ${resources};\nStatistical type: by keyword;\nExpression: $event.annotations.condition_expression;\nCurrent value: $event.annotations.current_value;\nStatistical period: $event.annotations.frequency;" }, { "sub_type" : "wechat", "content" : "Severity: ${event_severity};\nOccurred: ${starts_at};\nResource ID: ${resources};\nStatistical type: by keyword;\nExpression: $event.annotations.condition_expression;\nCurrent value: $event.annotations.current_value;\nStatistical period: $event.annotations.frequency;" }, { "sub_type" : "webhook", "content" : "Severity: ${event_severity};\nOccurred: ${starts_at};\nResource ID: ${resources};\nStatistical type: by keyword;\nExpression: $event.annotations.condition_expression;\nCurrent value: $event.annotations.current_value;\nStatistical period: $event.annotations.frequency;" }, { "sub_type" : "email", "content" : "Severity: ${event_severity};\nOccurred: ${starts_at};\nAlarm source: $event.metadata.resource_provider;\nResource type: $event.metadata.resource_type;\nResource ID: ${resources};\nStatistical type: by keyword;\nExpression: $event.annotations.condition_expression;\nCurrent value: $event.annotations.current_value;\nStatistical period: $event.annotations.frequency;\nQuery time: $event.annotations.results[0].time;\nQuery log: $event.annotations.results[0].raw_results;" }, { "sub_type" : "voice", "content" : "Severity: ${event_severity};\nOccurred: ${starts_at};\nResource ID: ${resources};\nStatistical type: by keyword;\nExpression: $event.annotations.condition_expression;\nCurrent value: $event.annotations.current_value;\nStatistical period: $event.annotations.frequency;" } ] }
Example Responses
Status code: 200
The request is successful.
{ "desc" : "description", "locale" : "zh-cn", "name" : "postman-test", "source" : "LTS", "templates" : [ { "content" : "Severity: ${event_severity};\nOccurred: ${starts_at};\nResource ID: ${resources};\nStatistical type: by keyword;\nExpression: $event.annotations.condition_expression;\nCurrent value: $event.annotations.current_value;\nStatistical period: $event.annotations.frequency;", "sub_type" : "sms" }, { "content" : "Severity: ${event_severity};\nOccurred: ${starts_at};\nResource ID: ${resources};\nStatistical type: by keyword;\nExpression: $event.annotations.condition_expression;\nCurrent value: $event.annotations.current_value;\nStatistical period: $event.annotations.frequency;", "sub_type" : "dingding" }, { "content" : "Severity: ${event_severity};\nOccurred: ${starts_at};\nResource ID: ${resources};\nStatistical type: by keyword;\nExpression: $event.annotations.condition_expression;\nCurrent value: $event.annotations.current_value;\nStatistical period: $event.annotations.frequency;", "sub_type" : "wechat" }, { "content" : "Severity: ${event_severity};\nOccurred: ${starts_at};\nResource ID: ${resources};\nStatistical type: by keyword;\nExpression: $event.annotations.condition_expression;\nCurrent value: $event.annotations.current_value;\nStatistical period: $event.annotations.frequency;", "sub_type" : "webhook" }, { "content" : "Severity: ${event_severity};\nOccurred: ${starts_at};\nAlarm source: $event.metadata.resource_provider;\nResource type: $event.metadata.resource_type;\nResource ID: ${resources};\nStatistical type: by keyword;\nExpression: $event.annotations.condition_expression;\nCurrent value: $event.annotations.current_value;\nStatistical period: $event.annotations.frequency;\nQuery time: $event.annotations.results[0].time;\nQuery log: $event.annotations.results[0].raw_results;", "sub_type" : "email" }, { "content" : "Severity: ${event_severity};\nOccurred: ${starts_at};\nResource ID: ${resources};\nStatistical type: by keyword;\nExpression: $event.annotations.condition_expression;\nCurrent value: $event.annotations.current_value;\nStatistical period: $event.annotations.frequency;", "sub_type" : "voice" } ], "type" : [ ] }
Status code: 400
Invalid request. Modify the request based on the description in error_msg before a retry.
{ "error_code" : "LTS.2014", "error_msg" : "desc is invalid!" }
Status code: 500
The server has received the request but encountered an internal error.
{ "error_code" : "LTS.2014", "error_msg" : "Failed to create notification template." }
SDK Sample Code
The SDK sample code is as follows.
Creating a message template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.lts.v2.region.LtsRegion; import com.huaweicloud.sdk.lts.v2.*; import com.huaweicloud.sdk.lts.v2.model.*; import java.util.List; import java.util.ArrayList; public class CreateNotificationTemplateSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); LtsClient client = LtsClient.newBuilder() .withCredential(auth) .withRegion(LtsRegion.valueOf("<YOUR REGION>")) .build(); CreateNotificationTemplateRequest request = new CreateNotificationTemplateRequest(); request.withDomainId("{domain_id}"); CreateNotificationTemplateRequestBody body = new CreateNotificationTemplateRequestBody(); List<SubTemplate> listbodyTemplates = new ArrayList<>(); listbodyTemplates.add( new SubTemplate() .withSubType(SubTemplate.SubTypeEnum.fromValue("sms")) .withContent("Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;") ); listbodyTemplates.add( new SubTemplate() .withSubType(SubTemplate.SubTypeEnum.fromValue("dingding")) .withContent("Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;") ); listbodyTemplates.add( new SubTemplate() .withSubType(SubTemplate.SubTypeEnum.fromValue("wechat")) .withContent("Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;") ); listbodyTemplates.add( new SubTemplate() .withSubType(SubTemplate.SubTypeEnum.fromValue("webhook")) .withContent("Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;") ); listbodyTemplates.add( new SubTemplate() .withSubType(SubTemplate.SubTypeEnum.fromValue("email")) .withContent("Severity: ${event_severity}; Occurred: ${starts_at}; Alarm source: $event.metadata.resource_provider; Resource type: $event.metadata.resource_type; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency; Query time: $event.annotations.results[0].time; Query log: $event.annotations.results[0].raw_results;") ); listbodyTemplates.add( new SubTemplate() .withSubType(SubTemplate.SubTypeEnum.fromValue("voice")) .withContent("Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;") ); body.withTemplates(listbodyTemplates); body.withLocale(CreateNotificationTemplateRequestBody.LocaleEnum.fromValue("zh-cn")); body.withSource("LTS"); body.withDesc("xxxxxx"); body.withName("xxx"); request.withBody(body); try { CreateNotificationTemplateResponse response = client.createNotificationTemplate(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } } |
Creating a message template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# coding: utf-8 import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdklts.v2.region.lts_region import LtsRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdklts.v2 import * if __name__ == "__main__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = os.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] credentials = BasicCredentials(ak, sk) client = LtsClient.new_builder() \ .with_credentials(credentials) \ .with_region(LtsRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateNotificationTemplateRequest() request.domain_id = "{domain_id}" listTemplatesbody = [ SubTemplate( sub_type="sms", content="Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;" ), SubTemplate( sub_type="dingding", content="Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;" ), SubTemplate( sub_type="wechat", content="Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;" ), SubTemplate( sub_type="webhook", content="Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;" ), SubTemplate( sub_type="email", content="Severity: ${event_severity}; Occurred: ${starts_at}; Alarm source: $event.metadata.resource_provider; Resource type: $event.metadata.resource_type; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency; Query time: $event.annotations.results[0].time; Query log: $event.annotations.results[0].raw_results;" ), SubTemplate( sub_type="voice", content="Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;" ) ] request.body = CreateNotificationTemplateRequestBody( templates=listTemplatesbody, locale="zh-cn", source="LTS", desc="xxxxxx", name="xxx" ) response = client.create_notification_template(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) |
Creating a message template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" lts "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := lts.NewLtsClient( lts.LtsClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.CreateNotificationTemplateRequest{} request.DomainId = "{domain_id}" var listTemplatesbody = []model.SubTemplate{ { SubType: model.GetSubTemplateSubTypeEnum().SMS, Content: "Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;", }, { SubType: model.GetSubTemplateSubTypeEnum().DINGDING, Content: "Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;", }, { SubType: model.GetSubTemplateSubTypeEnum().WECHAT, Content: "Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;", }, { SubType: model.GetSubTemplateSubTypeEnum().WEBHOOK, Content: "Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;", }, { SubType: model.GetSubTemplateSubTypeEnum().EMAIL, Content: "Severity: ${event_severity}; Occurred: ${starts_at}; Alarm source: $event.metadata.resource_provider; Resource type: $event.metadata.resource_type; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency; Query time: $event.annotations.results[0].time; Query log: $event.annotations.results[0].raw_results;", }, { SubType: model.GetSubTemplateSubTypeEnum().VOICE, Content: "Severity: ${event_severity}; Occurred: ${starts_at}; Resource ID: ${resources}; Statistical type: by keyword; Expression: $event.annotations.condition_expression; Current value: $event.annotations.current_value; Statistical period: $event.annotations.frequency;", }, } request.Body = &model.CreateNotificationTemplateRequestBody{ Templates: listTemplatesbody, Locale: model.GetCreateNotificationTemplateRequestBodyLocaleEnum().ZH_CN, Source: "LTS", Desc: "xxxxxx", Name: "xxx", } response, err := client.CreateNotificationTemplate(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } } |
For SDK sample code of more programming languages, see the Sample Code tab in API Explorer. SDK sample code can be automatically generated.
Status Codes
Status Code |
Description |
---|---|
200 |
The request is successful. |
400 |
Invalid request. Modify the request based on the description in error_msg before a retry. |
500 |
The server has received the request but encountered an internal error. |
Error Codes
See Error Codes.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot