Resources: Template Message Syntax

All resources

WeChat Gateway user Mustache template engine is the engine used to render messages from template and input data.

Template messages with a single response type or a list response type have different available data at a render stage.

First let's look at single object response type.

Let's assume that your API server provides information about when the user last logged in and the user want to access this information. Also assume that the system returns the following JSON:

{
  "error": null,
  "result": {
    "user_name": "Bill Smith",
    "last_activity": "18th Feb"
  }     
}

On the WeChat Gateway side you have set up this template:

Hello, {{user_name}}
Your last activity was at {{last_activity}}

Thanks for using our service. 

After the rendering step, the user will get following message:

Hello, Bill Smith
Your last activity was at 18th Feb

Thanks for using our service.

List object response type

Let's imagine that the user can receive a list of all the papers that they have in your system. Assume, that your system response is with the following body:

{
  "error": null,
  "result": [
    {
      "title": "My great paper name #1",
      "date_created": "18th Feb, 2018"
    }, {
      "title": "My great paper name #2",
      "date_created": "11th April, 2018"
    }, {
      "title": "My greatest paper",
      "date_created": "15th May, 2018"
    }
  ]
}

On the WeChat Gateway side you create following template:

{{#IsFirst}}
You created {{Size}} paper(s) on our system
{{/IsFirst}}

Title: {{Body.title}}
Created at: {{Body.date_created}}
{{#IsLast}}

If you want to create another paper follow this link
.. some link here ..
{{/IsLast}}

WeChat Gateway will create the following message for the user:

You created 3 paper(s) on our system

Title: My great paper name #1
Created at: 18th Feb, 2018

Title: My great paper name #2
Created at: 11th April, 2018

Title: My greatest paper
Created at: 15th May, 2018

If you want to create another paper follow this link
.. insert a link here ..

You can see the except fields that your system sends. WeChat Gateway adds some more helpful fields, for example:

  • Index - index of list item starting from zero (0)
  • Size - size of the list
  • IsLast - true if template is rendered for the last element in the list
  • IsFirst - true if template is rendered for the first element in the list

Also please note that for the list response type object's fields must be accessed via Body variable like {{Body.title}} or {{Body.date_created}}.

Processing of Template Messages with a list response type can also be set up differently than the above example. The renderer will process the template message for each item of the list separately and then join all the parts into one message. Let's look at step-by-step process for the previous example:

First item of the list

{
  "IsFirst": true,
  "IsLast": false,
  "Size": 3,
  "Index": 0,
  "Body": {
    "title": "My great paper name #1",
    "date_created": "18th Feb, 2018"
  }
} 

Renderer will create the following part of the message:

You created 3 paper(s) on our system

Title: My great paper name #1
Created at: 18th Feb, 2018

Second part of the list

{
  "IsFirst": false,
  "IsLast": false,
  "Size": 3,
  "Index": 1,
  "Body": {
    "title": "My great paper name #2",
    "date_created": "11th April, 2018"
  }
} 

Renderer will create the following part of the message:

Title: My great paper name #2
Created at: 11th April, 2018

Third part of the list

{
  "IsFirst": false,
  "IsLast": true,
  "Size": 3,
  "Index": 2,
  "Body": {
    "title": "My greatest paper",
    "date_created": "15th May, 2018"
  }
} 

Renderer will create the following part of the message:

Title: My greatest paper
Created at: 15th May, 2018

If you want to create another paper follow this link
.. some link here ..

The render will then join all the parts into one main message and WeChat Gateway sends this message to the user.

    •  
    •