微信服务号 - 模板消息
模板消息是服务号独有的能力,用于向用户发送重要的服务通知,如订单状态变更、支付成功通知、物流提醒等。
使用规则
| 规则 | 说明 | 注意事项 |
|---|---|---|
| 触发条件 | 需用户主动交互后触发 | 支付成功、提交表单、关注等行为后发送 |
| 模板选择 | 从模板库中选取或申请新模板 | 不可自定义模板内容格式,只能填充数据 |
| 发送频率 | 无次数限制 | 但不能用于营销推广,否则会被封禁 |
| 跳转能力 | 可跳转网页或小程序 | 点击模板消息可打开详情页 |
发送模板消息
async function sendTemplateMsg(openid, templateId, data, url) {
const token = await getAccessToken()
const msg = {
touser: openid,
template_id: templateId,
url: url,
data: data
}
const res = await axios.post(
`https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${token}`,
msg
)
return res.data
}
sendTemplateMsg(
'用户openid',
'模板ID',
{
first: { value: '您的订单已发货', color: '#173177' },
keyword1: { value: 'DD20250214001' },
keyword2: { value: '顺丰快递 SF1234567890' },
keyword3: { value: '2025-02-14 10:30:00' },
remark: { value: '点击查看物流详情', color: '#FF6600' }
},
'https://example.com/order/detail?id=001'
)