使用微信公众平台接口设置推送模板消息的所属行业
模板消息仅用于公众号向用户发送重要的服务通知,只能用于符合其要求的服务场景中,如信用卡刷卡通知,商品购买成功通知等。不支持广告等营销类消息以及其它所有可能对用户造成骚扰的消息。
官方文档地址
https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html
设置微信消息模板所属行业
注意!每月可修改行业1次 根据文档我们先定义一个TemplateSetIndustryResponse类继承 ErrorMessage 用于解析接口结果
ErrorMessage 公共类参考 : 微信公众平台C#开发系列(四):获取access token凭证
namespace YuanTK.WeiXin.Wrapper { ////// 设置微信消息模板所属行业结果 /// public class TemplateSetIndustryResponse: ErrorMessage { } }
定义一个TemplateSetIndustry方法类继承WeiXinRequest 和接口IWeiXin
namespace YuanTK.WeiXin { ////// 设置行业模板参数 /// public class TemplateSetIndustryRequest { /// /// 公众号模板消息所属行业编号代码 /// public string IndustryNo { get; set; } /// /// 公众号模板消息所属行业编号 /// public string IndustryId { get; set; } } /// /// 设置所属行业信息 /// public class TemplateSetIndustry : WeiXinRequest , IWeiXin { String api; Dictionary<string,string> _RequestData; public TemplateSetIndustry(string access_token, List industrys) { _RequestData = new Dictionary<string, string>(); foreach (TemplateSetIndustryRequest industry in industrys) { _RequestData.Add(industry.IndustryId, industry.IndustryNo); } this.api = $"https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token={access_token}"; } /// /// 调用接口获取结果 /// /// public TemplateSetIndustryResponse GetResponse() { var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; string poststr = Newtonsoft.Json.JsonConvert.SerializeObject(this._RequestData, Formatting.Indented, jSetting); return this.Post(api, poststr); } } }
调用代码例子
TemplateSetIndustryRequest request1 = new TemplateSetIndustryRequest() {
IndustryId = "industry_id1",
IndustryNo = "1"
};
TemplateSetIndustryRequest request2 = new TemplateSetIndustryRequest()
{
IndustryId = "industry_id2",
IndustryNo = "4"
};
List requests = new List() {
request1,
request2
};
string access_token = token.access_token;
IWeiXin api = new TemplateSetIndustry(access_token, requests);
TemplateSetIndustryResponse response = api.GetResponse();
调用成功返回的结果