/// <summary> /// 群发消息预览,微信支持多种预览账号,但每次只能一个,所以OpenIdSendParam.touser参数只第一个值有效并转成微信需要的参数键名 /// </summary> /// <param name="previewAccount">touser字段都可以改为towxname,这样就可以针对微信号进行预览(而非openID),towxname和touser同时赋值时,以towxname优先</param> /// <param name="openIdSendParam">与实际群发参数一致</param> /// <returns>群发消息预览发送结果</returns> public WeChatResult <GroupSendResult> PreviewSendAll(PreviewAccount previewAccount, OpenIdSendParam openIdSendParam) { string accessToken = connect.GetAccessToken(); string url = $"https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token={accessToken}"; if (openIdSendParam.touser == null || openIdSendParam.touser.Count == 0) { throw new ArgumentException("OpenIdSendParam.touser必须指定最少一个预览账号"); } string previewWx = openIdSendParam.touser[0]; string originJson = JsonConvert.SerializeObject(openIdSendParam); JObject jobject = JObject.Parse(originJson); jobject.Remove("touser"); if (previewAccount == PreviewAccount.OpenId) { jobject.Add($"\"touser\":\"{previewWx}\""); } else { jobject.Add($"\"towxname\":\"{previewWx}\""); } string resultStr = SimulateRequest.HttpPost(url, jobject.ToString()); WeChatResult <GroupSendResult> weChatResult = JsonConvert.DeserializeObject <WeChatResult <GroupSendResult> >(resultStr); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"群发消息预览PreviewSendAll,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 直接去微信获取AccessToken /// </summary> /// <returns>微信提供的AccessToken</returns> private string GetWeixinAccessToken() { logObj.Info("直接去微信获取AccessToken,GetWeixinAccessToken()"); string accessToken = String.Empty; string appId = ConfigurationManager.AppSettings["AppID"]; string secret = ConfigurationManager.AppSettings["AppSecret"]; string url = String.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appId, secret); string resultStr = SimulateRequest.HttpGet(url); var resultObj = JsonConvert.DeserializeObject <JObject>(resultStr); if (resultObj["errcode"] != null) { logObj.Info("直接去微信获取AccessToken,GetWeixinAccessToken(),微信服务报错"); var errorCode = new WeixinErrorInfo(resultObj["errcode"].ToString()); logObj.Warn(string.Format("{0}【errmsg:{1}】", errorCode.ToString(), resultObj["errmsg"].ToString())); } else { accessToken = resultObj["access_token"].ToString(); logObj.InfoFormat("直接去微信获取AccessToken,GetWeixinAccessToken(),微信access_token:{0}", accessToken); } WeChatDataManage.InsertWeChatData(new WeChatData() { Key = "AccessToken", Value = resultStr }); logObj.Info("直接去微信获取AccessToken,GetWeixinAccessToken(),返回:" + accessToken); return(accessToken); }
public void AddTempMaterialTest() { WeChatResult <TempMaterialResult> weChatResult = materialLinkUp.AddTempMaterial( "G:\\FFOutput\\wxtest.amr"); WeChatResult <string> fileResult; if (string.IsNullOrWhiteSpace(weChatResult.resultData.media_id)) { fileResult = materialLinkUp.GetTempMaterial(weChatResult.resultData.thumb_media_id, "F:\\ps\\FileImg\\file.txt"); } else { fileResult = materialLinkUp.GetTempMaterial(weChatResult.resultData.media_id, "F:\\ps\\FileImg\\file.txt"); } try { JObject videoResult = JsonConvert.DeserializeObject <JObject>(fileResult.resultData); string fileName = SimulateRequest.DownloadFile(videoResult["video_url"].ToString(), "F:\\ps\\FileImg\\bigBreast.txt"); Assert.IsNotNull(fileName); } catch (Exception ex) { Assert.IsNotNull(ex.Message); } Assert.IsNotNull(fileResult.resultData); }
/// <summary> /// 随机获取笑话 /// </summary> /// <returns>随机笑话数据集合</returns> public JuHeResponse <JokesData> RandomJokes() { string url = string.Format("http://v.juhe.cn/joke/randJoke.php?key={0}", JuHeOptions.AppKey); JuHeResponse <JokesData> jokesDataResult = SimulateRequest.HttpGet <JuHeResponse <JokesData> >(url); return(jokesDataResult); }
/// <summary> /// 新增临时素材,注意3天后media_id失效 /// </summary> /// <param name="filePathName">指定包含完整路径的文件名</param> /// <returns>微信服务返回的创建结果</returns> public WeChatResult <TempMaterialResult> AddTempMaterial(string filePathName) { string url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}"; string accessToken = connect.GetAccessToken(); string type = MimeMapping.GetMimeMapping(filePathName); //获取文件的Mime-Type type = type.Substring(0, type.IndexOf('/')); //提取Mime-Type的前部分 string fileName = Path.GetFileName(filePathName); using (Stream fileStream = new FileStream(filePathName, FileMode.Open)) { if ("image".Equals(type) && fileStream.Length < 64 * 1024) { type = "thumb";//如果文件属于图片,而且小于64KB的,类型改成缩略图(thumb) } else if ("audio".Equals(type)) { type = "voice"; } url = string.Format(url, accessToken, type); string resultStr = SimulateRequest.UploadFile(new UploadFileParam(url, fileName, fileStream)); WeChatResult <TempMaterialResult> weChatResult = new WeChatResult <TempMaterialResult>(resultStr); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(MethodBase.GetCurrentMethod(), $"新增临时素材AddTempMaterial,微信服务报错:{weChatResult}"); } return(weChatResult); } }
/// <summary> /// 上传多媒体文件,返回上传成功的media_id,否则null /// </summary> /// <param name="filePathName">包括完整路径的文件名</param> /// <param name="fileData">文件数据</param> /// <param name="fileType">文件类型:图片(image)、语音(voice)、视频(video)和缩略图(thumb,主要用于视频与音乐格式的缩略图)</param> /// <returns>上传成功的media_id,否则null</returns> public MediaFile UploadMediaFile(string filePathName, string fileType) { //上传的多媒体文件有格式和大小限制,如下: //图片(image): 1M,支持JPG格式 //语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式 //视频(video):10MB,支持MP4格式 //缩略图(thumb):64KB,支持JPG格式 //媒体文件在后台保存时间为3天,即3天后media_id失效 string url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}"; Stream fileStream = new FileStream(filePathName, FileMode.Open, FileAccess.Read); UploadParameter uploadParam = new UploadParameter() { FileNameValue = Path.GetFileName(filePathName), UploadStream = fileStream }; url = string.Format(url, Connect.GetAccessToken(), fileType); uploadParam.Url = url; string resultStr = SimulateRequest.UploadFile(uploadParam); var resultObj = JsonConvert.DeserializeObject <JObject>(resultStr); if (resultObj["errcode"] != null && resultObj["errcode"].ToString() != "0") { _logObj.Error("上传多媒体文件UploadMediaFile(),微信服务报错"); var errorCode = new WeixinErrorInfo(resultObj["errcode"].ToString()); _logObj.Warn(string.Format("{0}【errmsg:{1}】", errorCode.ToString(), resultObj["errmsg"].ToString())); return(null); } else { _logObj.InfoFormat("上传多媒体文件UploadMediaFile(),微信处理成功"); MediaFile mediaFile = JsonConvert.DeserializeObject <MediaFile>(resultStr); return(mediaFile); } }
/// <summary> /// 获取天气服务提供的区域信息 /// </summary> /// <param name="level">区域等级</param> /// <param name="areaCode">区域代号,当level=1时请默认留空</param> /// <returns>区域信息</returns> public List <AreaCodeName> SearchAreaCode(AreaLevel level, string areaCode = "") { string redisKey = string.Format(RedisKeyPrefix.AREA_CODE_LIST, level, areaCode); List <AreaCodeName> areaInfoList = redisHelper.StringGet <List <AreaCodeName> >(redisKey);//先从缓存中读取 if (areaInfoList != null) { return(areaInfoList); } string getUrl = string.Format("http://www.weather.com.cn/data/list3/city{0}.xml?level={1}", areaCode, (int)level); string areaStr = SimulateRequest.HttpGet(getUrl); string[] areaResultAry = areaStr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); areaInfoList = areaResultAry.Select(areaResult => { string[] areaAry = areaResult.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); return(new AreaCodeName { Code = areaAry[0], Name = areaAry[1] }); }).ToList(); if (areaInfoList != null && areaInfoList.Count > 0) { redisHelper.StringSet(redisKey, areaInfoList, TimeSpan.FromSeconds(CacheTime.Term_S)); } return(areaInfoList); }
/// <summary> /// 获取城市天气信息 /// </summary> /// <param name="cityId">天气城市代号</param> /// <returns>返回城市天气信息,参数无法解析会返回null</returns> public WeatherInfo GetWeatherInfo(string cityId) { string getUrl = string.Format("http://www.weather.com.cn/data/cityinfo/{0}.html", cityId); JObject weatherResult = SimulateRequest.HttpGet <JObject>(getUrl); string weatherStr = weatherResult["weatherinfo"]?.ToString(); if (!string.IsNullOrWhiteSpace(weatherStr)) { ClimateInfo climate = JsonConvert.DeserializeObject <ClimateInfo>(weatherStr); if (climate != null) { WeatherInfo weatherSky = new WeatherInfo() { CityId = climate.cityid, CityName = climate.city, TempMin = climate.temp1, TempMax = climate.temp2, WeatherDesc = climate.weather, WindImg1 = climate.img1, WindImg2 = climate.img2, Time = climate.ptime }; } } return(null); }
/// <summary> /// 新增其他类型永久素材 /// </summary> /// <param name="filePathName">指定包含完整路径的文件名</param> /// <param name="videoDescription">上传视频素材时,需提供视频的描述信息</param> /// <returns>新增永久素材的结果</returns> public WeChatResult <MaterialResult> AddMaterial(string filePathName, VideoDescription videoDescription = null) { string accessToken = connect.GetAccessToken(); string type = MimeMapping.GetMimeMapping(filePathName); //获取文件的Mime-Type type = type.Substring(0, type.IndexOf('/')); //提取Mime-Type的前部分 string fileName = Path.GetFileName(filePathName); using (Stream fileStream = new FileStream(filePathName, FileMode.Open)) { if ("image".Equals(type) && fileStream.Length < 64 * 1024) { type = "thumb";//如果文件属于图片,而且小于64KB的,类型改成缩略图(thumb) } else if ("audio".Equals(type)) { type = "voice"; } string url = $"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={accessToken}&type={type}"; UploadFileParam fileParam = new UploadFileParam(url, fileName, fileStream); if (videoDescription != null) { fileParam.PostParameters.Add("title", videoDescription.title); fileParam.PostParameters.Add("introduction", videoDescription.introduction); } string resultStr = SimulateRequest.UploadFile(fileParam); WeChatResult <MaterialResult> weChatResult = new WeChatResult <MaterialResult>(resultStr); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(MethodBase.GetCurrentMethod(), $"上传图文消息内的图片获取URLGetUrlByUpdateImg,微信服务报错:{weChatResult}"); } return(weChatResult); } }
/// <summary> /// 直接去微信获取AccessToken /// </summary> /// <returns>微信提供的AccessToken</returns> private string GetWeChatAccessToken() { if (WeChatConfig == null) { throw new NullReferenceException("请使用构造ConnectLinkUp(idOrAppId),或初始化Initialize(idOrAppId)"); } string accessToken = String.Empty; string appId = WeChatConfig.AppID; string secret = WeChatConfig.AppSecret; string url = String.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appId, secret); string resultStr = SimulateRequest.HttpGet(url); WeChatResult <AccessToken> accessTokenResult = new WeChatResult <AccessToken>(resultStr); if (accessTokenResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(MethodBase.GetCurrentMethod(), $"直接去微信获取AccessToken,GetWeChatAccessToken(),{accessTokenResult}"); } else { accessToken = accessTokenResult.resultData.access_token; string accessTokenKey = string.Format(RedisKeyPrefix.WECHAT_ACCESS_TOKEN, appId); int validTime = accessTokenResult.resultData.expires_in - 600;//有效时间提前10分钟 redisHelper.StringSet(accessTokenKey, accessTokenResult.resultData, TimeSpan.FromSeconds(validTime)); } return(accessToken); }
/// <summary> /// 获取城市天空气象信息 /// </summary> /// <returns>返回天空气象信息,参数无法解析会返回null</returns> public WeatherSky GetWeatherSky(string cityId) { string getUrl = string.Format("http://www.weather.com.cn/data/sk/{0}.html", cityId); JObject weatherResult = SimulateRequest.HttpGet <JObject>(getUrl); string weatherStr = weatherResult["weatherinfo"]?.ToString(); if (!string.IsNullOrWhiteSpace(weatherStr)) { SkyClimate skyClimate = JsonConvert.DeserializeObject <SkyClimate>(weatherStr); if (skyClimate != null) { WeatherSky weatherSky = new WeatherSky() { CityId = skyClimate.cityid, CityName = skyClimate.city, Condition = skyClimate.njd, Humidity = skyClimate.SD, Sm = skyClimate.sm, Temperature = skyClimate.temp, Time = skyClimate.time, WindDirection = skyClimate.WD, WindGrade = skyClimate.WS, WindPower = skyClimate.AP, WindGradeE = skyClimate.WSE, IsRadar = skyClimate.isRadar, Radar = skyClimate.Radar }; } } return(null); }
/// <summary> /// 从微信服务器下载多媒体文件,返回实际存储在本地的文件名,null则下载失败 /// </summary> /// <param name="filePathName">保存在本地的文件名,(由于文件扩展名未知)文件名以实际返回为准</param> /// <param name="media_id">媒体文件Id</param> /// <returns>实际存储在本地的文件名</returns> public string DownloadMediaFile(string filePathName, string media_id) { string url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}"; url = string.Format(url, Connect.GetAccessToken(), media_id); try { string fileName = SimulateRequest.DownloadFile(url, filePathName); _logObj.InfoFormat("下载多媒体文件DownloadMediaFile(),微信处理成功,文件存储为:" + fileName); return(fileName); } catch (Exception ex) { if (ex.Message.StartsWith("{") && ex.Message.EndsWith("}")) { var resultObj = JsonConvert.DeserializeObject <JObject>(ex.Message); if (resultObj["errcode"] != null && resultObj["errcode"].ToString() != "0") { _logObj.Error("下载多媒体文件DownloadMediaFile(),微信服务报错"); var errorCode = new WeixinErrorInfo(resultObj["errcode"].ToString()); _logObj.Warn(string.Format("{0}【errmsg:{1}】", errorCode.ToString(), resultObj["errmsg"].ToString())); } } else { _logObj.Error("下载多媒体文件DownloadMediaFile(),出错", ex); } } return(null); }
/// <summary> /// 查找最新笑话数据 /// </summary> /// <param name="pageIndex">当前页数,默认1</param> /// <param name="pageSize">每次返回条数,默认1,最大20</param> /// <returns>最新笑话数据集合</returns> public JuHeResponse <JokesData> SearchNewestJokes(int pageIndex = 1, int pageSize = 20) { string url = string.Format("http://v.juhe.cn/joke/content/text.php?key={0}&page={1}&pagesize={2}", JuHeOptions.AppKey, pageIndex, pageSize); JuHeResponse <JokesData> jokesDataResult = SimulateRequest.HttpGet <JuHeResponse <JokesData> >(url); return(jokesDataResult); }
/// <summary> /// 获取逆地理编码信息 /// </summary> /// <param name="reGeoCodeParam">逆地理编码所需参数</param> /// <returns>逆地理编码信息</returns> public ReGeoCodeResult GetReGeoCode(ReGeoCodeParam reGeoCodeParam) { string getUrl = "https://restapi.amap.com/v3/geocode/geo?{0}"; string paras = QueryHelper.ToQueryString(reGeoCodeParam); getUrl = string.Format(getUrl, paras); ReGeoCodeResult reGeoCodeResult = SimulateRequest.HttpGet <ReGeoCodeResult>(getUrl); return(reGeoCodeResult); }
/// <summary> /// 获取设置的行业信息 /// </summary> /// <returns>公众号设置的行业信息</returns> public WeChatResult <IndustryQuery> GetIndustry() { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=" + accessToken; WeChatResult <IndustryQuery> weChatResult = SimulateRequest.HttpGet <WeChatResult <IndustryQuery> >(url); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"获取设置的行业信息GetIndustry,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 获取公众号已添加的模板列表 /// </summary> /// <returns>公众号获得的模板列表</returns> public WeChatResult <TemplateListInfo> SearchTemplate() { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=" + accessToken; WeChatResult <TemplateListInfo> weChatResult = SimulateRequest.HttpGet <WeChatResult <TemplateListInfo> >(url); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"获取公众号已添加的模板列表SearchTemplate,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 新增永久图文素材 /// </summary> /// <param name="articleNews">图文消息素材</param> /// <returns>图文素材MediaId</returns> public WeChatResult <Media_Msg> AddNews(ArticleNews articleNews) { string accessToken = connect.GetAccessToken(); string url = $"https://api.weixin.qq.com/cgi-bin/material/add_news?access_token={accessToken}"; string resultStr = SimulateRequest.HttpPost(url, articleNews); WeChatResult <Media_Msg> weChatResult = new WeChatResult <Media_Msg>(resultStr); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(MethodBase.GetCurrentMethod(), $"新增永久图文素材AddNews,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 发送模板消息 /// </summary> /// <param name="templateMessage">发送模板消息的参数</param> /// <returns>发送模板消息获得的消息ID</returns> public WeChatResult <Msg_ID> SendTemplate(TemplateMessageParam templateMessage) { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken; string resultData = SimulateRequest.HttpPost(url, templateMessage); WeChatResult <Msg_ID> weChatResult = new WeChatResult <Msg_ID>(resultData); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"发送模板消息SendTemplate,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 根据标签进行群发 /// </summary> /// <param name="tagSendParam">标签群发参数</param> /// <returns>标签进行群发结果</returns> public WeChatResult <GroupSendResult> SendAll(TagSendParam tagSendParam) { string accessToken = connect.GetAccessToken(); string url = $"https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={accessToken}"; string resultData = SimulateRequest.HttpPost(url, tagSendParam); WeChatResult <GroupSendResult> weChatResult = new WeChatResult <GroupSendResult>(resultData); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"根据标签进行群发SendAll,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 上传图文消息素材 /// </summary> /// <param name="articleNews">图文消息素材</param> /// <returns>上传图文消息素材结果</returns> public WeChatResult <UploadNewsResult> UpdateNews(ArticleNews articleNews) { string accessToken = connect.GetAccessToken(); string url = $"https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token={accessToken}"; string resultStr = SimulateRequest.HttpPost(url, articleNews); WeChatResult <UploadNewsResult> weChatResult = new WeChatResult <UploadNewsResult>(resultStr); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"上传图文消息素材UpdateNews,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 获取素材总数 /// </summary> /// <returns>微信服务返回的数量结果</returns> public WeChatResult <MaterialCount> GetMaterialCount() { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=" + accessToken; string resultStr = SimulateRequest.HttpGet(url); WeChatResult <MaterialCount> weChatResult = new WeChatResult <MaterialCount>(resultStr); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(MethodBase.GetCurrentMethod(), $"获取素材总数GetMaterialCount,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 删除默认菜单,同时删除全部个性化菜单 /// </summary> /// <returns>微信服务返回的删除结果</returns> public WeChatResult DeleteMenu() { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken; string resultStr = SimulateRequest.HttpGet(url); WeChatResult deleteResult = new WeChatResult(resultStr); if (deleteResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"删除默认菜单DeleteMenu(),微信服务报错:{deleteResult}"); } return(deleteResult); }
/// <summary> /// 查找当前公众号使用的自定义菜单的配置,包括API设置的菜单和公众平台官网通过网站功能发布的菜单 /// </summary> /// <returns>自定义菜单的配置JObject信息</returns> public WeChatResult <JObject> SearchCustomMenu() { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" + accessToken; string resultStr = SimulateRequest.HttpGet(url); WeChatResult <JObject> customMenuResult = new WeChatResult <JObject>(resultStr); if (customMenuResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"查找当前公众号使用的自定义菜单的配置SearchCustomMenu(),微信服务报错:{customMenuResult}"); } return(customMenuResult); }
/// <summary> /// 获取城市天气详情信息 /// </summary> /// <param name="cityId">天气城市代号</param> /// <returns>城市天气详情信息</returns> public CityWeather GetCityWeather(string cityId) { string getUrl = string.Format("http://wthrcdn.etouch.cn/WeatherApi?citykey={0}", cityId); string weatherXml = SimulateRequest.HttpClientGet(getUrl, true); if (string.IsNullOrWhiteSpace(weatherXml)) { throw new WeatherException(-200, "请求到的天气数据Xml为空!"); } CityWeather cityWeather = XmlConvert.DeserializeObject <CityWeather>(weatherXml); return(cityWeather); }
/// <summary> /// 删除永久素材 /// </summary> /// <param name="mediaId">要删除的永久素材Id</param> /// <returns>微信服务返回的删除结果</returns> public WeChatResult DelMaterial(string mediaId) { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=" + accessToken; string resultStr = SimulateRequest.HttpPost(url, new { media_id = mediaId }); WeChatResult weChatResult = new WeChatResult(resultStr); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(MethodBase.GetCurrentMethod(), $"删除永久素材DelMaterial,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 设置群发速度 /// </summary> /// <param name="msgId">群发消息后返回的消息id</param> /// <returns>发送状态查询结果</returns> public WeChatResult SetSendSpeed(int speed) { string accessToken = connect.GetAccessToken(); string url = $"https://api.weixin.qq.com/cgi-bin/message/mass/speed/set?access_token={accessToken}"; string resultData = SimulateRequest.HttpPost(url, new { speed }); WeChatResult weChatResult = new WeChatResult(resultData); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"设置群发速度SetSendSpeed,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 获取所有客服账号,获取公众号中所设置的客服基本信息,包括客服工号、客服昵称、客服登录账号 /// </summary> /// <param name="account">账号,如:[email protected]</param> /// <param name="nickName">昵称,如:客服1</param> /// <param name="password">客服登录密码,内部会转成md5</param> /// <returns>删除的微信结果</returns> public WeChatResult <KFAccountInfo> SearchKFList() { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=" + accessToken; string resultStr = SimulateRequest.HttpGet(url); WeChatResult <KFAccountInfo> weChatResult = new WeChatResult <KFAccountInfo>(resultStr); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"获取所有客服账号SearchKFList,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 获取群发速度 /// </summary> /// <returns>发送状态查询结果</returns> public WeChatResult <GroupSendSpeed> GetSendSpeed() { string accessToken = connect.GetAccessToken(); string url = $"https://api.weixin.qq.com/cgi-bin/message/mass/speed/get?access_token={accessToken}"; WeChatResult <GroupSendSpeed> weChatResult = SimulateRequest .HttpGet <WeChatResult <GroupSendSpeed> >(url); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"获取群发速度GetSendSpeed,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 发送客服输入状态 /// <para>下发输入状态,需要客服之前30秒内跟用户有过消息交互</para> /// <para>在输入状态中(持续15s),不可重复下发输入态</para> /// <para>在输入状态中,如果向用户下发消息,会同时取消输入状态</para> /// </summary> /// <param name="serviceMsg">需发送的客服消息,可指定客服账号</param> /// <returns>发送客服消息的微信结果</returns> public WeChatResult SendTyping(string openId, WxMsgCommand msgCommand) { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/message/custom/typing?access_token=" + accessToken; string resultStr = SimulateRequest.HttpPost(url, new { touser = openId, command = msgCommand.ToString() }); WeChatResult weChatResult = new WeChatResult(resultStr); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"获取所有客服账号SearchKFList,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 查询群发消息发送状态 /// </summary> /// <param name="msgId">群发消息后返回的消息id</param> /// <returns>发送状态查询结果</returns> public WeChatResult <GroupSendStatusResult> GetSendStatus(int msgId) { string accessToken = connect.GetAccessToken(); string url = $"https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token={accessToken}"; string resultData = SimulateRequest.HttpPost(url, new { msg_id = msgId }); WeChatResult <GroupSendStatusResult> weChatResult = new WeChatResult <GroupSendStatusResult>(resultData); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"查询群发消息发送状态GetSendStatus,微信服务报错:{weChatResult}"); } return(weChatResult); }