/// <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 bool DownloadMediaFileS(ReceiveMessageBase rmb) { if (rmb is VoiceReceiveMessage) { VoiceReceiveMessage _vrm = rmb as VoiceReceiveMessage; DownloadMediaRequest _request = new DownloadMediaRequest(); _request.AppIdInfo = new DefAppIdInfo(); _request.AccessToken = WxAccToken.AccessTokenStr; _request.MediaId = _vrm.MediaId; string fileName = AppDomain.CurrentDomain.BaseDirectory; if (!fileName.EndsWith("\\")) { fileName += "\\"; } fileName += "WxVoice\\"; _request.SaveDir = fileName; //Logger.WriteTxtLog("语音ID:" + _vrm.MediaId); IMpClient mpClient = new MpClient(); DownloadMediaResponse _response = mpClient.Execute(_request); if (_response.IsError) { return(false); } string _filename = _response.SaveFileName; _filename = _filename.Substring(_filename.LastIndexOf('\\') + 1); //Logger.WriteTxtLog("语音ID:" + _filename); SchoolData.语音记录(_filename); } return(true); }
/// <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="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> /// 获取AccessToken /// </summary> /// <returns></returns> private static AccessTokenInfo GetAccessToken() { DateTime _now = DateTime.Now; //有效时间到 从新获取AccessToken if (_accessToken == null || _now >= _expTime) { IMpClient mpClient = new MpClient(); AccessTokenGetRequest request = new AccessTokenGetRequest() { AppIdInfo = new DefAppIdInfo() }; AccessTokenGetResponse response = mpClient.Execute(request); if (response.IsError) { Logger.WriteTxtLog(string.Format("获取AccessToken发生错误,错误编码为:{0},错误消息为:{1}", response.ErrInfo.ErrCode, response.ErrInfo.ErrMsg)); _accessToken = null; return(null); } _accessToken = response.AccessToken; _expTime = _now.AddSeconds(_accessToken.ExpiresIn - 10); Logger.WriteTxtLog(string.Format("获取到AccessToken,值为:{0},有效期:{1}秒", response.AccessToken.AccessToken, response.AccessToken.ExpiresIn)); } return(_accessToken); }
/// <summary> /// 发送客服信息 /// </summary> /// <param name="accessToken">调用凭据</param> /// <param name="msg">客服消息</param> /// <returns></returns> public static SendCustomMessageResponse SendCustomMessage(string accessToken, CustomMessage msg) { IMpClient mpClient = new MpClient(); SendCustomMessageRequest request = new SendCustomMessageRequest() { AccessToken = accessToken, SendData = msg.ToJsonString() }; SendCustomMessageResponse response = mpClient.Execute(request); return(response); }
/// <summary> /// 获取用户基本信息 /// </summary> /// <param name="OpenID"></param> /// <returns></returns> public static User GetWxUser(string OpenID) { IMpClient mpClient = new MpClient(); GetUserInfoRequest request = new GetUserInfoRequest() { AppIdInfo = new DefAppIdInfo(), AccessToken = WxAccToken.AccessTokenStr, OpenId = OpenID }; GetUserInfoResponse response = mpClient.Execute(request); if (response.IsError) { Logger.WriteTxtLog(string.Format("获取USER发生错误,错误编码为:{0},错误消息为:{1}", response.ErrInfo.ErrCode, response.ErrInfo.ErrMsg)); return(null); } return(response.UserInfo); }
protected void OnbutUpClick(object sender, EventArgs e) { Wx.Inter.Domain.Menu _menu = new Wx.Inter.Domain.Menu(); Wx.Inter.Domain.Button _button = new Wx.Inter.Domain.Button() { key = "subkey1", name = "子按钮1", sub_button = null, type = "click", url = "http://" }; _menu.button = new List <Wx.Inter.Domain.Button> { _button }; Wx.Inter.Domain.MenuGroup mg = new Wx.Inter.Domain.MenuGroup() { menu = _menu }; string postData = mg.ToJsonString(); Wx.Inter.Request.CreateMenuRequest createRequest = new Wx.Inter.Request.CreateMenuRequest() { AccessToken = Wx.Inter.WxAccToken.AccessTokenStr, SendData = postData }; Wx.Inter.Interface.IMpClient mpClient = new Wx.Inter.MpClient(); Wx.Inter.Response.CreateMenuResponse createResponse = mpClient.Execute(createRequest); if (createResponse.IsError) { string _msg = string.Format("创建菜单失败,错误信息为:" + createResponse.ErrInfo.ErrCode + "-" + createResponse.ErrInfo.ErrMsg); Response.Write("<script>alert('" + _msg + "!');</script>"); } else { Response.Write("<script>alert('创建菜单成功');</script>"); } }
/// <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)); }