c# 微信開發 《主動發送內容》

須要用戶和公衆號,在48小時內有過互動,否則發不出去。css

 

  public const string customUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}"; /// <summary>
        /// 主動推送微信消息
        /// </summary>
        public static void PushWeiXinMessage(string PostData)
        {
            try {
                string Accsss_token = WeiXinCommom.GetAccsss_token();
                string PostUrl = string.Format(WeiXinCommom.customUrl, Accsss_token);
                WeiXinCommom.PostData(PostUrl, PostData);
            }catch(Exception ex){
                LogHelper.WriteFileLog("PushWeiXinMessageError", string.Format("{0}", ex.Message));
                return;
            }
        }

        /// <summary>
        /// 返回要推送的文本信息
        /// </summary>
        /// <param name="WeixinOpendID"></param>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string ReturnTextMessage(string WeixinOpendID, string Content)
        {
            string responeJsonStr = "{";
            responeJsonStr += "\"touser\": \"" + WeixinOpendID + "\",";
            responeJsonStr += "\"msgtype\": \"text\",";
            responeJsonStr += "\"text\": {";
            responeJsonStr += "  \"content\": \"" + Content + "\"";
            responeJsonStr += "},";
            responeJsonStr += "\"safe\":\"0\"";
            responeJsonStr += "}";
            return responeJsonStr;
        }

        /// <summary>
        /// 返回要推送的圖片信息
        /// </summary>
        /// <param name="WeixinOpendID"></param>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string ReturnImgMessage(string WeixinOpendID, string MEDIA_ID)
        {
            string responeJsonStr = "{";
            responeJsonStr += "\"touser\": \"" + WeixinOpendID + "\",";
            responeJsonStr += "\"msgtype\": \"image\",";
            responeJsonStr += "\"image\": {";
            responeJsonStr += "  \"media_id\": \"" + MEDIA_ID + "\"";
            responeJsonStr += "},";
            responeJsonStr += "\"safe\":\"0\"";
            responeJsonStr += "}";
            return responeJsonStr;
        }
相關文章
相關標籤/搜索