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