Help Center/ Simple Message Notification/ API Reference/ Examples/ Example 2: Publishing a Message Using a Message Template
Updated on 2022-12-12 GMT+08:00

Example 2: Publishing a Message Using a Message Template

Scenarios

This section describes how to publish a message using a message template. For details, see Calling APIs.

Procedure

  1. Obtain the token by following instructions in Authentication.
  2. Create a topic.
    1. Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics.
    2. Add X-Auth-Token to the request header.
    3. Specify the following parameters in the request body:
      For details about the parameters, see Creating a Topic.
      {
          "name": "publish_by_template", //(Mandatory) Topic name 
          "display_name": "test", //(Optional) Display name 
          "enterprise_project_id": "0" //Enterprise project ID (mandatory in the enterprise project scenario)
      }
      Example response
      {     
          "request_id": "a8f8161449564f569c19e2a9e3be25c9",     
          "topic_urn": "urn:smn:br-iaas-odin1:0605767e0a00d5762fe7c00d0e33345c:publish_by_template" 
      }
  3. Add a subscription.
    1. Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/{topic_urn}/subscriptions.

      Set topic_urn based on the response in 2.c.

    2. Add X-Auth-Token to the request header.
    3. Specify the following parameters in the request body:
      {     
          "protocol": "sms", //(Mandatory) Subscription protocol
          "endpoint": "86137****3629", //(Mandatory) Email address
          "remark": "Subscription" //(Optional) Description
      }

      Example response

      {
          "subscriptions_result":[{
              "request_id":"126c17f805934e5eadf647b0860c7a0a",
              "subscription_urn":"urn:smn:br-iaas-odin1:0605767e0a00d5762fe7c00d0e33345c:publish_by_template:3e5ac4544a1947b7b0d9f743fe52bdbd",
              "http_code":201}]
      }

      You can add multiple subscriptions to a topic.

    4. The subscription endpoint receives a confirmation message, and you must confirm the subscription in the email.
  4. Create a message template.

    Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/message_template.

  5. Add X-Auth-Token to the request header.
    1. Specify the following parameters in the request body:
      {
          "message_template_name": "sms_template_test", 
          "locale": "zh-cn",
          "content": "{content}", 
          "protocol": "sms"
      }

      Example response

      {
          "request_id": "e67228ac92324a58980e922ca600f8ef",
          "message_template_id": "cdb113996c5d49149fb97af36d9801ce"
      }
    1. The subscription endpoint receives a confirmation message and you must confirm the subscription with your phone.
  6. Use the message template to publish a message.
    1. Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/{topic_urn}/publish.
    2. Add X-Auth-Token to the request header.
    3. Specify the following parameters in the request body:
      {
          "message_template_name": "sms_template_test",
          "tags": {
      	"content": "hello test"
          },
          "subject": "template message test"
      }

      Example response

      {
      	"message_id": "5da965fd9e2c4036a0542317d944af83",
      	"request_id": "c6be4517f985470e8448e42f9d2ec5a2"
      }

      Only confirmed subscription endpoints can receive the message you publish.