微信公众平台C#开发系列(十二): 模板消息-设置微信消息的所属行业

微信公众平台
52
微信公众平台
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
{
    /// 
    /// 设置微信消息模板所属行业结果
    /// 
    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();

微信公众号凭证access_token如何获取?

调用成功返回的结果

设置微信消息模板所属行业

微信公众平台C#开发系列(九):删除自定义菜单
微信公众平台C#开发系列(十三):模板消息-获得模板ID
微信公众平台C#开发系列(八):查询自定义菜单
微信公众平台C#开发系列(四):获取access token凭证
微信公众平台C#开发系列(五):获取微信服务器IP地址
微信公众平台C#开发系列(七):创建自定义菜单
微信公众平台C#开发系列(十四): 模板消息-获取模板列表
微信公众平台C#开发系列(二):微信公众号开发的准备工作
微信公众平台C#开发系列(十六)模板消息-发送消息模板
微信公众平台C#开发系列(十五): 模板消息-删除消息模板
微信公众平台C#开发系列(三):公众平台接入
微信公众平台C#开发系列(一):为什么要微信公众号开发
暂无相关内容...
暂无相关内容...
免责声明 部分转载分享内容若侵犯您的权益,还请 邮件联系 侵删