C# 微信消息模板 发送

0
10

项目要用到微信提醒 ,加上调转到小程序页面,或者 指定url

用到RestSharp、Senparc.Weixin 类库

一开始直接照着微信示例直接post进去 发现一直提示 47001 ,估计是我姿势水平不太够,还是用个类库操作吧

using RestSharp;
using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;
using System;

namespace TemplateApp1
{
    class Program
    {
        public static string OpenId = "";
        public static string Template_id = "";
        public static string AccessToken = GetACCESS_TOKEN();
        static void Main(string[] args)
        {
            //网页跳转
            SendTemplateMessageResult T = SendTemplateURL(AccessToken, OpenId, Template_id);
            //小程序跳转
            SendTemplateMessageResult T1 = SendTemplatMiniProgram(AccessToken, OpenId, Template_id);
            Console.WriteLine(T+"\n"+T1);
            Console.ReadKey();
        }
        /// <summary>
        /// 网页跳转
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="template_id"></param>
        /// <returns></returns>
        public static SendTemplateMessageResult SendTemplateURL(string accessToken, string openId,string template_id)
        {
            var data = new
            {
                first = new TemplateDataItem("网页跳转"),
                keyword1 = new TemplateDataItem("keyword1"),
                keyword2 = new TemplateDataItem(DateTime.Now.ToString("yyyy年MM月dd日 HH:mm")),
                remark = new TemplateDataItem("remark"),
            };
            string url = "https://baidu.com";
            return Senparc.Weixin.MP.AdvancedAPIs.TemplateApi.SendTemplateMessage(accessToken, openId, template_id, url, data,null);
        }
        /// <summary>
        /// 小程序跳转
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="template_id"></param>
        /// <returns></returns>
        public static SendTemplateMessageResult SendTemplatMiniProgram(string accessToken,string openId, string template_id)
        {
            var data = new
            {
                first = new TemplateDataItem("小程序跳转"),
                keyword1 = new TemplateDataItem("keyword1"),
                keyword2 = new TemplateDataItem(DateTime.Now.ToString("yyyy年MM月dd日 HH:mm")),
                remark = new TemplateDataItem("remark"),
            };
            // 小程序
            TempleteModel_MiniProgram miniProgram = new TempleteModel_MiniProgram
            {
                appid = "",
                pagepath = ""
            };
            string url = string.Empty;
            return Senparc.Weixin.MP.AdvancedAPIs.TemplateApi.SendTemplateMessage(accessToken, openId, template_id, url, data, miniProgram);
        }
        /// <summary>
        /// 获取 AccessToken 需保存 有次数限制
        /// </summary>
        /// <returns></returns>
        public static string GetACCESS_TOKEN()
        {
            string url = "";
            var request = new RestRequest("", Method.GET);
            RestClient restClient = new RestClient(url);
            return restClient.Execute(request).Content;
        }
    }
}

  

<

发布回复

请输入评论!
请输入你的名字