nav-img
Advanced

Publishing a Message

Function

This API is used to publish messages to a topic. After a message ID is returned, the message has been saved and is to be pushed to the subscribers of the topic. To send messagesto subscribers successfully, ensure that your message content complies with local laws and regulations.

The following three message formats are supported:

message

message_structure

message_template_name

If the three formats are specified at the same time, they take effect in the following sequence:

message_structure > message_template_name > message

Calling Method

For details, see Calling APIs.

URI

POST /v2/{project_id}/notifications/topics/{topic_urn}/publish

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Specifies the project ID.For details about how to obtain the project ID, see Obtaining the Project ID.

topic_urn

Yes

String

Specifies the resource identifier of the topic, which is unique. To obtain the resource identifier, see Querying Topics.

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

Specifies a user token.

It can be obtained by calling an IAM API. The value of X-Subject-Token in the response header is the user token.

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

subject

No

String

Specifies the message subject, which is used as the email subject when you publish email messages. The length of the subject cannot exceed 512 bytes.

message

No

String

Message content. The message content must be UTF-8-coded and can be no more than 256 KB. If the endpoint is a phone number, the maximum length of an SMS message is 490 characters, or the message may be intercepted. The SMS message cannot contain square brackets ([]).Note:The message sending modes inclue message, message_structure, and message_template_name.Set at least one of them. If you set all of them, they take effect in the following sequence:message_structure > message_template_name > message

message_structure

No

String

Message structure, which is a JSON string. The options are email, sms, http, https, functiongraph, functionstage, dingding, wechat, feishu, and welink. The default protocol is mandatory. If the system fails to match any other protocols, the message using the default protocol is sent. The msgType field must be specified for messages of the DingTalk chatbot, WeCom chatbot, Lark chatbot, or WeLink protocol. The DingTalk, WeCom, or Lark chatbot protocol support messages in text or markdown format. WeLink and WeLink (red version) chatbots support messages in text format.For the DingTalk chatbot protocol, SMN supports the @ group member function through the at fields. When you need to @ group members, set isAtAll to true. You can enter the phone numbers of people who need to be @ for the atMobiles field or enter the DingTalk user IDs of people who need to be @ for the atUserIds field. When you use the atMobiles or atUserIds field, add @ before each phone number or user ID in the message content. For details about the display effect, see the official DingTalk document.Note:The message sending modes inclue message, message_structure, and message_template_name.Set at least one of them. If you set all of them, they take effect in the following sequence:message_structure > message_template_name > message

message_template_name

No

String

Message template name. To obtain the name, see [Querying Message Templates] (ListMessageTemplates.xml).Note:The message sending modes inclue message, message_structure, and message_template_name.Set at least one of them. If you set all of them, they take effect in the following sequence:message_structure > message_template_name > message

tags

No

Map<String,String>

Specifies the dictionary consisting of variable parameters and values. The value cannot be left blank. This parameter is mandatory when you use a message template to publish messages. The key in the dictionary is the parameter name in the message template and contains a maximum of 21 characters. The value in the dictionary is the value after the key in the message template is replaced. The value cannot exceed 1 KB.

time_to_live

No

String

Specifies the maximum retention period of a message in SMN. After the retention period expires, SMN does not send this message. Unit: second Default value: 3600s (one hour) The retention period must be a positive integer less than or equal to 86,400 (3600 x 24).

message_attributes

No

Array of MessageAttribute objects

Specifies the message attribute list.

locale

No

String

Language of the additional system content in messages sent by SMN. If this parameter is not specified, language settings for the account are used. The value is the language supported by the site, for example, zh-cn or en-us.

Table 4 MessageAttribute

Parameter

Mandatory

Type

Description

name

Yes

String

Specifies the attribute name. name can contain 1 to 32 characters, including only lowercase letters, digits, and underscores (_). name cannot start or end with an underscore, nor contain consecutive underscores.

type

Yes

String

Specifies the attribute type. The value can be one of the following:

STRING

STRING_ARRAY

PROTOCOL

value

Yes

Object

Specifies the attribute value.

When you set type to STRING, value can contain 1 to 32 characters, including only letters, digits, and underscores (). When you set type to STRING_ARRAY, value is an array containing 1 to 10 strings. Each string in the array must be unique. Each string in the array must contain 1 to 32 characters, including only letters, digits, and underscores ().

When you set type to PROTOCOL, value is a string array of supported protocol types.

Response Parameters

Status code: 200

Table 5 Response body parameters

Parameter

Type

Description

request_id

String

Specifies the request ID, which is unique.

message_id

String

Specifies the message ID, which is unique.

Status code: 400

Table 6 Response body parameters

Parameter

Type

Description

request_id

String

Specifies the request ID, which is unique.

code

String

Specifies the error code.

message

String

Describes the error message.

Status code: 403

Table 7 Response body parameters

Parameter

Type

Description

request_id

String

Specifies the request ID, which is unique.

code

String

Specifies the error code.

message

String

Describes the error message.

Status code: 404

Table 8 Response body parameters

Parameter

Type

Description

request_id

String

Specifies the request ID, which is unique.

code

String

Specifies the error code.

message

String

Describes the error message.

Status code: 429

Table 9 Response body parameters

Parameter

Type

Description

request_id

String

Specifies the request ID, which is unique.

code

String

Specifies the error code.

message

String

Describes the error message.

Status code: 500

Table 10 Response body parameters

Parameter

Type

Description

request_id

String

Specifies the request ID, which is unique.

code

String

Specifies the error code.

message

String

Describes the error message.

Example Requests

  • Publishing a message

    POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/urn:smn:regionId:f96188c7ccaf4ffba0c9aa149ab2bd57:test_create_topic_v2/publish
    {
    "subject" : "test message v2",
    "message" : "Message test message v2",
    "time_to_live" : "3600",
    "message_attributes" : [ {
    "name" : "smn_protocol",
    "type" : "PROTOCOL",
    "value" : [ "email", "sms" ]
    } ]
    }
  • Publishing messages using a message structure

    POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/urn:smn:regionId:f96188c7ccaf4ffba0c9aa149ab2bd57:test_create_topic_v2/publish
    {
    "subject" : "test message v2",
    "message_structure" : "{ \"default\": \"Message structure test message v2\", \"sms\": \"Message structure test message v2\", \"email\": \"Message structure test message v2\", \"wechat\": \"{\\\"msgtype\\\":\\\"text\\\",\\\"text\\\":{\\\"content\\\":\\\"Message structure test message v2\\\"}}\", \"dingding\": \"{\\\"msgtype\\\":\\\"markdown\\\",\\\"at\\\":{\\\"atMobiles\\\": [\\\"15XXXXXX\\\"],\\\"atUserIds\\\": [\\\"1234567XXX\\\"],\\\"isAtAll\\\": true},\\\"markdown\\\":{\\\"content\\\":\\\"Message structure test message v2 @15XXXXXX @1234567XXX\\\",\\\"title\\\":\\\"Message structure title\\\"}}\", \"feishu\": \"{\\\"msgtype\\\":\\\"text\\\",\\\"text\\\":{\\\"content\\\":\\\"Message structure test message v2\\\"}}\", \"welinkRed\": \"{\\\"msgtype\\\":\\\"text\\\",\\\"text\\\":{\\\"content\\\":\\\"Message structure test message v2\\\"}}\", \"http\": \"Message structure test message v2\", \"https\": \"Message structure test message v2\", \"dingTalkBot\": \"{\\\"msgtype\\\":\\\"markdown\\\",\\\"markdown\\\":{\\\"content\\\":\\\"Message structure test message v2 dingTalkBot markdown\\\",\\\"title\\\":\\\"Message structure title\\\"}}\", \"functionstage\": \"Message structure test message v2\" }",
    "time_to_live" : "3600",
    "message_attributes" : [ {
    "name" : "smn_protocol",
    "type" : "PROTOCOL",
    "value" : [ "email", "sms" ]
    } ]
    }
  • Publishing messages using a template

    POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/urn:smn:regionId:f96188c7ccaf4ffba0c9aa149ab2bd57:test_create_topic_v2/publish
    {
    "subject" : "test message template v2",
    "message_template_name" : "confirm_message",
    "time_to_live" : "3600",
    "tags" : {
    "topic_urn" : "topic_urn3331",
    "topic_id" : "topic_id3332"
    },
    "message_attributes" : [ {
    "name" : "smn_protocol",
    "type" : "PROTOCOL",
    "value" : [ "email", "sms" ]
    } ]
    }

Example Responses

Status code: 200

OK

{
"request_id" : "6a63a18b8bab40ffb71ebd9cb80d0085",
"message_id" : "bf94b63a5dfb475994d3ac34664e24f2"
}

Status Codes

Status Code

Description

200

OK

400

Bad Request

403

Unauthorized

404

Not Found

429

Too Many Requests

500

Internal Server Error

Error Codes