微信公众号开发(十)模板消息

模板消息仅用于公众号向用户发送重要的服务通知,只能用于符合其要求的服务场景中,如信用卡刷卡通知,商品购买成功通知等。不支持广告等营销类消息以及其它所有可能对用户造成骚扰的消息。

1、设置所属行业

设置行业可在微信公众平台后台完成,每月可修改行业1次,帐号仅可使用所属行业中相关的模板。

接口:https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN

tem_set_industry.php

<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
$data = '{
          "industry_id1":"1",
          "industry_id2":"4"
       }';
$url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?"
    ."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;

返回:

{"errcode":0,"errmsg":"ok"}

2、获取设置的行业信息

接口:https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=ACCESS_TOKEN

tem_get_industry.php

<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
$url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?"
    ."access_token=".Utils::get_access_token();
$result = Utils::https_request($url);
echo $result;

返回:

primary_industry:账号设置的主营行业

secondary_industry:账号设置的副营行业

{
    "primary_industry": {
        "first_class": "IT科技",
        "second_class": "互联网|电子商务"
    },
    "secondary_industry": {
        "first_class": "IT科技",
        "second_class": "电子技术"
    }
}

3、获得模板ID

从行业模板库选择模板到帐号后台,获得模板ID的过程可在微信公众平台后台完成。

接口:https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN

tem_add_template.php

template_id_short:模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式

<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
$data = '{
           "template_id_short":"TM00015"
       }';
$url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?"
    ."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;

返回:

{
    "errcode": 0,
    "errmsg": "ok",
    "template_id": "V4Gx8O_WixAGUIkYwjm2EuWBbq-L-wz8KUJzhMoUJXk"
}

4、获得模板列表

获取已添加至帐号下所有模板列表,可在微信公众平台后台中查看模板列表信息。

接口:https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN

tem_private_template.php

@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
$url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?"
    ."access_token=".Utils::get_access_token();
$result = Utils::https_request($url);
echo $result;

返回:

{
    "template_list": [
        {
            "template_id": "V4Gx8O_WixAGUIkYwjm2EuWBbq-L-wz8KUJzhMoUJXk",
            "title": "订单支付成功",
            "primary_industry": "IT科技",
            "deputy_industry": "互联网|电子商务",
            "content": "{{first.DATA}}\n\n支付金额:{{orderMoneySum.DATA}}\n商品信息:{{orderProductName.DATA}}\n{{Remark.DATA}}",
            "example": "我们已收到您的货款,开始为您打包商品,请耐心等待: )\n支付金额:30.00元\n商品信息:我是商品名字\n\n如有问题请致电400-828-1878或直接在微信留言,小易将第一时间为您服务!"
        }
    ]
}

5、删除模板

接口:https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=ACCESS_TOKEN

tem_del_template.php

<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
$data = '{
            "template_id" : "Dyvp3-Ff0cnail_CDSzk1fIc6-9lOkxsQE7exTJbwUE"
       }';
$url = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?"
    ."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;

返回:

{
   "errcode" : 0,
   "errmsg" : "ok"
}

6、发送模板消息

接口:https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

tem_send.php

<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
$data = '{
    "touser": "o4WmZ0h-4huBUVQUczx2ezaxIL9c",
    "template_id": "V4Gx8O_WixAGUIkYwjm2EuWBbq-L-wz8KUJzhMoUJXk",
    "url": "http://www.baidu.com",
    "data": {
        "first": {
            "value": "我们已收到您的货款,开始为您打包商品,请耐心等待:",
            "color": "#173177"
        },
        "orderMoneySum": {
            "value": "30.00元",
            "color": "#173177"
        },
        "orderProductName": {
            "value": "洗发水",
            "color": "#173177"
        },
        "Remark": {
            "value": "如有问题请致电400-828-1878或直接在微信留言,小易将第一时间为您服务!",
            "color": "#173177"
        }
    }
}';
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?"
    ."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;

返回:

{"errcode":0,"errmsg":"ok","msgid":414862553}

7、事件推送

在模版消息发送任务完成后,微信服务器会将是否送达成功作为通知,发送到开发者中心中填写的服务器配置地址中。

成功送达推送:

<xml>
    <ToUserName>
        <![CDATA[gh_6b9aa8a6f1e2]]>
    </ToUserName>
    <FromUserName>
        <![CDATA[o4WmZ0h-4huBUVQUczx2ezaxIL9c]]>
    </FromUserName>
    <CreateTime>1505407812</CreateTime>
    <MsgType>
        <![CDATA[event]]>
    </MsgType>
    <Event>
        <![CDATA[TEMPLATESENDJOBFINISH]]>
    </Event>
    <MsgID>414862553</MsgID>
    <Status>
        <![CDATA[success]]>
    </Status>
</xml>

送达由于用户拒收(用户设置拒绝接收公众号消息)而失败时,推送的XML如下:

<xml>
    <ToUserName>
        <![CDATA[gh_7f083739789a]]>
    </ToUserName>
    <FromUserName>
        <![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]>
    </FromUserName>
    <CreateTime>1395658984</CreateTime>
    <MsgType>
        <![CDATA[event]]>
    </MsgType>
    <Event>
        <![CDATA[TEMPLATESENDJOBFINISH]]>
    </Event>
    <MsgID>200163840</MsgID>
    <Status>
        <![CDATA[failed:user block]]>
    </Status>
</xml>

送达由于其他原因失败时,推送的XML如下:

<xml>
    <ToUserName>
        <![CDATA[gh_7f083739789a]]>
    </ToUserName>
    <FromUserName>
        <![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]>
    </FromUserName>
    <CreateTime>1395658984</CreateTime>
    <MsgType>
        <![CDATA[event]]>
    </MsgType>
    <Event>
        <![CDATA[TEMPLATESENDJOBFINISH]]>
    </Event>
    <MsgID>200163840</MsgID>
    <Status>
        <![CDATA[failed: system failed]]>
    </Status>
</xml>

相关博客

微信公众号开发(一)服务器及接口的配置

微信公众号开发(二)基础接口

微信公众号开发(三)获取access_token

微信公众号开发(四)自定义菜单

微信公众号开发(五)个性化菜单

微信公众号开发(六)素材管理

微信公众号开发(七)发送客服消息

微信公众号开发(八)用户管理

微信公众号开发(九)群发消息接口

微信公众号开发(十)模板消息

微信公众号开发(十一)生成带参数二维码

微信公众号开发(十二)OAuth2.0网页授权




 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值