发送模板消息

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

模板消息示例

BaaS.alipay.sendTemplateMessage(data)

参数说明

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

参数类型必填说明
recipient_typeString推送类型,可选值: user_id、user_list、user_group、schema_user
<recipient_params>Array、Integer、String、Object根据recipient_type来填写不同的参数名, 详见下方表格说明
template_idString模板 ID (在小程序后台配置)
submission_typeString模板消息触发条件,form_id 或者 trade_no
keywordsObject关键字 (在小程序后台配置)
schema_nameString数据表名,如果 recipient_type 为 schema_user 则为必填项,表示对该表名的数据表进行用户筛选
pageString点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数。该字段不填则模板无跳转。
can_send_template_messageBoolean是否过滤无效用户
template_message_high_value_userBoolean是否过滤低价值用户
can_send_subscription_messageBoolean是否过滤无效用户
template_message_rate_limitRateLimit发送频率限制
recipient_type 类型recipient_params类型说明
user_iduser_idInteger推送单个用户,传入用户 ID (对应 _userprofile 表中的 id 字段)
user_listuser_listInteger Array推送批量用户,传入用户 id 列表
user_groupuser_group_nameString用户组名,注意这里是提交用户组名称,而不是用户组 id
schema_useruser_profile_filtersString对指定数据表的查询条件,用于筛选用户

RateLimit 类型说明:

属性类型必填说明
intervalNumber时间间隔
limitNumber每批次发送条数

请求示例 - 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. BaaS.alipay.sendTemplateMessage(data).then(res => {
  17. // 发送成功
  18. }, err => {
  19. // 发送失败
  20. })

请求示例 - user_list

  1. let data = {
  2. recipient_type: 'user_list',
  3. user_list: [123, 456, 789],
  4. template_id: "tadfDf23asdi8dfd",
  5. // 其他参数
  6. }
  7. BaaS.alipay.sendTemplateMessage(data)

user_list 的长度不能超过 1000

请求示例 - user_group

  1. let data = {
  2. recipient_type: 'user_group',
  3. user_group_name: '运营人员',
  4. template_id: "tadfDf23asdi8dfd",
  5. // 其他参数
  6. }
  7. BaaS.alipay.sendTemplateMessage(data)

请求示例 - schema_user

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

如果 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. BaaS.alipay.sendTemplateMessage(data)

其中 keyword1, keyword2 为支付宝后台中实际关键词对应的键值

关键词对应键值示例

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

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