发送模板消息

如果用户在执行了某一特定的操作的时候,例如用户进行了支付操作,需要给用户发送一个关于订单的相关的信息的消息的时候,可以调用该接口给特定的用户群体发送一个特定的模板消息,

{% if platform == ‘wechat’ %} 模板消息示例

BaaS.sendTemplateMessage(data) {% elif platform == ‘alipay’ %} 模板消息示例

BaaS.alipay.sendTemplateMessage(data) {% elif platform == ‘baidu’ %} BaaS.baidu.sendTemplateMessage(data) {% elif platform == ‘bytedance’ %} BaaS.bytedance.sendTemplateMessage(data)

info SDK >= 3.7 {% else %} BaaS.qq.sendTemplateMessage(data) {% endif %}

参数说明

data 是 Object 类型,它包括以下几个属性

{% if platform == ‘wechat’ %} | 参数 | 类型 | 必填 | 说明 | | :——————— | :——- | :—- | :— | | recipient_type | String | 是 | 推送类型,可选值: user_id、user_list、user_group、schema_user | | <recipient_params> | Array、Integer、String、Object | 是 | 根据recipient_type来填写不同的参数名, 详见下方表格说明 | | template_id | String | 是 | 模板 ID | | submission_type | String | 是 | 模板消息触发条件,form_id 或者 prepay_id | | keywords | Object | 是 | 关键字(可在 知晓云-模板消息 配置)| | schema_name | String | 否 | 数据表名,如果 recipient_type 为 schema_user 则为必填项,表示对该表名的数据表进行用户筛选 | | page | String | 否 | 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数。该字段不填则模板无跳转。| | emphasis_keyword| String | 否 | 模板需要放大的关键词,不填则默认无放大,例:keyword1.DATA| | can_send_template_message | Boolean | 否 | 是否过滤无效用户 | | template_message_high_value_user | Boolean | 否 | 是否过滤低价值用户 | | can_send_subscription_message | Boolean | 否 | 是否过滤无效用户 | | template_message_rate_limit | RateLimit | 否 | 发送频率限制 |

{% elif platform == ‘baidu’ %} | 参数 | 类型 | 必填 | 说明 | | :——————— | :——- | :—- | :— | | recipient_type | String | 是 | 推送类型,可选值: user_id、user_list、user_group、schema_user | | <recipient_params> | Array、Integer、String、Object | 是 | 根据recipient_type来填写不同的参数名, 详见下方表格说明 | | template_id | String | 是 | 模板 ID | | submission_type | String | 是 | 模板消息触发条件,form_id 或者 order_id | | keywords | Object | 是 | 关键字(可在 知晓云-模板消息 配置)| | schema_name | String | 否 | 数据表名,如果 recipient_type 为 schema_user 则为必填项,表示对该表名的数据表进行用户筛选 | | page | String | 否 | 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数。该字段不填则模板无跳转。| | can_send_template_message | Boolean | 否 | 是否过滤无效用户 | | template_message_high_value_user | Boolean | 否 | 是否过滤低价值用户 | | can_send_subscription_message | Boolean | 否 | 是否过滤无效用户 | | template_message_rate_limit | RateLimit | 否 | 发送频率限制 |

{% elif platform == ‘bytedance’ %} | 参数 | 类型 | 必填 | 说明 | | :——————— | :——- | :—- | :— | | recipient_type | String | 是 | 推送类型,可选值: user_id、user_list、user_group、schema_user | | <recipient_params> | Array、Integer、String、Object | 是 | 根据recipient_type来填写不同的参数名, 详见下方表格说明 | | template_id | String | 是 | 模板 ID | | app_name | AppName | 是 | 应用名称。目前支持 toutiao(今日头条) | | submission_type | String | 是 | 模板消息触发条件,暂时只支持 form_id | | keywords | Object | 是 | 关键字(可在 知晓云-模板消息 配置)| | schema_name | String | 否 | 数据表名,如果 recipient_type 为 schema_user 则为必填项,表示对该表名的数据表进行用户筛选 | | page | String | 否 | 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数。该字段不填则模板无跳转。| | can_send_template_message | Boolean | 否 | 是否过滤无效用户 | | template_message_high_value_user | Boolean | 否 | 是否过滤低价值用户 | | can_send_subscription_message | Boolean | 否 | 是否过滤无效用户 | | template_message_rate_limit | RateLimit | 否 | 发送频率限制 |

{% else %} | 参数 | 类型 | 必填 | 说明 | | :——————— | :——- | :—- | :— | | recipient_type | String | 是 | 推送类型,可选值: user_id、user_list、user_group、schema_user | | <recipient_params> | Array、Integer、String、Object | 是 | 根据recipient_type来填写不同的参数名, 详见下方表格说明 | | template_id | String | 是 | 模板 ID (在小程序后台配置) | | submission_type | String | 是 | 模板消息触发条件,form_id 或者 trade_no | | keywords | Object | 是 | 关键字 (在小程序后台配置) | | schema_name | String | 否 | 数据表名,如果 recipient_type 为 schema_user 则为必填项,表示对该表名的数据表进行用户筛选 | | page | String | 是 | 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数。该字段不填则模板无跳转。| | can_send_template_message | Boolean | 否 | 是否过滤无效用户 | | template_message_high_value_user | Boolean | 否 | 是否过滤低价值用户 | | can_send_subscription_message | Boolean | 否 | 是否过滤无效用户 | | template_message_rate_limit | RateLimit | 否 | 发送频率限制 |

{% endif %}

{% if platform == ‘bytedance’ %} AppName 支持以下值:

名称 说明
toutiao 今日头条

{% endif %}

recipient_type 类型 recipient_params 类型 说明
user_id user_id Integer 推送单个用户,传入用户 ID (对应 _userprofile 表中的 id 字段)
user_list user_list Integer Array 推送批量用户,传入用户 id 列表
user_group user_group_name String 用户组名,注意这里是提交用户组名称,而不是用户组 id
schema_user user_profile_filters String 对指定数据表的查询条件,用于筛选用户

RateLimit 类型说明:

属性 类型 必填 说明
interval Number 时间间隔
limit Number 每批次发送条数

请求示例 - user_id

  1. let data = {
  2. recipient_type: 'user_id',
  3. user_id: 23425,
  4. template_id: "tadfDf23asdi8dfd",
  5. submission_type: "form_id",
  6. page: "pages/index/index",
  7. keywords: {
  8. keyword1: {
  9. value: "书籍",
  10. },
  11. keyword2: {
  12. value: "50.5",
  13. }
  14. }
  15. }
  16. {% if platform == 'wechat' %}
  17. BaaS.sendTemplateMessage(data).then(res => {
  18. // 发送成功
  19. }, err => {
  20. // 发送失败
  21. })
  22. {% elif platform == 'alipay' %}
  23. BaaS.alipay.sendTemplateMessage(data).then(res => {
  24. // 发送成功
  25. }, err => {
  26. // 发送失败
  27. })
  28. {% elif platform == 'baidu' %}
  29. BaaS.baidu.sendTemplateMessage(data).then(res => {
  30. // 发送成功
  31. }, err => {
  32. // 发送失败
  33. })
  34. {% elif platform == 'bytedance' %}
  35. BaaS.bytedance.sendTemplateMessage(data).then(res => {
  36. // 发送成功
  37. }, err => {
  38. // 发送失败
  39. })
  40. {% else %}
  41. BaaS.qq.sendTemplateMessage(data).then(res => {
  42. // 发送成功
  43. }, err => {
  44. // 发送失败
  45. })
  46. {% endif %}

请求示例 - user_list

  1. let data = {
  2. recipient_type: 'user_list',
  3. user_list: [123, 456, 789],
  4. template_id: "tadfDf23asdi8dfd",
  5. // 其他参数
  6. }
  7. {% if platform == 'wechat' %}
  8. BaaS.sendTemplateMessage(data)
  9. {% elif platform == 'alipay' %}
  10. BaaS.alipay.sendTemplateMessage(data)
  11. {% elif platform == 'baidu' %}
  12. BaaS.baidu.sendTemplateMessage(data)
  13. {% elif platform == 'bytedance' %}
  14. BaaS.bytedance.sendTemplateMessage(data)
  15. {% else %}
  16. BaaS.qq.sendTemplateMessage(data)
  17. {% endif %}

info user_list 的长度不能超过 1000

请求示例 - user_group

  1. let data = {
  2. recipient_type: 'user_group',
  3. user_group_name: '运营人员',
  4. template_id: "tadfDf23asdi8dfd",
  5. // 其他参数
  6. }
  7. {% if platform == 'wechat' %}
  8. BaaS.sendTemplateMessage(data)
  9. {% elif platform == 'alipay' %}
  10. BaaS.alipay.sendTemplateMessage(data)
  11. {% elif platform == 'baidu' %}
  12. BaaS.baidu.sendTemplateMessage(data)
  13. {% elif platform == 'bytedance' %}
  14. BaaS.bytedance.sendTemplateMessage(data)
  15. {% else %}
  16. BaaS.qq.sendTemplateMessage(data)
  17. {% endif %}

请求示例 - schema_user

schema_user 允许同时存在 user_profile_filters 和 user_group_name 参数

info 如果 recipient_typeschema_user 且参数中包含 user_group_name 字段, 则 user_profile_filters 字段中,最外层的 $and$or 不能省略。

  1. let data = {
  2. recipient_type: 'schema_user',
  3. user_profile_filters: {
  4. "$and": [
  5. {
  6. "is_authorize": true
  7. },
  8. {
  9. "array_field": {
  10. "$in": [
  11. "value_1",
  12. "value_2"
  13. ]
  14. }
  15. }
  16. ]
  17. },
  18. user_group_name: ['运营人员', '技术人员'],
  19. template_id: "tadfDf23asdi8dfd",
  20. // 其他参数
  21. }
  22. {% if platform == 'wechat' %}
  23. BaaS.sendTemplateMessage(data)
  24. {% elif platform == 'alipay' %}
  25. BaaS.alipay.sendTemplateMessage(data)
  26. {% elif platform == 'baidu' %}
  27. BaaS.baidu.sendTemplateMessage(data)
  28. {% elif platform == 'bytedance' %}
  29. BaaS.bytedance.sendTemplateMessage(data)
  30. {% else %}
  31. BaaS.qq.sendTemplateMessage(data)
  32. {% endif %}

{% if platform == ‘wechat’ %} 其中 keyword1, keyword2 为微信后台中实际关键词对应的键值

关键词对应键值示例

info 如果 submission_type = 'form_id',请确保在调用 BaaS.sendTemplateMessage 前,已在小程序端调用 wx.BaaS.wxReportTicket上报模版消息所需的 formId {% elif platform == ‘alipay’ %} 其中 keyword1, keyword2 为支付宝后台中实际关键词对应的键值

关键词对应键值示例

info 如果 submission_type = 'form_id',请确保在调用 BaaS.alipay.sendTemplateMessage 前,已在小程序端调用 my.BaaS.reportTicket上报模版消息所需的 formId {% elif platform == ‘baidu’ %} 其中 keyword1, keyword2 为百度后台中实际关键词对应的键值

info 如果 submission_type = 'form_id',请确保在调用 BaaS.baidu.sendTemplateMessage 前,已在小程序端调用 swan.BaaS.reportTicket上报模版消息所需的 formId {% elif platform == ‘bytedance’ %} 其中 keyword1, keyword2 为字节跳动小程序后台中实际关键词对应的键值

info 如果 submission_type = 'form_id',请确保在调用 BaaS.bytedance.sendTemplateMessage 前,已在小程序端调用 tt.BaaS.reportTicket上报模版消息所需的 formId {% else %} 其中 keyword1, keyword2 为 QQ 后台中实际关键词对应的键值

info 如果 submission_type = 'form_id',请确保在调用 BaaS.qq.sendTemplateMessage 前,已在小程序端调用 qq.BaaS.reportTicket上报模版消息所需的 formId {% endif %}

user_profile_filters 语法

操作符 示例 示例说明
= { a:{ $eq: ‘123’ } } a == ‘123’
< { a: { $lt: 22 } } a 小于 22
<= { a: { $lte: 22 } } a 小于等于 22
> { a: { $gt: 22 } } a 大于 22
>= { a: { $gte: 22 } } a 大于等于 22
in { a: { $in: [123, 456] } } a 存在于 [123, 456] , eg: 123 in [123, 456]
range { a: { $range: [0, 5] } } a 存在于 [0, 1, 2, 3, 4] 中, eg: 1 in [0, 1, 2]
!= { a: { $ne: ‘123’ } } a 不等于 ‘123’, eg ‘456’ != ‘123’
not in { a: { $nin: [123, 456] } } a 不在 [123, 456] 中, eg: 888 不在 [123, 456] 中
contains { a: { $contains: ‘123’} } a 包含 ‘123’, eg: ‘abc123’ 包含 ‘123’
regex { a: { $regex: ‘123’, $options: ‘g’} } a.match(/123/g)
all { a: { $all: [1, 2, 3] } } a 包含了 [1, 2, 3] , eg: [1, 2, 3] 包含了 [1, 2]
is null { a: { $isnull: true} } } a 是否为空
center { a: { $center: {“radius”: 123, “coordinates”: [1, 2]} } } 请参考withincircle
intersects { a: { $intersects: {“type”: GEOJSON, “coordinates”: [1, 2]} }} 请参考include
nearsphere { a: {“$nearsphere”:{“geometry”:{“type”:”Point”,”coordinates”:[1,2]},”min_distance”:3,”max_distance”:4}} } 请参考withinRegion