Resources: Template Syntax
Currently we are using a Mustache template engine which has a very simple template presentation.
Template messages with a single response type or a list response type have different available data at the 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 he has in your system.
Let's assume, that your system response 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 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 theexcept fields that your system sends, WeChat Gateway adds some more helpful fields like:
- Index - index of list item starting from zero
- 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 can be accessed via Body variable like {{Body.title}} or {{Body.date_created}}.
Render templates which created with a list response type will process render stage in a different way, than template messages set up with object response type. Renderer will procced template message for each item of the list separately and then join all part into one message, which will be sent to user. Let's look at sstep-by-step process for previos 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 inte 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 this parts into one main message and WeChat Gateway sends this message to the user.