NETCORE下用SKIT类库发送微信模板消息

NETCORE下用SKIT类库发送微信模板消息

 

        //测试发送模板消息-微信公众号
        //https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#5
        public async Task<IActionResult> Ceshi(int id, string openid) {
            try
            {
                var options = new WechatApiClientOptions()
                {
                    AppId = "********68",
                    AppSecret = "6b7b********9c5b93230c5d6b223d",
                };
                var client = new WechatApiClient(options);
                var aaa = await client.ExecuteCgibinTokenAsync(new SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinTokenRequest());


                if (aaa.ErrorCode != 0)
                {
                    throw new Exception("取accesstoken出错:" + aaa.ErrorMessage);
                }

                Dictionary<string, SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinMessageTemplateSendRequest.Types.DataItem> dic = new Dictionary<string, SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinMessageTemplateSendRequest.Types.DataItem>();
                dic.Add("first", new SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinMessageTemplateSendRequest.Types.DataItem() { 
                 Value = "您已报名成功"
                });
                dic.Add("keyword1", new SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinMessageTemplateSendRequest.Types.DataItem()
                {
                    Value = "2024年08月12日"
                });
                dic.Add("keyword2", new SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinMessageTemplateSendRequest.Types.DataItem()
                {
                    Value = "测试项目名称"
                });
                dic.Add("keyword3", new SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinMessageTemplateSendRequest.Types.DataItem()
                {
                    Value = "员工牛腩"
                });
                dic.Add("keyword4", new SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinMessageTemplateSendRequest.Types.DataItem()
                {
                    Value = "南宁大塘天城店"
                });
                dic.Add("keyword5", new SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinMessageTemplateSendRequest.Types.DataItem()
                {
                    Value = "其他信息呀呀呀"
                });
                dic.Add("remark", new SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinMessageTemplateSendRequest.Types.DataItem()
                {
                    Value = "有问题打电话:13888888888"
                });
              var bbb = await  client.ExecuteCgibinMessageTemplateSendAsync(new SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinMessageTemplateSendRequest()
                {
                    AccessToken = aaa.AccessToken,
                    TemplateId = "He**********VNU",
                    ToUserOpenId = openid,
                    Data = dic,
                });
                if (bbb.ErrorCode != 0)
                {
                    throw new Exception(  bbb.ErrorMessage);
                }

                return Json(new { code = 0, msg = "成功发送模板消息:" + bbb.ErrorMessage});
            }
            catch (Exception ex)
            {
                return Json(new { code=-1,msg="测试发送模板消息出错:"+ ex.Message});
            }
        }

 

posted @ 2024-08-28 19:52  牛腩  阅读(23)  评论(0编辑  收藏  举报