在首先得到了AccessToken的前提之下就可以对当前企业的成员进行相关的消息推送
/// <summary> /// 发文本文件给指定的用户 /// </summary> /// <param name="corpId"></param> /// <param name="text"></param> /// <param name="lstUserId">企业微信用户对应的OpenId</param> /// <param name="agentId"></param> /// <param name="agentSecret"></param> /// <returns></returns> public static ExeMsgInfo SendTextMsgToUserList(int agentId, string agentSecret, string corpId,string text, List<String> lstUserId) { ExeMsgInfo exeMsgInfo = new ExeMsgInfo(); if (lstUserId == null || lstUserId.Count == 0) { exeMsgInfo.RetValue = "目标用户不能为空"; return exeMsgInfo; } if (string.IsNullOrEmpty(corpId) || string.IsNullOrEmpty(agentSecret)) { exeMsgInfo.RetValue = "corpId或者 agentSecret不能为空"; return exeMsgInfo; } WechatWorkClientOptions options = new WechatWorkClientOptions(); options.AgentId = agentId; options.AgentSecret = agentSecret; options.CorpId = corpId; try { var client = WechatWorkClientBuilder.Create(options).Build(); var request = new CgibinMessageSendRequest(); request.AccessToken = GetAppAccessToken(agentId,agentSecret,corpId); request.ToUserIdList = lstUserId; request.MessageType = "text"; request.MessageContentAsText = new CgibinAppChatSendRequest.Types.TextMessage() { Content = text }; request.AgentId = agentId; var response = client.CgibinMessageSendRequest(request); exeMsgInfo.RetStatus = response.Result.ErrorCode; exeMsgInfo.RetValue = "返回的结果是:" + response.Result.ToJson(); LogHelper.WriteLog($"返回的结果是:{exeMsgInfo.RetValue}"); } catch (Exception ex) { exeMsgInfo.RetValue = "发送异常" + ex.Message; } return exeMsgInfo; }
说明:
采用了 CgibinMessageSendRequest 这个类
步骤
1、创建client
2、采用 CgibinMessageSendRequest 创建请求,以及请求包含的参数
3、采用 client.CgibinMessageSendRequest 通用的请求发送
4、采用 Models.CgibinMessageSendResponse 标准消息发送 返回来获取返回值,并进行格式化输出