Help Center/ Log Tank Service/ API Reference/ APIs/ Message Template Management/ Previewing the Email Format of a Message Template
Updated on 2024-12-20 GMT+08:00

Previewing the Email Format of a Message Template

Function

This API is used to preview the email format of a notification template.

Calling Method

For details, see Calling APIs.

URI

POST /v2/{project_id}/{domain_id}/lts/events/notification/templates/view

Table 1 Path Parameters

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.

Minimum: 32

Maximum: 32

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.

Minimum: 32

Maximum: 32

Request Parameters

Table 2 Request header 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.

Minimum: 1000

Maximum: 2000

Content-Type

Yes

String

Set this parameter to application/json;charset=UTF-8.

Minimum: 30

Maximum: 30

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

templates

Yes

String

Email template content.

Minimum: 2

Maximum: 1024

language

Yes

String

Language: zh-cn (Chinese), en-us (English)

source

Yes

String

Source. The value can only be LTS.

Minimum: 3

Maximum: 3

Response Parameters

Status code: 200

Table 4 Response body parameters

Parameter

Type

Description

template

String

The value is an HTML text and needs to be parsed before being displayed.

Minimum: 2

Maximum: 1024

Example Requests

Previewing the email format of a message template

POST https://{endpoint}/v2/{project_id}/{domain_id}/lts/events/notification/templates/view

{ 
  "templates": "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;" 
  "language": "en-us", 
  "source": "LTS" 
}

Example Responses

Status code: 200

The request is successful.

{
  "template" : "<style>    span {        display: inline-block;        float: left;        font-size: 14px;    }    b {        display: inline-block;        float: left;        color: #252B3A;        font-size: 14px    }</style><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"       style=\"font-family:HuaweiFont,Helvetica,Arial,PingFangSC-Regular,Hiragino Sans GB,Microsoft YaHei,XXXX,Microsoft JhengHei;border-spacing:0px 14px;font-size:14px;padding-left: 30px;line-height:25px;\">    <thead>    <tr style=\"font-size:14px;\">        <td colspan=\"2\" style=\"line-height:28px;color:#6e6e6e;font-size:14px\">            <b>Dear&nbsp;</b>            <b>Huawei Cloud user,</b>            <b>&nbsp; </b>        </td>    </tr>    </thead>    <tr>        <td colspan=\"2\">            <span>Your alarm rules set in the</span>            <b>CN North-Beijing1</b>            <span>&nbsp;region</span>            <b>&nbsp;&nbsp;</b>            <span>have a new</span>            <span>O&M notification</span>            <span>. For more information, log in to LTS. </span>            <br>            <br>        </td>    </tr>    <tr style=\"font-size:14px;\">        <td colspan=\"2\">            <p style=\"margin-bottom: -20px; margin-top: -26px;\">                <span style=\"color:#252B3A;line-height:24px\">The details are as follows:</span>            </p>        </td>    </tr>    <td><div>Severity: critical<br>Occurred: 2022-03-21 18:23:20 GMT+08:00;<br>Alarm source: NA;<br>Resource type: NA;<br>Resource ID: CCE;<br>Statistical type: by keyword;<br>Expression: NA;<br>Current value: NA;<br>Statistical period: NA;<br>Query time: NA;<br>Query log: NA;<br><div/></td>  </table>"
}

SDK Sample Code

The SDK sample code is as follows.

Previewing the email format of 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
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.*;


public class ListNotificationTemplateSolution {

    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();
        ListNotificationTemplateRequest request = new ListNotificationTemplateRequest();
        request.withDomainId("{domain_id}");
        PreviewTemplateBody body = new PreviewTemplateBody();
        body.withSource("LTS");
        body.withLanguage(PreviewTemplateBody.LanguageEnum.fromValue("en-us"));
        body.withTemplates("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;");
        request.withBody(body);
        try {
            ListNotificationTemplateResponse response = client.listNotificationTemplate(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());
        }
    }
}

Previewing the email format of 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
# 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 = ListNotificationTemplateRequest()
        request.domain_id = "{domain_id}"
        request.body = PreviewTemplateBody(
            source="LTS",
            language="en-us",
            templates="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;"
        )
        response = client.list_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)

Previewing the email format of 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
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.ListNotificationTemplateRequest{}
	request.DomainId = "{domain_id}"
	request.Body = &model.PreviewTemplateBody{
		Source: "LTS",
		Language: model.GetPreviewTemplateBodyLanguageEnum().EN_US,
		Templates: "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;",
	}
	response, err := client.ListNotificationTemplate(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.

Error Codes

See Error Codes.