/// <summary> /// 发送视频客服信息 /// </summary> /// <param name="accessToken">调用凭据</param> /// <param name="toUser">接收方</param> /// <param name="title">视频标题</param> /// <param name="description">视频描述</param> /// <param name="videoPath">视频文件路径</param> /// <returns></returns> public static SendCustomMessageResponse SendVideoCustomMessage(string accessToken, string toUser, string title, string description, string videoPath) { IMpClient mpClient = new MpClient(); UploadMediaRequest request = new UploadMediaRequest() { AccessToken = accessToken, Type = "video", FileName = videoPath }; UploadMediaResponse response = mpClient.Execute(request); if (response.IsError) { SendCustomMessageResponse response2 = new SendCustomMessageResponse() { Body = response.Body, ErrInfo = response.ErrInfo, ReqUrl = response.ReqUrl }; return(response2); } VideoCustomMessage msg = new VideoCustomMessage() { AccessToken = accessToken, MediaId = response.MediaId, MsgType = "video", ToUser = toUser, Description = description, Title = title }; return(SendCustomMessage(accessToken, msg)); }
/// <summary> /// 发送被动响应音乐消息 /// </summary> /// <param name="accessToken">调用接口凭证</param> /// <param name="fromUserName">发送方</param> /// <param name="toUserName">接收方</param> /// <param name="title">标题</param> /// <param name="description">描述</param> /// <param name="musicUrl">音乐链接</param> /// <param name="hqMusicUrl">高质量音乐链接</param> /// <param name="thumbMediaFilePath">缩略图文件路径(64KB,支持JPG格式 )</param> /// <returns>是否成功</returns> public static bool SendMusicReplyMessage(string accessToken, string fromUserName, string toUserName, string title, string description, string musicUrl, string hqMusicUrl, string thumbMediaFilePath) { IMpClient mpClient = new MpClient(); UploadMediaRequest request = new UploadMediaRequest() { AccessToken = accessToken, Type = "thumb", FileName = thumbMediaFilePath }; UploadMediaResponse response = mpClient.Execute(request); if (response.IsError) { return(false); } else { MusicReplyMessage msg = new MusicReplyMessage() { CreateTime = Tools.ConvertDateTimeInt(DateTime.Now), FromUserName = fromUserName, ToUserName = toUserName, Description = description, Title = title, ThumbMediaId = response.MediaId, HQMusicUrl = hqMusicUrl, MusicURL = musicUrl }; System.Web.HttpContext.Current.Response.Write(msg.ToXmlString()); return(true); } }
/// <summary> /// 发送图片客服消息 /// </summary> /// <param name="accessToken">调用凭据</param> /// <param name="toUser">接收方</param> /// <param name="imgPath">图片路径</param> /// <returns></returns> public static SendCustomMessageResponse SendImageCustomMessage(string accessToken, string toUser, string imgPath) { IMpClient mpClient = new MpClient(); UploadMediaRequest request = new UploadMediaRequest() { AccessToken = accessToken, Type = "image", FileName = imgPath }; UploadMediaResponse response = mpClient.Execute(request); if (response.IsError) { SendCustomMessageResponse response2 = new SendCustomMessageResponse() { Body = response.Body, ErrInfo = response.ErrInfo, ReqUrl = response.ReqUrl }; return(response2); } ImageCustomMessage msg = new ImageCustomMessage() { AccessToken = accessToken, MediaId = response.MediaId, MsgType = "image", ToUser = toUser }; return(SendCustomMessage(accessToken, msg)); }
public static string NewCreativeTemporaryMethod() { string imgPath = "D:/Image/WeChatImage.jpg"; IMpClient mpClient = new MpClient(); var request = new AccessTokenGetRequest() { AppIdInfo = new AppIdInfo() { AppId = AppId, AppSecret = AppSecret } }; var response = mpClient.Execute(request); if (response.IsError) { return(null); } var requestUpload = new UploadMediaRequest() { AccessToken = response.AccessToken.AccessToken, Type = "image", FileName = imgPath }; var createResponse = mpClient.Execute(requestUpload); if (createResponse.IsError) { LogHelper.WriteLog(typeof(StatisticsOperation), createResponse.ErrInfo.ErrMsg); return(createResponse.ErrInfo.ErrMsg); } var jsonSerializer = new JavaScriptSerializer(); return(jsonSerializer.Serialize(createResponse.Body)); }
/// <summary> /// 发送被动响应语音消息 /// </summary> /// <param name="accessToken">调用接口凭证</param> /// <param name="fromUserName">发送方</param> /// <param name="toUserName">接收方</param> /// <param name="voicePath">语音文件路径(支持AMR\MP3,最大256K,播放长度不超过60s)</param> /// <returns>是否成功</returns> public static bool SendVoiceReplyMessage(string accessToken, string fromUserName, string toUserName, string voicePath) { IMpClient mpClient = new MpClient(); UploadMediaRequest request = new UploadMediaRequest() { AccessToken = accessToken, Type = "voice", FileName = voicePath }; UploadMediaResponse response = mpClient.Execute(request); if (response.IsError) { return(false); } else { VoiceReplyMessage msg = new VoiceReplyMessage() { CreateTime = Tools.ConvertDateTimeInt(DateTime.Now), FromUserName = fromUserName, ToUserName = toUserName, MediaId = response.MediaId }; System.Web.HttpContext.Current.Response.Write(msg.ToXmlString()); return(true); } }
/// <summary> /// 发送被动响应视频消息 /// </summary> /// <param name="accessToken">调用接口凭证</param> /// <param name="fromUserName">发送方</param> /// <param name="toUserName">接收方</param> /// <param name="title">标题</param> /// <param name="description">描述</param> /// <param name="videoPath">视频文件路径(1MB,支持MP4格式)</param> /// <returns>是否成功</returns> public static bool SendVideoReplyMessage(string accessToken, string fromUserName, string toUserName, string title, string description, string videoPath) { IMpClient mpClient = new MpClient(); var request = new UploadMediaRequest() { AccessToken = accessToken, Type = "video", FileName = videoPath }; var response = mpClient.Execute(request); if (response.IsError) { return(false); } var msg = new VideoReplyMessage() { CreateTime = Tools.ConvertDateTimeInt(DateTime.Now), FromUserName = fromUserName, ToUserName = toUserName, MediaId = response.MediaId, Description = description, Title = title }; HttpContext.Current.Response.Write(msg.ToXmlString()); return(true); }
public async Task <MediaDTO> UploadMediaToTrayAsync(FileDTO file, CancellationTokenSource cancellationTokenSource) => await Task <MediaDTO> .Run(async() => { if (!CrossConnectivity.Current.IsConnected) { throw new InvalidOperationException(AppConsts.ERROR_INTERNET_CONNECTION); } MediaDTO media = null; try { UploadMediaRequest uploadMediaRequest = new UploadMediaRequest { AccessToken = GlobalSettings.Instance.UserProfile.AccesToken, Url = GlobalSettings.Instance.Endpoints.ProfileMediaEndPoints.UploadMedia, Data = file }; UploadMediaResponse uploadMediaResponse = await _requestProvider.PostAsync <UploadMediaRequest, UploadMediaResponse>(uploadMediaRequest); if (uploadMediaResponse != null) { media = _mediaFactory.BuildMediaDTO(uploadMediaResponse); } else { throw new InvalidOperationException(CANT_UPLOAD_MEDIA_COMMON_ERROR); } } catch (ServiceAuthenticationException exc) { _identityUtilService.RefreshToken(); throw exc; } catch (Exception ex) { media = null; Crashes.TrackError(ex); Debug.WriteLine($"ERROR:{ex.Message}"); throw; } return(media); }, cancellationTokenSource.Token);
/// <summary> /// 多媒体上传接口测试 /// </summary> public static void MediaUploadTest() { IMpClient mpClient = new MpClient(); UploadMediaRequest request = new UploadMediaRequest() { AccessToken = accessToken, //获取的token Type = "image", FileName = "D:\\Up.jpg" }; UploadMediaResponse response = mpClient.Execute(request); if (response.IsError) { Console.WriteLine("上传多媒体发生错误,错误编码为:{0},错误消息为:{1}", response.ErrInfo.ErrCode, response.ErrInfo.ErrMsg); } else { Console.WriteLine("上传成功,媒体类型为{0},媒体ID为{1},时间戳为:{2}", response.Type, response.MediaId, response.CreatedAt); } }
/// <summary> /// 发送音乐客服信息 /// </summary> /// <param name="accessToken">调用凭据</param> /// <param name="toUser">接收方</param> /// <param name="title">音乐标题</param> /// <param name="description">音乐描述</param> /// <param name="musicUrl">音乐地址</param> /// <param name="hqMusicUrl">高质量音乐地址</param> /// <param name="thumbMediaFilePath">音乐缩略图路径</param> /// <returns></returns> public static SendCustomMessageResponse SendMusicCustomMessage(string accessToken, string toUser, string title, string description, string musicUrl, string hqMusicUrl, string thumbMediaFilePath) { IMpClient mpClient = new MpClient(); UploadMediaRequest request = new UploadMediaRequest() { AccessToken = accessToken, Type = "thumb", FileName = thumbMediaFilePath }; UploadMediaResponse response = mpClient.Execute(request); if (response.IsError) { SendCustomMessageResponse response2 = new SendCustomMessageResponse() { Body = response.Body, ErrInfo = response.ErrInfo, ReqUrl = response.ReqUrl }; return(response2); } MusicCustomMessage msg = new MusicCustomMessage() { AccessToken = accessToken, ThumbMediaId = response.MediaId, HqMusicUrl = hqMusicUrl, MusicUrl = musicUrl, MsgType = "music", ToUser = toUser, Description = description, Title = title }; return(SendCustomMessage(accessToken, msg)); }
/// <summary> /// 同步上传文件 /// </summary> /// <param name="fileInfo">文件信息</param> /// <param name="toUserName">发送人UserName,其实没什么用,但是官方有这个参数</param> /// <returns></returns> public UploadMediaResponse UploadFile(FileInfo fileInfo, string toUserName) { string postUrl = uploadHost + "/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json"; int bufferLength = 512 * 1024; string datetime = DateTime.Now.ToString("ddd MMM dd yyyy HH:mm:ss", CultureInfo.CreateSpecificCulture("en-US")) + " GMT+0800 (中国标准时间)"; UploadMediaRequest uploadMediaRequest = new UploadMediaRequest() { UploadType = 2, BaseRequest = baseRequest, ClientMediaId = Utils.GetJavaTimeStamp(), TotalLen = fileInfo.Length, StartPos = 0, DataLen = fileInfo.Length, MediaType = 4, FromUserName = user.UserName, ToUserName = toUserName, FileMd5 = Utils.GetFileMD5Hash(fileInfo) }; UploadMediaResponse response = null; //文件大小超过512Kb,分块上传。 if (fileInfo.Length > bufferLength) { int chunks = (int)Math.Ceiling((double)fileInfo.Length / bufferLength); byte[] buffer = new byte[bufferLength]; Stream readStream = fileInfo.OpenRead(); int chunk = 0; int readLength = 0; while ((readLength = readStream.Read(buffer, 0, buffer.Length)) != 0) { List <FormDataItem> dataList = new List <FormDataItem>() { new FormDataItem("id", "WU_FILE_" + uploadMedia.Count), new FormDataItem("name", fileInfo.Name), new FormDataItem("type", MimeMapping.GetMimeMapping(fileInfo.Name)), new FormDataItem("lastModifiedDate", datetime), new FormDataItem("size", fileInfo.Length.ToString()), new FormDataItem("chunks", chunks.ToString()), new FormDataItem("chunk", chunk.ToString()), new FormDataItem("mediatype", GetMediaType(fileInfo.Extension)), new FormDataItem("uploadmediarequest", JsonConvert.SerializeObject(uploadMediaRequest, Newtonsoft.Json.Formatting.None)), new FormDataItem("webwx_data_ticket", httpClient.CookieContainer.GetCookies(new Uri(cookieRedirectUri))["webwx_data_ticket"].Value), new FormDataItem("pass_ticket", passTicket), new FormDataItem("filename", fileInfo.Name, buffer, readLength) }; string result = httpClient.PostMutipart(postUrl, dataList); response = JsonConvert.DeserializeObject <UploadMediaResponse>(result); chunk++; } } else { byte[] buffer = new byte[fileInfo.Length]; Stream readStream = fileInfo.OpenRead(); int readLength = readStream.Read(buffer, 0, buffer.Length); List <FormDataItem> dataList = new List <FormDataItem>() { new FormDataItem("id", "WU_FILE_" + uploadMedia.Count), new FormDataItem("name", fileInfo.Name), new FormDataItem("type", MimeMapping.GetMimeMapping(fileInfo.Name)), new FormDataItem("lastModifiedDate", datetime), new FormDataItem("size", fileInfo.Length.ToString()), new FormDataItem("mediatype", GetMediaType(fileInfo.Extension)), new FormDataItem("uploadmediarequest", JsonConvert.SerializeObject(uploadMediaRequest, Newtonsoft.Json.Formatting.None)), new FormDataItem("webwx_data_ticket", httpClient.CookieContainer.GetCookies(new Uri(cookieRedirectUri))["webwx_data_ticket"].Value), new FormDataItem("pass_ticket", passTicket), new FormDataItem("filename", fileInfo.Name, buffer, readLength) }; string result = httpClient.PostMutipart(postUrl, dataList); response = JsonConvert.DeserializeObject <UploadMediaResponse>(result); } uploadMedia.Add(fileInfo.Name, response.MediaId); return(response); }
/// <summary> /// 发送被动响应语音消息 /// </summary> /// <param name="accessToken">调用接口凭证</param> /// <param name="fromUserName">发送方</param> /// <param name="toUserName">接收方</param> /// <param name="voicePath">语音文件路径(支持AMR\MP3,最大256K,播放长度不超过60s)</param> /// <returns>是否成功</returns> public static bool SendVoiceReplyMessage(string accessToken, string fromUserName, string toUserName, string voicePath) { IMpClient mpClient = new MpClient(); UploadMediaRequest request = new UploadMediaRequest() { AccessToken = accessToken, Type = "voice", FileName = voicePath }; UploadMediaResponse response = mpClient.Execute(request); if (response.IsError) { return false; } else { VoiceReplyMessage msg = new VoiceReplyMessage() { CreateTime = Tools.ConvertDateTimeInt(DateTime.Now), FromUserName = fromUserName, ToUserName = toUserName, MediaId = response.MediaId }; System.Web.HttpContext.Current.Response.Write(msg.ToXmlString()); return true; } }
/// <summary> /// 发送语音客服信息 /// </summary> /// <param name="accessToken">调用凭据</param> /// <param name="toUser">接收方</param> /// <param name="voicePath">语音文件路径</param> /// <returns></returns> public static SendCustomMessageResponse SendVoiceCustomMessage(string accessToken, string toUser, string voicePath) { IMpClient mpClient = new MpClient(); UploadMediaRequest request = new UploadMediaRequest() { AccessToken = accessToken, Type = "voice", FileName = voicePath }; UploadMediaResponse response = mpClient.Execute(request); if (response.IsError) { SendCustomMessageResponse response2 = new SendCustomMessageResponse() { Body = response.Body, ErrInfo = response.ErrInfo, ReqUrl = response.ReqUrl }; return response2; } VoiceCustomMessage msg = new VoiceCustomMessage() { AccessToken = accessToken, MediaId = response.MediaId, MsgType = "voice", ToUser = toUser }; return SendCustomMessage(accessToken, msg); }
/// <summary> /// 发送被动响应音乐消息 /// </summary> /// <param name="accessToken">调用接口凭证</param> /// <param name="fromUserName">发送方</param> /// <param name="toUserName">接收方</param> /// <param name="title">标题</param> /// <param name="description">描述</param> /// <param name="musicUrl">音乐链接</param> /// <param name="hqMusicUrl">高质量音乐链接</param> /// <param name="thumbMediaFilePath">缩略图文件路径(64KB,支持JPG格式 )</param> /// <returns>是否成功</returns> public static bool SendMusicReplyMessage(string accessToken, string fromUserName, string toUserName, string title, string description, string musicUrl, string hqMusicUrl, string thumbMediaFilePath) { IMpClient mpClient = new MpClient(); UploadMediaRequest request = new UploadMediaRequest() { AccessToken = accessToken, Type = "thumb", FileName = thumbMediaFilePath }; UploadMediaResponse response = mpClient.Execute(request); if (response.IsError) { return false; } else { MusicReplyMessage msg = new MusicReplyMessage() { CreateTime = Tools.ConvertDateTimeInt(DateTime.Now), FromUserName = fromUserName, ToUserName = toUserName, Description = description, Title = title, ThumbMediaId = response.MediaId, HQMusicUrl = hqMusicUrl, MusicURL = musicUrl }; System.Web.HttpContext.Current.Response.Write(msg.ToXmlString()); return true; } }
/// <summary> /// 发送音乐客服信息 /// </summary> /// <param name="accessToken">调用凭据</param> /// <param name="toUser">接收方</param> /// <param name="title">音乐标题</param> /// <param name="description">音乐描述</param> /// <param name="musicUrl">音乐地址</param> /// <param name="hqMusicUrl">高质量音乐地址</param> /// <param name="thumbMediaFilePath">音乐缩略图路径</param> /// <returns></returns> public static SendCustomMessageResponse SendMusicCustomMessage(string accessToken, string toUser, string title, string description, string musicUrl, string hqMusicUrl, string thumbMediaFilePath) { IMpClient mpClient = new MpClient(); UploadMediaRequest request = new UploadMediaRequest() { AccessToken = accessToken, Type = "thumb", FileName = thumbMediaFilePath }; UploadMediaResponse response = mpClient.Execute(request); if (response.IsError) { SendCustomMessageResponse response2 = new SendCustomMessageResponse() { Body = response.Body, ErrInfo = response.ErrInfo, ReqUrl = response.ReqUrl }; return response2; } MusicCustomMessage msg = new MusicCustomMessage() { AccessToken = accessToken, ThumbMediaId = response.MediaId, HqMusicUrl = hqMusicUrl, MusicUrl = musicUrl, MsgType = "music", ToUser = toUser, Description = description, Title = title }; return SendCustomMessage(accessToken, msg); }
/// <summary> /// 上传多媒体文件 /// </summary> /// <param name="media"></param> /// <param name="access_token"></param> /// <returns></returns> public UploadMediaResult UploadMedia(string access_token, UploadMediaRequest media) { string url = string.Format("{0}/cgi-bin/media/upload?access_token={1}&type={2}", BaseUrl, access_token, media.GetMediaType()); return(JsonHelper.Decode <UploadMediaResult>(HttpHelper.HttpPost.GetMediaResult(url, media.AsPostMedia()))); }