Example #1
0
        /// <summary>
        /// 预览消息
        /// <para>toWxName和toOpenId同时赋值时,以toWxName优先</para>
        /// </summary>
        /// <param name="message">消息实体</param>
        /// <param name="toOpenId">接收消息用户对应该公众号的openid,也可以使用toWxName,以实现对微信号的预览</param>
        /// <param name="toWxName">接收消息用户的微信号</param>
        /// <returns></returns>
        public static PushMessageReturnValue PreviewMessage(PushMessage.Base message, string toOpenId, string toWxName)
        {
            string json = PushMessageBuilder.BuildPreviewJson(message, toOpenId, toWxName);
            string url  = ServiceUrl.PreviewMessage + "?access_token=" + AccessToken.Value;

            return(HttpHelper.Post <PushMessageReturnValue>(url, json));
        }
Example #2
0
        /// <summary>
        /// 根据OpenID列表群发【订阅号不可用,服务号认证后可用】
        /// </summary>
        /// <param name="message">消息实体</param>
        /// <param name="openIdList">填写图文消息的接收者,一串OpenID列表,OpenID最少2个,最多10000个</param>
        /// <returns></returns>
        public static PushMessageReturnValue PushMessageByOpenID(PushMessage.Base message, List <string> openIdList)
        {
            string json = PushMessageBuilder.BuildPushJsonByOpenID(message, openIdList);
            string url  = ServiceUrl.PushMessageByOpenID + "?access_token=" + AccessToken.Value;

            return(HttpHelper.Post <PushMessageReturnValue>(url, json));
        }
Example #3
0
        //private static Client Create()
        //{
        //    Client client = new Client();
        //    client.WebClient.Encoding = Encoding.UTF8;
        //    client.WebClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
        //    return client;
        //}
        #endregion

        /// <summary>
        /// 根据分组进行群发【订阅号与服务号认证后均可用】
        /// </summary>
        /// <param name="message">消息实体</param>
        /// <param name="groupId">群发到的分组的group_id,参加用户管理中用户分组接口,若is_to_all值为true,可不填写group_id</param>
        /// <param name="isToAll">用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,选择false可根据group_id发送给指定群组的用户</param>
        /// <returns></returns>
        public static PushMessageReturnValue PushMessageByGroupID(PushMessage.Base message, string groupId, bool isToAll)
        {
            string json = PushMessageBuilder.BuildPushJsonByGroupID(message, groupId, isToAll);
            string url  = ServiceUrl.PushMessageByGroupID + "?access_token=" + AccessToken.Value;

            return(HttpHelper.Post <PushMessageReturnValue>(url, json));
        }
        /// <summary>
        /// 构建根据OpenID群发消息的Json内容
        /// </summary>
        /// <param name="message"></param>
        /// <param name="openIdList"></param>
        /// <returns></returns>
        public static string BuildPushJsonByOpenID(PushMessage.Base message, List <string> openIdList)
        {
            Dictionary <string, object> dic = new Dictionary <string, object>();

            dic.Add("touser", openIdList);
            dic.Add(message.MsgType, message.GetData());
            dic.Add("msgtype", message.MsgType);

            return(JsonConvert.SerializeObject(dic));
        }
        /// <summary>
        /// 构建根据群组ID群发消息的Json内容
        /// </summary>
        /// <param name="message"></param>
        /// <param name="groupId"></param>
        /// <param name="isToAll"></param>
        /// <returns></returns>
        public static string BuildPushJsonByGroupID(PushMessage.Base message, string groupId, bool isToAll)
        {
            Dictionary <string, object> dic = new Dictionary <string, object>();

            dic.Add("filter", new { is_to_all = isToAll, group_id = groupId });
            dic.Add(message.MsgType, message.GetData());
            dic.Add("msgtype", message.MsgType);

            return(JsonConvert.SerializeObject(dic));
        }
        /// <summary>
        /// 构建预览消息的Json内容
        /// </summary>
        /// <param name="message"></param>
        /// <param name="toOpenId">接收消息用户对应该公众号的openid,也可以使用toWxName,以实现对微信号的预览</param>
        /// <param name="toWxName">接收消息用户的微信号</param>
        /// <returns></returns>
        public static string BuildPreviewJson(PushMessage.Base message, string toOpenId, string toWxName)
        {
            Dictionary <string, object> dic = new Dictionary <string, object>();

            if (!string.IsNullOrWhiteSpace(toWxName))
            {
                dic.Add("towxname", toWxName);
            }
            string msgType = message.MsgType == "video" ? "mpvideo" : message.MsgType;

            dic.Add("touser", toOpenId);
            dic.Add(msgType, message.GetData());
            dic.Add("msgtype", msgType);

            return(JsonConvert.SerializeObject(dic));
        }