1. 程式人生 > >使用微信公眾號模板訊息傳送(基於.NET開發)

使用微信公眾號模板訊息傳送(基於.NET開發)

使用微信公眾號模板訊息傳送,能夠在使用者操作網站時對指定使用者傳送訊息提示,不僅能夠及時反饋,還能使用者一個好的體驗,還可以節約短息推送的成本;
下面是比較重要的部分我做了一個擷取展示,詳細介面介紹請移步到, 微信官網地址:https://mp.weixin.qq.com/wiki

獲得模板ID,從行業模板庫選擇模板到帳號後臺,獲得模板ID的過程可在微信公眾平臺後臺完成。為方便第三方開發者,提供通過介面呼叫的方式來

獲取模板ID,具體如下:

POST資料示例如下:
      {
           "template_id_short":"TM00015"
}

引數說明
引數是否必須說明access_token是介面呼叫憑證template_id_short是模板庫中模板的編號,有“TM**”和“OPENTMTM**”等形式
返回碼說明 在呼叫模板訊息介面後,會返回JSON資料包。正常時的返回JSON資料包示例:

       {
           "errcode":0,
           "errmsg":"ok",
           "template_id":"Doclyl5uP7Aciu-qZ7mJNPtWkbkYnWBWVja26EGbNyk"
       }
POST資料示例如下:

     {
           "touser"
:"OPENID", "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY", "url":"http://weixin.qq.com/download", "miniprogram":{ "appid":"xiaochengxuappid12345", "pagepath":"index?foo=bar" }, "data":{ "first"
: { "value":"恭喜你購買成功!", "color":"#173177" }, "keynote1":{ "value":"巧克力", "color":"#173177" }, "keynote2": { "value":"39.8元", "color":"#173177" }, "keynote3": { "value":"2014年9月22日", "color":"#173177" }, "remark":{ "value":"歡迎再次購買!", "color":"#173177" } } }

引數說明

引數是否必填說明touser是接收者openidtemplate_id是模板IDurl否模板跳轉連結miniprogram否跳小程式所需資料,不需跳小程式可不用傳該資料appid是所需跳轉到的小程式appid(該小程式appid必須與發模板訊息的公眾號是繫結關聯關係)pagepath是所需跳轉到小程式的具體頁面路徑,支援帶引數,(示例index?foo=bar)data是模板資料
注:url和miniprogram都是非必填欄位,若都不傳則模板無跳轉;若都傳,會優先跳轉至小程式。開發者可根據實際需要選擇其中一種跳轉方式即可。當用戶的微信客戶端版本不支援跳小程式時,將會跳轉至url。

返回碼說明 在呼叫模板訊息介面後,會返回JSON資料包。正常時的返回JSON資料包示例:

      {
           "errcode":0,
           "errmsg":"ok",
           "msgid":200228332
       }

下面是我利用.NET實現的關鍵程式碼(注:這只是實現會員註冊成功後的資訊推送模板,其他模板相同處理)
呼叫方式,如下:

 string strJSON = WeixinTemplateHelper.GetJoinMember(weiXinAccount.User.Userid, weiXinAccount.UserName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), SiteConfigManager.GetConfigStringByKey("weixinH5Domain"));//這裡是獲取資料庫中儲存的模板資訊組裝成介面需要傳送的格式
                    if (strJSON != "")
                    {                       
                        WeixinTemplateHelper.SendMessage(strJSON);//呼叫微信介面傳送
                    }

組裝為微信標準格式的資訊

///組裝為微信標準格式的資訊
 public static string GetJoinMember(long UserID, string MemberNO, string JoinTime, string Url = "")
        {
            string UserAccount = "";
            if (IsWeiXinUser(UserID, ref UserAccount))//獲取使用者在微信中OpeId資訊
            {
                WeiXinMsgConfig model = new WeiXinMsgConfigBLL().GetModelByTemplateNO(WeixinTemplateType.ChengWeiHuiYuan);//這裡我是將自定義模板資訊儲存到資料的,後臺做了一個管理,這個方法是獲取我配置的推送訊息模板
                if (model.IsSend == 0)
                {
                    return "";
                }
                var obj = new
                {
                    touser = UserAccount,
                    template_id = model.TemplateID,
                    url = Url,
                    topcolor = "#FF0000",
                    data = new
                    {
                        first = new { value = model.TemplateFirst, color = "#173177" },
                        keyword1 = new { value = MemberNO, color = "#173177" },
                        keyword2 = new { value = JoinTime, color = "#173177" },
                        remark = new { value = model.TemplateRemark, color = "#173177" }
                    }
                };               
                return obj.GetJsonByObj();
            }
            else
            {
                return "";
            }
        }

傳送組裝的資訊給指定的使用者

  //傳送組裝的資訊給指定的使用者
   public static string SendMessage(string JsonData)
        {
            try
            {
                HttpClientHelper httpclient = new HttpClientHelper();
                string strUrl = string.Format(CommonUtilities.H5Constants.WeixinApi.WeiXinSendMessage, GetAccess_Token(appid, appsecret));//根據微信的介面請求組裝相應的路徑
                string strResult = httpclient.SendPostRequest(strUrl, JsonData);//通過Post請求訪問介面
                return WebHelper.ParseFormJson<SendMessageResult>(strResult).errmsg;//獲取返回資訊
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

獲取微信的Access_Token

//獲取微信的Access_Token
 public static string GetAccess_Token(string appID, string appSecret)
        {
            //if (appID == appid && appSecret == appsecret && access_token != "" && (DateTime.Now - access_token_time).Seconds < 7000)
            //{
            //}
            //else
            //{

            DataRow dr = new DeveloperInfoDAL().GetDeveloperInfo(CommonUtilities.DataBase.DataFactory.CreateDataBase());//這是獲取儲存在資料中微信公眾號的appID和appSecret 
            appid = dr != null ? dr["AppId"].ToString() : "";
            appsecret = dr != null ? dr["AppSecret"].ToString() : "";

            HttpClientHelper httpclient = new HttpClientHelper();
            httpclient.Url = string.Format(CommonUtilities.H5Constants.WeixinApi.WeixinMenuGetAccessToken, appID, appSecret);//組裝獲取Access_Token的介面訪問路徑
            string strResult = httpclient.GetString();//採用GET請求訪問介面
            access_token = WebHelper.JSONToObject<TokenResult>(strResult).access_token;
            access_token_time = DateTime.Now;
            //}
            return access_token;
        }

需要的基礎方法的封裝實現

      /// <summary>
        /// 發起一個POST型別的Web請求,並返回響應
        /// </summary>
        /// <param name="requestUri">請求地址</param>
        /// <param name="jsonData">json資料包</param>
        /// <returns>響應</returns>
        public string SendPostRequest(string requestUri, string jsonData)
        {
            if (requestUri.IfNull().ToLower().Trim().StartsWith("https"))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            }
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);
            byte[] data = Encoding.UTF8.GetBytes(jsonData);
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.ContentLength = data.Length;
            httpWebRequest.KeepAlive = true;

            using (var requestStream = httpWebRequest.GetRequestStream())
            {
                requestStream.Write(data, 0, data.Length);
            }

            HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            string content = reader.ReadToEnd();
            reader.Close();
            responseStream.Close();
            response.Close();
            return content;
        }
 /// <summary>
        /// 發出一次新的請求,並以位元組陣列形式返回迴應的內容
        /// 呼叫此方法會觸發StatusUpdate事件
        /// </summary>
        /// <returns>包含迴應主體內容的位元組陣列</returns>
        public byte[] GetBytes()
        {
            HttpWebResponse res = GetResponse();
            int length = (int)res.ContentLength;

            MemoryStream memoryStream = new MemoryStream();
            byte[] buffer = new byte[0x100];
            Stream rs = res.GetResponseStream();
            for (int i = rs.Read(buffer, 0, buffer.Length); i > 0; i = rs.Read(buffer, 0, buffer.Length))
            {
                memoryStream.Write(buffer, 0, i);
                OnStatusUpdate(new StatusUpdateEventArgs((int)memoryStream.Length, length));
            }
            rs.Close();

            return memoryStream.ToArray();
        }

        /// <summary>
        /// 發出一次新的請求,以Http頭,或Html Meta標籤,或DefaultEncoding指示的編碼資訊對迴應主體解碼
        /// 呼叫此方法會觸發StatusUpdate事件
        /// </summary>
        /// <returns>解碼後的字串</returns>
        public string GetString()
        {
            byte[] data = GetBytes();
            string encodingName = GetEncodingFromHeaders();

            if (encodingName == null)
                encodingName = GetEncodingFromBody(data);

            Encoding encoding;
            if (encodingName == null)
                encoding = defaultEncoding;
            else
            {
                try
                {
                    encoding = Encoding.GetEncoding(encodingName);
                }
                catch (ArgumentException)
                {
                    encoding = defaultEncoding;
                }
            }
            return encoding.GetString(data);
        }