Beispiel #1
0
        /// <summary>
        /// 根据OpenId列表群发消息
        /// </summary>
        /// <param name="userName">公众号</param>
        /// <param name="tousers">OpenId列表</param>
        /// <param name="messageType">群发消息类型</param>
        /// <param name="mediaIdOrContent">多媒体id或者文本内容</param>
        /// <param name="errorMessage">返回发送是否成功</param>
        /// <returns>如果发送成功,返回消息ID;否则,返回-1。</returns>
        public static long Send(string userName, IEnumerable <string> tousers, MassMessageTypeEnum messageType, string mediaIdOrContent, out ErrorMessage errorMessage)
        {
            long messageId = -1;

            if (tousers == null)
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, string.Format("接收者不正确,数目必须大于0,小于等于{0}。", maxUserCount));
                return(messageId);
            }
            List <string> touserList = new List <string>(tousers);

            if (touserList.Count == 0 || touserList.Count > maxUserCount)
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, string.Format("接收者不正确,数目必须大于0,小于等于{0}。", maxUserCount));
                return(messageId);
            }
            if (string.IsNullOrWhiteSpace(mediaIdOrContent))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode,
                                                string.Format("缺少{0}。", messageType == MassMessageTypeEnum.text ? "文本内容" : "媒体ID"));
                return(messageId);
            }
            string json = GetMassMessageJsonString(touserList.ToArray(), messageType, mediaIdOrContent);

            return(Send(userName, urlForSending, json, out errorMessage));
        }
Beispiel #2
0
        /// <summary>
        /// 预览群发消息
        /// </summary>
        /// <param name="userName">公众号</param>
        /// <param name="touser">OpenId</param>
        /// <param name="messageType">群发消息类型</param>
        /// <param name="mediaIdOrContent">多媒体id或者文本内容</param>
        /// <param name="errorMessage">返回发送是否成功</param>
        /// <returns>如果发送成功,返回消息ID;否则,返回-1。</returns>
        public static long Preview(string userName, string touser, MassMessageTypeEnum messageType, string mediaIdOrContent, out ErrorMessage errorMessage)
        {
            long messageId = -1;

            if (string.IsNullOrWhiteSpace(mediaIdOrContent))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode,
                                                string.Format("缺少{0}。", messageType == MassMessageTypeEnum.text ? "文本内容" : "媒体ID"));
                return(messageId);
            }
            string json = GetMassMessageJsonString(touser, messageType, mediaIdOrContent);

            return(Send(userName, urlForPreviewing, json, out errorMessage));
        }
Beispiel #3
0
        /// <summary>
        /// 获取群发消息的json字符串。
        /// </summary>
        /// <param name="touser">OpenId</param>
        /// <param name="messageType">群发消息类型</param>
        /// <param name="mediaIdOrContent">多媒体id或者文本内容</param>
        /// <returns>返回群发消息的json字符串。</returns>
        private static string GetMassMessageJsonString(string touser, MassMessageTypeEnum messageType, string mediaIdOrContent)
        {
            dynamic msg = new ExpandoObject();

            msg.touser = touser;
            if (messageType == MassMessageTypeEnum.text)
            {
                ((IDictionary <string, object>)msg).Add(messageType.ToString(), new { content = mediaIdOrContent });
            }
            else
            {
                ((IDictionary <string, object>)msg).Add(messageType.ToString(), new { media_id = mediaIdOrContent });
            }
            msg.msgtype = messageType.ToString();
            return(JsonConvert.SerializeObject(msg));
        }
Beispiel #4
0
        /// <summary>
        /// 根据分组群发消息
        /// </summary>
        /// <param name="userName">公众号</param>
        /// <param name="isToAll">是否群发给所有用户</param>
        /// <param name="groupId">如果群发给所有用户,忽略该参数;否则群发给该组中的用户</param>
        /// <param name="messageType">群发消息类型</param>
        /// <param name="mediaIdOrContent">多媒体id或者文本内容</param>
        /// <param name="errorMessage">返回发送是否成功</param>
        /// <returns>如果发送成功,返回消息ID;否则,返回-1。</returns>
        public static long Send(string userName, bool isToAll, string groupId, MassMessageTypeEnum messageType, string mediaIdOrContent, out ErrorMessage errorMessage)
        {
            long messageId = -1;

            if (!isToAll && string.IsNullOrWhiteSpace(groupId))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "缺少分组ID。");
                return(messageId);
            }
            if (string.IsNullOrWhiteSpace(mediaIdOrContent))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode,
                                                string.Format("缺少{0}。", messageType == MassMessageTypeEnum.text ? "文本内容" : "媒体ID"));
                return(messageId);
            }
            string json = GetMassMessageJsonString(isToAll, groupId, messageType, mediaIdOrContent);

            return(Send(userName, urlForSendingAll, json, out errorMessage));
        }
Beispiel #5
0
        /// <summary>
        /// 获取群发消息的json字符串。
        /// </summary>
        /// <param name="isToAll">是否群发给所有用户</param>
        /// <param name="groupId">如果群发给所有用户,忽略该参数;否则群发给该组中的用户</param>
        /// <param name="messageType">群发消息类型</param>
        /// <param name="mediaIdOrContent">多媒体id或者文本内容</param>
        /// <returns>返回群发消息的json字符串。</returns>
        private static string GetMassMessageJsonString(bool isToAll, string groupId, MassMessageTypeEnum messageType, string mediaIdOrContent)
        {
            dynamic msg = new ExpandoObject();

            msg.filter = new
            {
                is_to_all = isToAll,
                group_id  = isToAll ? string.Empty : groupId
            };
            if (messageType == MassMessageTypeEnum.text)
            {
                ((IDictionary <string, object>)msg).Add(messageType.ToString(), new { content = mediaIdOrContent });
            }
            else
            {
                ((IDictionary <string, object>)msg).Add(messageType.ToString(), new { media_id = mediaIdOrContent });
            }
            msg.msgtype = messageType.ToString();
            return(JsonConvert.SerializeObject(msg));
        }