微信公眾號開發(十)模板訊息
阿新 • • 發佈:2019-02-04
模板訊息僅用於公眾號向用戶傳送重要的服務通知,只能用於符合其要求的服務場景中,如信用卡刷卡通知,商品購買成功通知等。不支援廣告等營銷類訊息以及其它所有可能對使用者造成騷擾的訊息。
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返回: primary_industry:賬號設定的主營行業<?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;
secondary_industry:賬號設定的副營行業
{
"primary_industry": {
"first_class": "IT科技",
"second_class": "網際網路|電子商務"
},
"secondary_industry": {
"first_class": "IT科技",
"second_class": "電子技術"
}
}
3、獲得模板ID
從行業模板庫選擇模板到帳號後臺,獲得模板ID的過程可在微信公眾平臺後臺完成。<?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>