コード例 #1
0
        /// <summary>
        /// 群组消息推送
        /// </summary>
        /// <param name="type">消息类型</param>
        /// <param name="value">
        /// 文字消息:文字内容;
        /// 图片、音频、视频消息:文件ID;
        /// 图文消息:消息ID;
        /// </param>
        /// <param name="groupId">分组ID</param>
        /// <param name="gender">性别(0:全部; 1:男; 2:女)</param>
        /// <param name="country">国家</param>
        /// <param name="province">省</param>
        /// <param name="city">市</param>
        public Task <bool> MassSendMessageAsync(MpMessageType type, string value, string groupId = "-1", int gender = 0,
                                                string country = null, string province = null, string city = null)
        {
            return(Task.Factory.StartNew(() =>
            {
                if (!InternalLogin())
                {
                    return false;
                }

                var postData = new StringBuilder();

                postData.AppendFormat(
                    "type={0}&groupid={1}&sex={2}&country={3}&province={4}&city={5}&token={6}&synctxweibo=0&synctxnews=0&imgcode=&lang=zh_CN&random=0.1234567890&f=json&ajax=1&t=ajax-response",
                    (int)type,
                    string.IsNullOrWhiteSpace(groupId) ? "-1" : groupId,
                    gender,
                    string.IsNullOrWhiteSpace(country) ? string.Empty : HttpUtility.UrlEncode(country, Encoding.UTF8),
                    string.IsNullOrWhiteSpace(province) ? string.Empty : HttpUtility.UrlEncode(province, Encoding.UTF8),
                    string.IsNullOrWhiteSpace(city) ? string.Empty : HttpUtility.UrlEncode(city, Encoding.UTF8),
                    _loginContext.Token);

                switch (type)
                {
                case MpMessageType.Text:
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        throw new ArgumentNullException("value", "文字消息内容为空");
                    }

                    postData.AppendFormat("&content={0}", HttpUtility.UrlEncode(value, Encoding.UTF8));
                    break;

                case MpMessageType.Image:
                case MpMessageType.Audio:
                case MpMessageType.Video:
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        throw new ArgumentNullException("value", "文件ID为空");
                    }

                    postData.AppendFormat("&fileid={0}", value);
                    break;

                case MpMessageType.AppMsg:
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        throw new ArgumentNullException("value", "图文消息ID为空");
                    }

                    postData.AppendFormat("&appmsgid={0}", value);
                    break;
                }

                var resultJson = RequestHelper.Post(MpAddresses.MassSendMessageUrl, postData.ToString(), _loginContext.LoginCookie);
                var resultPackage = JsonHelper.Deserialize <CommonExecuteResult>(resultJson);

                return resultPackage != null && resultPackage.ret == 0;
            }));
        }
コード例 #2
0
        /// <summary>
        /// 上传微信临时素材
        /// </summary>
        /// <param name="mpid"></param>
        /// <param name="path"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private async Task <string> UploadMedia(int mpid, string path, MpMessageType type)
        {
            var account = await _mpAccountAppService.Get(new EntityDto <int>() { Id = mpid });

            if (account != null)
            {
                var retString = "";
                try
                {
                    var url = string.Format("https://api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), type.ToString());
                    retString = await HttpHelper.HttpPost(url, path, timeOut : Config.TIME_OUT);
                }
                catch (Exception ex)
                {
                    Logger.Error(string.Format("上传MpID为{0},地址{1}的临时素材报错 {2}", mpid, path, type.ToString()), ex);
                    var url = string.Format("https://api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), type.ToString());
                    retString = await HttpHelper.HttpPost(url, path, timeOut : Config.TIME_OUT);
                }
                var returndata = JsonConvert.DeserializeObject <UploadTemporaryMediaResult>(retString);
                if (returndata.errcode == ReturnCode.请求成功)
                {
                    return(returndata.media_id);
                }
                else
                {
                    Logger.Error(string.Format("上传MpID为{0},地址{1}的临时素材报错:{2}", mpid, path, JsonConvert.SerializeObject(returndata)));
                }
            }
            return(string.Empty);
        }
コード例 #3
0
        /// <summary>
        /// 单用户消息发送
        /// </summary>
        /// <param name="openId">用户OpenId</param>
        /// <param name="type">消息类型</param>
        /// <param name="value">
        /// 文字消息:文字内容;
        /// 图片、音频、视频消息:文件ID;
        /// 图文消息:消息ID;
        /// </param>
        public Task <bool> SingleSendMessageAsync(string openId, MpMessageType type, string value)
        {
            return(Task.Factory.StartNew(() =>
            {
                if (!InternalLogin())
                {
                    return false;
                }

                var postData = new StringBuilder();

                postData.AppendFormat(
                    "type={0}&tofakeid={1}&&token={2}&lang=zh_CN&random=0.1234567890&f=json&ajax=1&t=ajax-response",
                    (int)type, openId, _loginContext.Token);

                switch (type)
                {
                case MpMessageType.Text:
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        throw new ArgumentNullException("value", "文字消息内容为空");
                    }

                    postData.AppendFormat("&content={0}", HttpUtility.UrlEncode(value, Encoding.UTF8));
                    break;

                case MpMessageType.Image:
                case MpMessageType.Audio:
                case MpMessageType.Video:
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        throw new ArgumentNullException("value", "文件ID为空");
                    }

                    postData.AppendFormat("&file_id={0}&fileid={0}", value);
                    break;

                case MpMessageType.AppMsg:
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        throw new ArgumentNullException("value", "图文消息ID为空");
                    }

                    postData.AppendFormat("&app_id={0}&appmsgid={0}", value);
                    break;
                }

                var resultJson = RequestHelper.Post(MpAddresses.SingleSendMessageUrl, postData.ToString(), _loginContext.LoginCookie);
                var resultPackage = JsonHelper.Deserialize <SendMessageResult>(resultJson);

                return resultPackage != null && resultPackage.base_resp != null && resultPackage.base_resp.ret == 0;
            }));
        }
コード例 #4
0
        public virtual async Task <string> UploadArtImageAndGetMediaID(int mpid, string fileUrl, MpMessageType type)
        {
            var account = await _mpAccountAppService.Get(new Abp.Application.Services.Dto.EntityDto <int> {
                Id = mpid
            });

            var access_token = await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret);

            UploadImgResult responseModel = null;
            var             fileName      = fileUrl.Substring(fileUrl.LastIndexOf("/") + 1);

            try
            {
                responseModel = JsonConvert.DeserializeObject <UploadImgResult>(await HttpHelper.HttpPost($"https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={access_token}&type={type.ToString()}", fileUrl, fileName));
            }
            catch
            {
                access_token = await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true);

                try
                {
                    responseModel = JsonConvert.DeserializeObject <UploadImgResult>(await HttpHelper.HttpPost($"https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={access_token}&type={type.ToString()}", fileUrl, fileName));
                }
                catch (Exception e)
                {
                    throw new UserFriendlyException(e.Message);
                }
            }
            return(responseModel != null ? responseModel.url : "");
        }
コード例 #5
0
        public virtual async Task <string> UploadVideoAndGetMediaID(int mpid, string fileUrl, MpMessageType type, string title, string introduction)
        {
            var account = await _mpAccountAppService.Get(new Abp.Application.Services.Dto.EntityDto <int> {
                Id = mpid
            });

            var access_token = await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret);

            UploadForeverMediaResult responseModel = null;
            var fileName = fileUrl.Substring(fileUrl.LastIndexOf("/") + 1);
            Dictionary <string, string> content = new Dictionary <string, string>()
            {
                { "description", JsonConvert.SerializeObject(new { title = title, introduction = introduction }) }
            };


            try
            {
                responseModel = JsonConvert.DeserializeObject <UploadForeverMediaResult>(await HttpHelper.HttpPostVideo($"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={access_token}&type={type.ToString()}", fileUrl, content.First(), fileName));
            }
            catch
            {
                access_token = await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true);

                try
                {
                    responseModel = JsonConvert.DeserializeObject <UploadForeverMediaResult>(await HttpHelper.HttpPostVideo($"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={access_token}&type={type.ToString()}", fileUrl, content.First(), fileName));
                }
                catch (Exception e)
                {
                    throw new UserFriendlyException(e.Message);
                }
            }
            return(responseModel != null ? responseModel.media_id : "");
        }
コード例 #6
0
ファイル: MPManager.cs プロジェクト: 010227leo/MPHelper
        /// <summary>
        /// 单用户消息发送
        /// </summary>
        /// <param name="openId">用户OpenId</param>
        /// <param name="type">消息类型</param>
        /// <param name="value">
        /// 文字消息:文字内容; 
        /// 图片、音频、视频消息:文件ID; 
        /// 图文消息:消息ID; 
        /// </param>
        public Task<bool> SingleSendMessageAsync(string openId, MpMessageType type, string value)
        {
            return Task.Factory.StartNew(() =>
            {
                if (!InternalLogin())
                    return false;

                var postData = new StringBuilder();

                postData.AppendFormat(
                    "type={0}&tofakeid={1}&&token={2}&lang=zh_CN&random=0.1234567890&f=json&ajax=1&t=ajax-response",
                    (int) type, openId, _loginContext.Token);

                switch (type)
                {
                    case MpMessageType.Text:
                        if (string.IsNullOrWhiteSpace(value))
                            throw new ArgumentNullException("value", "文字消息内容为空");

                        postData.AppendFormat("&content={0}", HttpUtility.UrlEncode(value, Encoding.UTF8));
                        break;
                    case MpMessageType.Image:
                    case MpMessageType.Audio:
                    case MpMessageType.Video:
                        if (string.IsNullOrWhiteSpace(value))
                            throw new ArgumentNullException("value", "文件ID为空");

                        postData.AppendFormat("&file_id={0}&fileid={0}", value);
                        break;
                    case MpMessageType.AppMsg:
                        if (string.IsNullOrWhiteSpace(value))
                            throw new ArgumentNullException("value", "图文消息ID为空");

                        postData.AppendFormat("&app_id={0}&appmsgid={0}", value);
                        break;
                }

                var resultJson = RequestHelper.Post(MpAddresses.SingleSendMessageUrl, postData.ToString(), _loginContext.LoginCookie);
                var resultPackage = JsonHelper.Deserialize<SendMessageResult>(resultJson);

                return resultPackage != null && resultPackage.base_resp != null && resultPackage.base_resp.ret == 0;
            });
        }
コード例 #7
0
ファイル: MPManager.cs プロジェクト: 010227leo/MPHelper
        /// <summary>
        /// 群组消息推送
        /// </summary>
        /// <param name="type">消息类型</param>
        /// <param name="value">
        /// 文字消息:文字内容; 
        /// 图片、音频、视频消息:文件ID; 
        /// 图文消息:消息ID; 
        /// </param>
        /// <param name="groupId">分组ID</param>
        /// <param name="gender">性别(0:全部; 1:男; 2:女)</param>
        /// <param name="country">国家</param>
        /// <param name="province">省</param>
        /// <param name="city">市</param>
        public Task<bool> MassSendMessageAsync(MpMessageType type, string value, string groupId = "-1", int gender = 0,
            string country = null, string province = null, string city = null)
        {
            return Task.Factory.StartNew(() =>
            {
                if (!InternalLogin())
                    return false;

                var postData = new StringBuilder();

                postData.AppendFormat(
                    "type={0}&groupid={1}&sex={2}&country={3}&province={4}&city={5}&token={6}&synctxweibo=0&synctxnews=0&imgcode=&lang=zh_CN&random=0.1234567890&f=json&ajax=1&t=ajax-response",
                    (int) type,
                    string.IsNullOrWhiteSpace(groupId) ? "-1" : groupId,
                    gender,
                    string.IsNullOrWhiteSpace(country) ? string.Empty : HttpUtility.UrlEncode(country, Encoding.UTF8),
                    string.IsNullOrWhiteSpace(province) ? string.Empty : HttpUtility.UrlEncode(province, Encoding.UTF8),
                    string.IsNullOrWhiteSpace(city) ? string.Empty : HttpUtility.UrlEncode(city, Encoding.UTF8),
                    _loginContext.Token);

                switch (type)
                {
                    case MpMessageType.Text:
                        if (string.IsNullOrWhiteSpace(value))
                            throw new ArgumentNullException("value", "文字消息内容为空");

                        postData.AppendFormat("&content={0}", HttpUtility.UrlEncode(value, Encoding.UTF8));
                        break;
                    case MpMessageType.Image:
                    case MpMessageType.Audio:
                    case MpMessageType.Video:
                        if (string.IsNullOrWhiteSpace(value))
                            throw new ArgumentNullException("value", "文件ID为空");

                        postData.AppendFormat("&fileid={0}", value);
                        break;
                    case MpMessageType.AppMsg:
                        if (string.IsNullOrWhiteSpace(value))
                            throw new ArgumentNullException("value", "图文消息ID为空");

                        postData.AppendFormat("&appmsgid={0}", value);
                        break;
                }

                var resultJson = RequestHelper.Post(MpAddresses.MassSendMessageUrl, postData.ToString(), _loginContext.LoginCookie);
                var resultPackage = JsonHelper.Deserialize<CommonExecuteResult>(resultJson);

                return resultPackage != null && resultPackage.ret == 0;
            });
        }