当前位置: 移动技术网 > IT编程>开发语言>c# > C# 微信消息模板 发送

C# 微信消息模板 发送

2019年08月19日  | 移动技术网IT编程  | 我要评论

项目要用到微信提醒 ,加上调转到小程序页面,或者 指定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;
        }
    }
}

  

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网