支付宝小程序Serverless 营销能力API·cloud.marketing.templateMessage.send

2020-09-18 14:32 更新

cloud.marketing.templateMessage.send 是小程序模板消息 场景下发送模板消息的接口。

使用该API前,请确认你的小程序应用已经添加 模板消息 功能

入参说明

字段名 类型 必填 说明
toUserId String 发送消息的支付宝账号
formId String 用户发生的交易行为的交易号,或者用户在小程序产生表单提交的表单号,用于信息发送的校验
userTemplateId String 用户申请的模板id号,固定的模板id会发送固定的消息
page String 小程序的跳转页面,用于消息中心用户点击之后详细跳转的小程序页面,例如:page/component/index
data String 开发者需要发送模板消息中的自定义部分来替换模板的占位符,例如:{"keyword1": {"value" : "12:00"},"keyword2": {"value" : "20180808"},"keyword3": {"value" : "支付宝"}}
appAuthToken String 三方代调用应用访问令牌,目前仅在云函数中调用支持。

调用示例

在云函数中调用

// 云函数中使用云调用无需引入其他依赖,只需要使用ctx.cloud调用
module.exports = async function (ctx) {
  const res = await ctx.cloud.marketing.templateMessage.send({
    // 参数接收自云函数调用端传入的参数
    toUserId: ctx.args.toUserId,
    formId: ctx.args.formId,
    page: ctx.args.page,
    data: ctx.args.data,
    userTemplateId: ctx.args.userTemplateId  });
  return res;
};

支持传入appAuthToken进行三方代调用

// 云函数中使用云调用无需引入其他依赖,只需要使用ctx.cloud调用
module.exports = async function (ctx) {
  const res = await ctx.cloud.marketing.templateMessage.send({
    // 参数接收自云函数调用端传入的参数
    toUserId: ctx.args.toUserId,
    formId: ctx.args.formId,
    page: ctx.args.page,
    data: ctx.args.data,
    userTemplateId: ctx.args.userTemplateId  }, {
    // appAuthToken参数接收自云函数调用处传入的参数
    appAuthToken: ctx.args.appAuthToken
  });
  return res;
};

在小程序页面调用

alipay-serverless-sdk 版本&=1.0.0

import cloud from 'alipay-serverless-sdk';

const res = await cloud.marketing.templateMessage.send({
    // 参数接收自云函数调用端传入的参数
    toUserId: '2088102122458832',
    formId: '2017010100000000580012345',
    page: 'page/component/index',
    data: '{"keyword1": {"value" : "12:00"}}',
    userTemplateId: 'MDI4YzIxMDE2M2I5YTQzYjUxNWE4MjA4NmU1MTIyYmM='  });

alipay-serverless-sdk 版本<1.0.0

import cloud from 'alipay-serverless-sdk';

const res = await cloud.marketing.templateMessage.send('2088102122458832', '2017010100000000580012345', 'MDI4YzIxMDE2M2I5YTQzYjUxNWE4MjA4NmU1MTIyYmM=','page/component/index','{"keyword1": {"value" : "12:00"}}');

返回数据示例

{
    "code":"10000",
    "msg":"Success"
}
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号