?? ??? ??? ??? ?????? ?? ??? ??? ????? ????? ?? ??? ??? ???? ?? ? ????. ??? ???? ?? ????? ????? ??? ??? ??? ??? ? ???? ?? ?? ?? ??, ?? ?? ?? ?? ? ?? ??? ???? ??? ??????? ??? ? ????. ?? ?? ??? ???? ???? ???? ?? ?? ???? ???? ????. ? ???? ?? C# ??? ?? ?? ??? ??? ?? ?? ?? ? ??? ?????.
“??? ??? ??? ??? ??? ??? ??? ?? ??? ?? ??? ????.” WeChat? ??? ??? ?? ??? ???? ?? ?? ?? ???? ??? ?? ???? WeChat? ??? ?????? ??? ?? ?? ???? ?? ???? ??? ??? ??, ??? ??? ? ?? ???? ?? ??? ??? ?? ??? ??? ??? ??? ???? ?? ??? ?? ??? ??????. ???? ?? ????? ?? ???? ???? ????? ??? ??? ?????.
1. ?? ?? ?? ? ??? ??
??? ??? ???? ???? ???, ??? ?? ????? ?? ??? ???? ????? ????. ???? ?? ??, ?? ??, ?? ??, ?? ???, ?? ?? ?? ? ??? ?????? ??? ? ?? ???? ?????.
?? ??? ???? ???? ?? ?? ??? ??????? ??? ???? ???? ???(?? ??? 15????). ???? ? ???? ???? ? ???? [??? ID]?? ??? ?? ?????. ??? ?? ? ? ??? ID? ?? ????.
? ????? ??? ???? ??? ?? ??? ????.
WeChat ? ???? ??? ?? ?????. ??? ??? ??? ?? ?? ??? ?? ?? ???? ??? ? ??? ?? ?? ????. ??? ???? ????(? 4?? ???? ???? ?? ? ??). ?? ?? ???? ??? ?? ? ?? ??? ?? ?? ??? ????? ???? ?? ?????. ??? ???? ??? ? ??? WeChat ???? ?? ??? ???? ???? ?? ??? ? ?? ?? ???? ?? ??? ? ??? ???? ????? ? ?? ?? ???? ??? ? ????.
WeChat? ????? ??? ??? ???? ????. ??? ??? ??? ?? ???? ?? ??? ??? ?? ??, ?? ??? ?? ?? ?? ?? ?? ???? ??? ???? ???? ??? ???????. ?? ??, ?? ??, ?? ?? ? ?? ??? ??? ??? WeChat? ??? ??? ?? ??? ???? ?????. ???? ??? ??? ??? ?? ???? ?? ??? ?? ??, ??? ?? ?? ???, ?? ?? ? ?? ?? ?? ??, ?? ??? ?? ? ?? ?? ?? ??? ??? ? ????. ?? ? ??? WeChat? ?? ???? ??? ??? ???? ?? ????? ?? ?????.
2. ??? ???? ??? ?????
??? ?? ??? ?? ??? ??? ?? ??? ? ????. ??? ??? ??? ????? ????? ???? ???, ???? ???? ???. ??? API? ?????? ??? ??? API? ???? ???? ??? ??? ??????
?? ??? ??? ?? ??? ???????.
1、所有服務(wù)號都可以在功能->添加功能插件處看到申請模板消息功能的入口,但只有認證后的服務(wù)號才可以申請模板消息的使用權(quán)限并獲得該權(quán)限; 2、需要選擇公眾賬號服務(wù)所處的2個行業(yè),每月可更改1次所選行業(yè); 3、在所選擇行業(yè)的模板庫中選用已有的模板進行調(diào)用; 4、每個賬號可以同時使用15個模板。 5、當(dāng)前每個模板的日調(diào)用上限為10萬次【2014年11月18日將接口調(diào)用頻率從默認的日1萬次提升為日10萬次,可在MP登錄后的開發(fā)者中心查看】。
??? ???? ?? ??? ??? ????.
1 ?? ??
2 Get the ??? ID
3 ??? ??? ???
4 ??? ??
2.1 ?? ??
?? ??? MP? ??? ? ???, ?? ? ?? ??? ? ?? ? ? ?????. ?? ?? ???? ??? ? ????. ?? ???? ??? ?? ????? ??? ?? ??? ??? ??? ? ?? ??? ?????.
????? ?? ?? ??
http請求方式:?POST https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN
POST ??? ??
POST ??? ??? ??? ????.
??????{ ??????????"industry_id1":"1", ??????????"industry_id2":"4" ???????}
? ??? ??? ?? ????? ??? ITemplateMessageApi? ??? ?? ?? ???? ?? ??? ?????? ???? ??? ? ????. ??? ??? ????.
????????///?<summary> ????????///?設(shè)置所屬行業(yè)????????///?</summary> ????????///?<param name="accessToken"></param> ????????///?<param name="industry_id1">公眾號模板消息所屬行業(yè)編號(主營行業(yè))</param> ????????///?<param name="industry_id2">公眾號模板消息所屬行業(yè)編號(副營行業(yè))</param> ????????///?<returns></returns> ????????CommonResult?SetIndustry(string?accessToken,?IndustryCode?industry_id1,?IndustryCode?industry_id2);
??? IndustryCode? ??? ?? ????? ???? ?? ?? ??? ???? ?? ??? ?????.
而實現(xiàn)代碼和之前的函數(shù)處理類似,都是POST數(shù)據(jù)到一個連接即可,并解析返回的結(jié)果就可以了,具體實現(xiàn)代碼如下所示。
///?<summary> ????????///?設(shè)置所屬行業(yè) ????????///?</summary> ????????///?<param name="accessToken">訪問憑證</param> ????????///?<param name="industry_id1">公眾號模板消息所屬行業(yè)編號(主營行業(yè))</param> ????????///?<param name="industry_id2">公眾號模板消息所屬行業(yè)編號(副營行業(yè))</param> ????????///?<returns></returns> ????????public?CommonResult?SetIndustry(string?accessToken,?IndustryCode?industry_id1,?IndustryCode?industry_id2) ????????{ ????????????var?url?=?string.Format("https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token={0}",?accessToken); ????????????var?data?=?new ????????????{ ????????????????industry_id1?=?(int)industry_id1, ????????????????industry_id2?=?(int)industry_id2 ????????????}; ????????????string?postData?=?data.ToJson(); ????????????return?Helper.GetExecuteResult(url,?postData); ????????}
2.2?獲得模板ID
獲得模板ID,也就是從模板庫里面添加對應(yīng)的模板消息到我的模板里面。
從行業(yè)模板庫選擇模板到賬號后臺,獲得模板ID的過程可在MP中完成。為方便第三方開發(fā)者,提供通過接口調(diào)用的方式來修改賬號所屬行業(yè),具體如下:
接口調(diào)用請求說明
http請求方式:?POST https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN
POST數(shù)據(jù)說明
POST數(shù)據(jù)示例如下:
??????{ ???????????"template_id_short":"TM00015" ???????}
C#函數(shù)實現(xiàn)代碼如下所示:
///?<summary> ????????///?獲得模板ID. ????????///?從行業(yè)模板庫選擇模板到賬號后臺,獲得模板ID的過程可在MP中完成。 ????????///?</summary> ????????///?<param name="accessToken">訪問憑證</param> ????????///?<param name="template_id_short">模板庫中模板的編號,有“TM**”和“OPENTMTM**”等形式</param> ????????///?<returns></returns> ????????public?AddTemplateResult?AddTemplate(string?accessToken,?string?template_id_short) ????????{ ????????????var?url?=?string.Format("https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token={0}",?accessToken); ????????????var?data?=?new ????????????{ ????????????????template_id_short?=?template_id_short ????????????}; ????????????string?postData?=?data.ToJson(); ????????????return?JsonHelper<AddTemplateResult>.ConvertJson(url,?postData); ????????}
2.3?發(fā)送模板消息
根據(jù)上面小節(jié)處理,添加到我的模板里面的操作得到的模板ID,我們就可以調(diào)用發(fā)送模板消息的API進行模板消息發(fā)送了。
接口調(diào)用請求說明
http請求方式:?POST https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
POST數(shù)據(jù)說明
POST數(shù)據(jù)示例如下:
??????{ ???????????"touser":"OPENID", ???????????"template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY", ???????????"url":"http://weixin.qq.com/download", ???????????"topcolor":"#FF0000", ???????????"data":{ ???????????????????"first":?{ ???????????????????????"value":"恭喜你購買成功!", ???????????????????????"color":"#173177" ???????????????????}, ???????????????????"keynote1":{ ???????????????????????"value":"巧克力", ???????????????????????"color":"#173177" ???????????????????}, ???????????????????"keynote2":?{ ???????????????????????"value":"39.8元", ???????????????????????"color":"#173177" ???????????????????}, ???????????????????"keynote3":?{ ???????????????????????"value":"2014年9月16日", ???????????????????????"color":"#173177" ???????????????????}, ???????????????????"remark":{ ???????????????????????"value":"歡迎再次購買!", ???????????????????????"color":"#173177" ???????????????????} ???????????} ???????}
根據(jù)上面的JSON參數(shù),我們可以看到,有部分是模板消息公共的部分,有部分則是模板消息的具體參數(shù),這些參數(shù)需要根據(jù)不同的模板進行不同的賦值。
如這部分是共同的:
???touser":"OPENID", ???"template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY", ???"url":"http://weixin.qq.com/download", ???"topcolor":"#FF0000",
根據(jù)這個特點,我們定義發(fā)送模板消息的接口如下所示:
///?<summary> ????????///?模板消息僅用于公眾號向用戶發(fā)送重要的服務(wù)通知,只能用于符合其要求的服務(wù)場景中,如信用卡刷卡通知,商品購買成功通知等。 ????????///?不支持廣告等營銷類消息以及其它所有可能對用戶造成騷擾的消息。 ????????///?</summary> ????????///?<param name="accessToken">訪問憑證</param> ????????///?<param name="openId">賬號的openID</param> ????????///?<param name="templateId">在公眾平臺線上模板庫中選用模板獲得ID</param> ????????///?<param name="data">模板的變化參數(shù)數(shù)據(jù)</param> ????????///?<param name="url">,URL置空,則在發(fā)送后,點擊模板消息會進入一個空白頁面(ios),或無法點擊(android)。</param> ????????///?<param name="topcolor">頂部顏色,默認為#173177</param> ????????///?<returns></returns> ????????SendMassMessageResult?SendTemplateMessage(string?accessToken,?string?openId,?string?templateId,?object?data,?string?url,?string?topcolor?=?"#173177");
我們用object data來定義模板的變化參數(shù)數(shù)據(jù)。
具體的實現(xiàn)還是和前面的方法提交數(shù)據(jù)處理差不多,代碼如下所示。
///?<summary> ????????///?模板消息僅用于公眾號向用戶發(fā)送重要的服務(wù)通知,只能用于符合其要求的服務(wù)場景中,如信用卡刷卡通知,商品購買成功通知等。 ????????///?不支持廣告等營銷類消息以及其它所有可能對用戶造成騷擾的消息。 ????????///?</summary> ????????///?<typeparam name="T"></typeparam> ????????///?<param name="accessToken">訪問憑證</param> ????????///?<param name="openId"></param> ????????///?<param name="templateId">在公眾平臺線上模板庫中選用模板獲得ID</param> ????????///?<param name="data"></param> ????????///?<param name="url">,URL置空,則在發(fā)送后,點擊模板消息會進入一個空白頁面(ios),或無法點擊(android)。</param> ????????///?<param name="topcolor"></param> ????????///?<returns></returns> ????????public?SendMassMessageResult?SendTemplateMessage(string?accessToken,?string?openId,?string?templateId,?object?data,?string?url,?string?topcolor?=?"#173177") ????????{ ????????????var?postUrl?=?string.Format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}",?accessToken); ????????????var?msgData?=?new?TemplateData() ????????????{ ????????????????touser?=?openId, ????????????????template_id?=?templateId, ????????????????topcolor?=?topcolor, ????????????????url?=?url, ????????????????data?=?data ????????????}; ????????????string?postData?=?msgData.ToJson(); ????????????SendMassMessageResult?result?=?JsonHelper<SendMassMessageResult>.ConvertJson(postUrl,?postData); ????????????return?result; ????????}
發(fā)送模板的消息相對其他兩個接口的使用復(fù)雜一些,例如我以一個會員通知的模板消息為例,模板的詳細情況如下:
具體的測試代碼如下所示。
#region?發(fā)送模板消息 ????????????var?data?=?new ????????????{ ????????????????//使用TemplateDataItem簡單創(chuàng)建數(shù)據(jù)。 ????????????????first?=?new?TemplateDataItem("您好,您已成為微信【廣州愛奇迪】會員。"), ????????????????type?=?new?TemplateDataItem("18620292076"), ????????????????address?=?new?TemplateDataItem("廣州市白云區(qū)廣州大道北"), ????????????????VIPName?=?new ????????????????{ ????????????????????//使用new?方式,構(gòu)建數(shù)據(jù),包括value,?color兩個固定屬性。 ????????????????????value?=?"伍華聰", ????????????????????color?=?"#173177" ????????????????}, ????????????????VIPPhone?=?new?TemplateDataItem("18620292076"), ????????????????expDate?=?new?TemplateDataItem("2016年4月18日"), ????????????????remark?=?new?TemplateDataItem("如有疑問,請咨詢18620292076。",?"#173177"), ????????????}; ????????????#endregion ????????????string?url?=?"http://www.iqidi.com"; ????????????string?topColor?=?"#173177"; ????????????string?templateId?=?"-5LbClAa9KUlEmr5bCSS0rxU_I2iT16iYBDxCVU1iJg"; ????????????SendMassMessageResult?sendResult?=?api.SendTemplateMessage(token,?openId,?templateId,?data,?url,?topColor); ????????????if(sendResult?!=?null) ????????????{ ????????????????Console.WriteLine(sendResult.msg_id); ????????????}
那么我們得到的提示效果如下所示。
微信模板消息,能夠讓我們與客戶之間溝通不受每月幾條數(shù)量的限制,同時也能夠利用微信模板庫豐富的內(nèi)容,實現(xiàn)強大的應(yīng)用場景。
更多C#開發(fā)微信門戶及應(yīng)用公眾號模板消息管理?相關(guān)文章請關(guān)注PHP中文網(wǎng)!?

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)