/// <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> /// 新增其他类型永久素材 /// </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> /// 正式写入日志到Redis /// </summary> /// <typeparam name="TState">泛型</typeparam> /// <param name="logLevel">日志等级</param> /// <param name="eventId">日志触发事件</param> /// <param name="state">泛型实体</param> /// <param name="exception">异常信息</param> /// <param name="formatter">格式化处理</param> public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter) { if (!IsEnabled(logLevel)) { return; } var msg = $"日志触发ID:{eventId.Id},日志名:{eventId.Name},具体》》" + formatter(state, exception); switch (logLevel) { case LogLevel.Trace: case LogLevel.Debug: SystemLogHelper.Debug(moduleName, msg, exception); break; case LogLevel.Information: SystemLogHelper.Info(moduleName, msg, exception); break; case LogLevel.Warning: SystemLogHelper.Warn(moduleName, msg, exception); break; case LogLevel.Error: SystemLogHelper.Error(moduleName, msg, exception); break; case LogLevel.Critical: SystemLogHelper.Fatal(moduleName, msg, exception); break; default: break; } }
/// <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> /// 输出Warn的日志提示 /// </summary> /// <param name="message">日志提示消息</param> /// <param name="exception">可指定发生的异常对象</param> /// <param name="isWriteLog">是否需要记录到系统日志,默认需要</param> public void Warn(string message, Exception exception = null, bool isWriteLog = true) { showLog(message + exception?.GetExceptionMsg() + $"---{DateTime.Now} ThreadId:{Thread.CurrentThread.ManagedThreadId}\r\n", Color.DarkOrange); if (isWriteLog) { SystemLogHelper.Warn(MethodBase.GetCurrentMethod(), message, exception); } }
/// <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> /// <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> /// <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> /// <param name="subscribeMsg">订阅模板消息</param> /// <returns>订阅模板消息推送结果</returns> public WeChatResult SendSubscribe(OnceSubscribeMsg subscribeMsg) { string accessToken = connect.GetAccessToken(); string url = $"https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token={accessToken}"; string resultData = SimulateRequest.HttpPost(url, subscribeMsg); WeChatResult weChatResult = new WeChatResult(resultData); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"推送订阅模板消息给到授权微信用户SendSubscribe,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <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> /// <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> /// 发送客服输入状态 /// <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="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 <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> /// 设置群发速度 /// </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="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); }
/// <summary> /// 根据OpenID列表群发 /// </summary> /// <param name="tagSendParam">OpenID列表群发参数</param> /// <returns>OpenID列表群发结果</returns> public WeChatResult <GroupSendResult> SendAll(OpenIdSendParam openIdSendParam) { string accessToken = connect.GetAccessToken(); string url = $"https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={accessToken}"; string resultData = SimulateRequest.HttpPost(url, openIdSendParam); WeChatResult <GroupSendResult> weChatResult = new WeChatResult <GroupSendResult>(resultData); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(MethodBase.GetCurrentMethod(), $"根据OpenID列表群发SendAll,微信服务报错:{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> /// <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="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="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="userId">user_id可以是粉丝的OpenID,也可以是粉丝的微信号</param> /// <returns>微信服务返回的匹配菜单</returns> public WeChatResult <OrdinaryMenu> TryMatchMenu(string userId) { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token=" + accessToken; var matchParam = new { user_id = userId }; string resultStr = SimulateRequest.HttpPost(url, matchParam, "application/json"); WeChatResult <OrdinaryMenu> matchMenuResult = new WeChatResult <OrdinaryMenu>(resultStr); if (matchMenuResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"测试个性化菜单匹配结果TryMatchMenu(),微信服务报错:{matchMenuResult}"); } return(matchMenuResult); }
/// <summary> /// 获取公众号的自动回复规则 /// </summary> /// <returns>自动回复规则</returns> public WeChatResult <JObject> GetAutoReplyRule() { string url = "https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info?access_token={0}"; string accessToken = connect.GetAccessToken(); url = String.Format(url, accessToken); WeChatResult <JObject> weChatResult = SimulateRequest.HttpGet <WeChatResult <JObject> >(url); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"获取微信服务器IP地址GetWeChatServerIP(),微信服务报错:{weChatResult}"); } return(weChatResult); }
//private static List<MenuButton> LimitMenuNum(List<MenuButton> menuButtonList) //{ // //开始生成微信菜单按钮的请求字符串 // int minLength = Math.Min(menuButtonList.Count, 3); // if (menuButtonList.Count > 3) // menuButtonList = menuButtonList.Take(3).ToList();//最多包括3个一级菜单 // menuButtonList.ForEach(menuBtn => // { // if (menuBtn is ParentButton) // { // var parentBtn = menuBtn as ParentButton; // if (parentBtn.sub_button != null && parentBtn.sub_button.Count > 5) // parentBtn.sub_button = parentBtn.sub_button.Take(5).ToList();//每个一级菜单最多包含5个二级菜单 // } // }); // return menuButtonList; //} /// <summary> /// 获取默认菜单和全部个性化菜单信息 /// </summary> /// <returns>微信服务返回的菜单信息</returns> public WeChatResult <MenuQueryInfo> SearchMenu() { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + accessToken; string resultStr = SimulateRequest.HttpGet(url); WeChatResult <MenuQueryInfo> menuResult = new WeChatResult <MenuQueryInfo>(resultStr); if (menuResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"获取默认菜单和全部个性化菜单信息SearchMenu(),微信服务报错:{menuResult}"); return(menuResult); } JObject menuJobj = JObject.Parse(resultStr); JToken ordinaryMenu = menuJobj["menu"];//一个默认菜单 if (ordinaryMenu == null) { return(menuResult); } JArray menuButtonArray = ordinaryMenu["button"] as JArray;//默认菜单中的1~3个主菜单 if (menuButtonArray == null) { return(menuResult); } MenuQueryInfo menuQueryInfo = menuResult.resultData; AnalysisMenuButton(menuButtonArray, menuQueryInfo.menu.button); JArray personalMenuArray = menuJobj["conditionalmenu"] as JArray;//多个个性化菜单 if (personalMenuArray == null) { return(menuResult); } foreach (JToken personalMenu in personalMenuArray) { if (personalMenu == null) { return(menuResult); } int index = personalMenuArray.IndexOf(personalMenu); JArray personalButtonArray = personalMenu["button"] as JArray;//默认菜单中的1~3个主菜单 if (personalButtonArray == null) { return(menuResult); } AnalysisMenuButton(personalMenu["button"] as JArray, menuQueryInfo.conditionalmenu[index].button); } return(menuResult); }
/// <summary> /// 设置所属行业,每月可修改行业1次 /// </summary> /// <param name="industry1">公众号模板消息所属行业编号</param> /// <param name="industry2">公众号模板消息所属行业编号</param> /// <returns></returns> public WeChatResult SetIndustry(WeChatIndustry industry1, WeChatIndustry industry2) { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=" + accessToken;//POST string postData = JsonConvert.SerializeObject(new { industry_id1 = industry1, industry_id2 = industry2 }); string resultStr = SimulateRequest.HttpPost(url, postData); WeChatResult weChatResult = new WeChatResult(resultStr); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"设置所属行业SetIndustry,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 删除指定的个性化菜单 /// </summary> /// <param name="menuId">个性化菜单的Id</param> /// <returns>微信服务返回的删除结果</returns> public WeChatResult DeleteMenu(string menuId) { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=" + accessToken; var delMenu = new { menuid = menuId }; string resultStr = SimulateRequest.HttpPost(url, delMenu, "application/json"); WeChatResult deleteResult = new WeChatResult(resultStr); if (deleteResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"删除指定的个性化菜单DeleteMenu(),微信服务报错:{deleteResult}"); } return(deleteResult); }
/// <summary> /// 发送客服消息给指定微信用户,可指定客服账号 /// </summary> /// <param name="serviceMsg">需发送的客服消息,可指定客服账号</param> /// <returns>发送客服消息的微信结果</returns> public WeChatResult SendMsg(CustomerServiceMsg serviceMsg) { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken; string postData = JsonConvert.SerializeObject(serviceMsg); string resultStr = SimulateRequest.HttpPost(url, postData); WeChatResult weChatResult = new WeChatResult(resultStr); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"获取所有客服账号SearchKFList,微信服务报错:{weChatResult}"); } return(weChatResult); }
/// <summary> /// 给公众号添加消息模板,获得模板ID /// </summary> /// <param name="templateShortId">模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式</param> /// <returns>公众号获得的模板ID</returns> public WeChatResult <TemplateID> AddTemplate(string templateShortId) { string accessToken = connect.GetAccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=" + accessToken; string resultData = SimulateRequest.HttpPost(url, new { template_id_short = templateShortId }); WeChatResult <TemplateID> weChatResult = new WeChatResult <TemplateID>(resultData); if (weChatResult.errcode != WeChatErrorCode.SUCCESS) { SystemLogHelper.Warn(GetType().FullName, $"获取设置的行业信息GetIndustry,微信服务报错:{weChatResult}"); } return(weChatResult); }