/// <summary> /// 发送Ding消息 /// </summary> /// <param name="recvs"></param> /// <param name="msgContent"></param> /// <returns></returns> public bool SendDingMsg(string recvs, string msgContent) { if (string.IsNullOrEmpty(recvs) || string.IsNullOrEmpty(msgContent)) { return(false); } IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request(); request.UseridList = recvs; request.AgentId = agentid; request.ToAllUser = false; OapiMessageCorpconversationAsyncsendV2Request.MsgDomain msg = new OapiMessageCorpconversationAsyncsendV2Request.MsgDomain(); msg.Msgtype = "text"; msg.Text = new OapiMessageCorpconversationAsyncsendV2Request.TextDomain(); msg.Text.Content = msgContent; request.Msg_ = msg; OapiMessageCorpconversationAsyncsendV2Response response = client.Execute(request, getDingAccessToken()); //logger.LogInformation(fastJSON.JSON.ToJSON(response)); return(response.IsError); }
/// <summary> /// 通知消息发送基础方法 /// </summary> /// <param name="accessToken"></param> /// <param name="agentId"></param> /// <param name="sendtoType"></param> /// <param name="userList"></param> /// <param name="_msg"></param> /// <returns></returns> private static SendWorkMessageResult SendMessage(string accessToken, long agentId, EnumClass.RecvType sendtoType, List <string> userList, OapiMessageCorpconversationAsyncsendV2Request.MsgDomain _msg) { SendWorkMessageResult result = new SendWorkMessageResult(); try { IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request(); request.AgentId = agentId; StringBuilder listStr = new StringBuilder(); foreach (string value in userList) { if (!string.IsNullOrEmpty(value)) { listStr.Append(value + ","); } } listStr.Remove(listStr.Length - 1, 1); switch (sendtoType) { case EnumClass.RecvType.UseridList: request.UseridList = listStr.ToString(); request.ToAllUser = false; break; case EnumClass.RecvType.DeptList: request.DeptIdList = listStr.ToString(); request.ToAllUser = false; break; case EnumClass.RecvType.AllUser: request.ToAllUser = true; break; } request.Msg_ = _msg; OapiMessageCorpconversationAsyncsendV2Response response = client.Execute(request, accessToken); if (response.Errcode == 0) { result = SendWorkMessageResult.CreateSuccessResult(response.Errcode, response.Errmsg, response.TaskId ); } else { result = SendWorkMessageResult.CreateFailResult(response.Errcode, response.Errmsg ); } return(result); } catch (Exception ex) { return(SendWorkMessageResult.CreateFailResult(ex.Message)); } }
public static OapiMessageCorpconversationAsyncsendV2Response PushMessage(string userIdList, bool?toAllUser, string detptIdList, MsgDomain msg_) { OapiGettokenResponse token = getToken(); IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request(); req.UseridList = userIdList; req.ToAllUser = toAllUser; req.DeptIdList = detptIdList; req.Msg_ = msg_; req.AgentId = AgentId; OapiMessageCorpconversationAsyncsendV2Response rsp = client.Execute(req, token.AccessToken); return(rsp); }
/// <summary> /// 发送工作通知消息 /// </summary> /// <param name="accessToken"></param> /// <param name="msg">消息内容,消息类型和样例参考“消息类型与数据格式”。最长不超过2048个字节</param> /// <param name="agentId">应用agentId</param> /// <param name="deptIds">接收者的部门id列表,最大列表长度:20, 接收者是部门id下(包括子部门下)的所有用户</param> /// <param name="userIds">接收者的用户userid列表,最大列表长度:100</param> /// <param name="toAllUser">是否发送给企业全部用户</param> /// <returns></returns> public OapiMessageCorpconversationAsyncsendV2Response Send(string accessToken, MsgDomain msg, long agentId, string deptIds = "", string userIds = "", bool toAllUser = false) { IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request(); request.UseridList = userIds; request.AgentId = agentId; request.ToAllUser = toAllUser; request.DeptIdList = deptIds; request.Msg_ = msg; OapiMessageCorpconversationAsyncsendV2Response response = client.Execute(request, accessToken); return(response); }
private void AddMsg(OapiMessageCorpconversationAsyncsendV2Request.MsgDomain msg) { if (!_isQueue) { if (_list != null && _list.Count > 0) { return; } } ////实例化请求 var request = new OapiMessageCorpconversationAsyncsendV2Request(); ////接受消息人员 request.UseridList = _userList; ////微应用凭证 request.AgentId = base.AgentId; ////是否发送给企业所有人 request.ToAllUser = false; //消息内容 request.Msg_ = msg; _list.Add(request); }
//private void DingTalkOAuth(string accessToken) //{ // IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get"); // OapiUserGetRequest req = new OapiUserGetRequest(); // req.Userid = "userid1"; // req.SetHttpMethod("GET"); // OapiUserGetResponse rsp = client.Execute(req, accessToken); //} /// <summary> /// 发送工作通知 /// </summary> /// <param name="userList"></param> /// <param name="content"></param> /// <returns></returns> public static bool sentMessageToUser(string userList, string content) { DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request(); request.UseridList = userList; request.AgentId = Convert.ToInt64(agentid); request.ToAllUser = false; request.SetHttpMethod("POST"); OapiMessageCorpconversationAsyncsendV2Request.MsgDomain msg = new OapiMessageCorpconversationAsyncsendV2Request.MsgDomain(); msg.Msgtype = "text"; msg.Text = new OapiMessageCorpconversationAsyncsendV2Request.TextDomain(); msg.Text.Content = content; request.Msg_ = msg; string accessToken = GetDingTalkAccessToken().AccessToken; OapiMessageCorpconversationAsyncsendV2Response response = client.Execute(request, accessToken); return(!response.IsError); //return getSentMessageResult(response.TaskId); }
/// <summary> /// 钉钉发送OA模板消息 /// </summary> /// <param name="agentid">微应用id</param> /// <param name="userlist">推送指定人员多个用,分隔</param> /// <param name="accessToken">token</param> /// <param name="content"></param> /// <param name="keys">OA内容body字典集合</param> /// <returns></returns> public string Message(long agentid, string userlist, string accessToken, string content, Dictionary <string, string> keys) { IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request(); request.UseridList = userlist; request.AgentId = agentid; request.ToAllUser = false; OapiMessageCorpconversationAsyncsendV2Request.MsgDomain msg = new OapiMessageCorpconversationAsyncsendV2Request.MsgDomain(); msg.Msgtype = "oa"; var domains = new List <OapiMessageCorpconversationAsyncsendV2Request.FormDomain>(); //body foreach (var item in keys) { domains.Add(new OapiMessageCorpconversationAsyncsendV2Request.FormDomain { Key = item.Key, Value = item.Value }); } //头部默认值 msg.Oa = new OapiMessageCorpconversationAsyncsendV2Request.OADomain { MessageUrl = "www.baidu.com", Head = new OapiMessageCorpconversationAsyncsendV2Request.HeadDomain(), Body = new OapiMessageCorpconversationAsyncsendV2Request.BodyDomain { Form = domains } }; request.Msg_ = msg; OapiMessageCorpconversationAsyncsendV2Response response = client.Execute(request, accessToken); return(response.Body); }
/// <summary> /// 发送工作消息公共函数 /// </summary> /// <param name="url"></param> /// <param name="userid">多用户逗号分隔</param> /// <param name="msg"></param> /// <returns></returns> public JObject SendWorkMsg(string userid, MsgDomain msg) { try { var url = $"{m_host}/topapi/message/corpconversation/asyncsend_v2"; var client = new DefaultDingTalkClient(url); var request = new OapiMessageCorpconversationAsyncsendV2Request(); request.AgentId = m_agentId; request.UseridList = userid; request.Msg_ = msg; var response = client.Execute(request, access_token); var _json = JObject.Parse(response.Body); //if (Convert.ToInt32(_json["errcode"].ToString()) != 0) //{ // //LogService.AppendDebugLog(typeof(DingTalkService), $"请求地址[{url}]返回{_json.ToString()}"); //} return(_json); } catch (Exception ex) { //this.AppendErrorLog(ex.Message, ex); } return(null); }
/// <summary> /// 发送消息 /// </summary> /// <param name="accessToken"></param> /// <returns></returns> public static string SendMessage(string agentId, string accessToken, string title, string body, string user) { try { IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request(); request.UseridList = user;// GetUserIdByPhone("15918514353", accessToken); request.AgentId = long.Parse(agentId); request.ToAllUser = false; OapiMessageCorpconversationAsyncsendV2Request.MsgDomain msg = new OapiMessageCorpconversationAsyncsendV2Request.MsgDomain(); //msg.Msgtype = "text"; //msg.Text = new OapiMessageCorpconversationAsyncsendV2Request.TextDomain(); //msg.Text.Content = "test123"; //request.Msg_ = msg; //msg.Msgtype = "image"; //msg.Image = (new OapiMessageCorpconversationAsyncsendV2Request.ImageDomain()); //msg.Image.MediaId = "@lADOdvRYes0CbM0CbA"; //request.Msg_ = msg; //msg.Msgtype = "file"; //msg.File = (new OapiMessageCorpconversationAsyncsendV2Request.FileDomain()); //msg.File.MediaId = "@lADOdvRYes0CbM0CbA"; //request.Msg_ = msg; //msg.Msgtype = "link"; //msg.Link = (new OapiMessageCorpconversationAsyncsendV2Request.LinkDomain()); //msg.Link.Title = "test"; //msg.Link.Text = "test"; //msg.Link.MessageUrl = "https://www.baidu.com"; //msg.Link.PicUrl = "https://www.baidu.com"; //request.Msg_ = msg; //msg.Msgtype = "markdown"; //msg.Markdown = (new OapiMessageCorpconversationAsyncsendV2Request.MarkdownDomain()); //msg.Markdown.Text = "##### text"; //msg.Markdown.Title = "### Title"; //request.Msg_ = msg; //msg.Oa = (new OapiMessageCorpconversationAsyncsendV2Request.OADomain()); //msg.Oa.Head = (new OapiMessageCorpconversationAsyncsendV2Request.HeadDomain()); //msg.Oa.Head.Text = title; //msg.Oa.Body = (new OapiMessageCorpconversationAsyncsendV2Request.BodyDomain()); //msg.Oa.Body.Content = body; //msg.Oa.MessageUrl = "http://www.fxlweb.com/Web2018/DingLogin.aspx"; //msg.Oa.PcMessageUrl = "http://www.fxlweb.com/Web2018/DingLogin.aspx"; //msg.Msgtype = "oa"; //request.Msg_ = msg; msg.ActionCard = (new OapiMessageCorpconversationAsyncsendV2Request.ActionCardDomain()); msg.ActionCard.Title = title; msg.ActionCard.Markdown = body; msg.ActionCard.SingleTitle = "访问网站"; msg.ActionCard.SingleUrl = "http://www.fxlweb.com/Web2018/DingLogin.aspx"; msg.Msgtype = "action_card"; request.Msg_ = msg; OapiMessageCorpconversationAsyncsendV2Response response = client.Execute(request, accessToken); log.Info("SendMessage: " + response.Body); return(response.Body); } catch (Exception ex) { log.Info(ex); return(""); } }
static void Main(string[] args) { //获取到AccessToken获取AccessToken需要Appkey和Appsecret,请求是GET DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken"); OapiGettokenRequest request = new OapiGettokenRequest(); request.Appkey = appKey; //Appkey request.Appsecret = appSecret; //Appsecret request.SetHttpMethod("GET"); OapiGettokenResponse response = client.Execute(request); //获取到AccessToken accessToken = response.AccessToken; // 创建员工 DefaultDingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/create"); OapiUserCreateRequest request1 = new OapiUserCreateRequest(); request1.Userid = "zhangsan"; request1.Mobile = "16657119236"; request1.Email = "*****@*****.**"; request1.Name = "张三"; List <long> departments1 = new List <long>(); departments1.Add(1L); request1.Department = JSON.ToJSON(departments1); OapiUserCreateResponse response1 = client1.Execute(request1, accessToken); Console.WriteLine("创建员工:"); Console.WriteLine(response1.Body); Console.WriteLine(); // 删除员工 DefaultDingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/delete"); OapiUserDeleteRequest request2 = new OapiUserDeleteRequest(); request2.Userid = "zhangsan"; request2.SetHttpMethod("GET"); OapiUserDeleteResponse response2 = client2.Execute(request2, accessToken); Console.WriteLine("删除员工:"); Console.WriteLine(response2.Body); Console.WriteLine(); // 获取用户信息 DefaultDingTalkClient client3 = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get"); OapiUserGetRequest request3 = new OapiUserGetRequest(); request3.Userid = userId; request3.SetHttpMethod("GET"); OapiUserGetResponse response3 = client3.Execute(request3, accessToken); Console.WriteLine("获取用户信息:"); Console.WriteLine(response3.Body); Console.WriteLine(); // 获取管理员信息 DefaultDingTalkClient client4 = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_admin"); OapiUserGetAdminRequest request4 = new OapiUserGetAdminRequest(); request4.SetHttpMethod("GET"); OapiUserGetAdminResponse response4 = client4.Execute(request4, accessToken); Console.WriteLine("获取管理员信息:"); Console.WriteLine(response4.Body); Console.WriteLine(); // 创建角色 DefaultDingTalkClient client5 = new DefaultDingTalkClient("https://oapi.dingtalk.com/role/add_role"); OapiRoleAddRoleRequest request5 = new OapiRoleAddRoleRequest(); request5.RoleName = "Test"; request5.GroupId = groupId; OapiRoleAddRoleResponse response5 = client5.Execute(request5, accessToken); Console.WriteLine("创建角色:"); Console.WriteLine(response5.Body); Console.WriteLine(); //更新角色,有bug,提示需要修改为GET,修改后依然报错称缺少CorId和APPkey,但是新版本小程序已经没有CorId了。 DefaultDingTalkClient client6 = new DefaultDingTalkClient("https://oapi.dingtalk.com/role/update_role"); OapiRoleUpdateRoleRequest request6 = new OapiRoleUpdateRoleRequest(); request6.RoleName = "AnyThing"; request6.RoleId = 1; request6.SetHttpMethod("GET"); OapiRoleUpdateRoleResponse response6 = client.Execute(request6, accessToken); Console.WriteLine("更新角色"); Console.WriteLine(response6.Body); Console.WriteLine(); // 删除角色 DefaultDingTalkClient client7 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/deleterole"); OapiRoleDeleteroleRequest request7 = new OapiRoleDeleteroleRequest(); request7.RoleId = response5.RoleId; OapiRoleDeleteroleResponse response7 = client7.Execute(request7, accessToken); Console.WriteLine("删除角色:"); Console.WriteLine(response7.Body); Console.WriteLine(); // 添加外部联系人 官方代码有问题(已解决,需要在OpenExtContactDomain前加上request名 // 然而又有新问题:还是和上面“更新角色”的报错一样,提示:缺少参数 corpid or appkey DefaultDingTalkClient client8 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/extcontact/create"); OapiExtcontactCreateRequest request8 = new OapiExtcontactCreateRequest(); OapiExtcontactCreateRequest.OpenExtContactDomain contacter = new OapiExtcontactCreateRequest.OpenExtContactDomain { Title = "CFO", Name = "测试的外部联系人", StateCode = "86", CompanyName = "钉钉", Mobile = "15295778118" }; request8.Contact_ = contacter; OapiExtcontactCreateResponse response8 = client8.Execute(request8, accessToken); Console.WriteLine("添加外部联系人:"); Console.WriteLine(response6.Body); Console.WriteLine(); // 获取外部联系人列表 DefaultDingTalkClient client9 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/extcontact/list"); OapiExtcontactListRequest request9 = new OapiExtcontactListRequest(); request9.Size = 20L; request9.Offset = 0L; OapiExtcontactListResponse response9 = client9.Execute(request9, accessToken); Console.WriteLine("获取外部联系人列表:"); Console.WriteLine(response9.Body); Console.WriteLine(); // 创建群会话 //DefaultDingTalkClient client10 = new DefaultDingTalkClient("https://oapi.dingtalk.com/chat/create"); //OapiChatCreateRequest request10 = new OapiChatCreateRequest(); //request10.Name = "TestCreate"; //request10.Owner = userId; //request10.Useridlist ??= new List<string>(); //request10.Useridlist.Add(userId); //OapiChatCreateResponse response10 = client10.Execute(request10, accessToken); //Console.WriteLine("创建群会话"); //Console.WriteLine(response10.Body); //Console.WriteLine(); //// 获取群会话 //DefaultDingTalkClient client11 = new DefaultDingTalkClient("https://oapi.dingtalk.com/chat/get"); //OapiChatGetRequest request11 = new OapiChatGetRequest(); //request11.Chatid = response10.Chatid; //request11.SetHttpMethod("GET"); //OapiChatGetResponse response11 = client11.Execute(request11, accessToken); //Console.WriteLine("获取群会话"); //Console.WriteLine(response11.Body); //Console.WriteLine(); //// 更新群对话 //DefaultDingTalkClient client12 = new DefaultDingTalkClient("https://oapi.dingtalk.com/chat/update"); //OapiChatUpdateRequest request12 = new OapiChatUpdateRequest(); //request12.Chatid = response10.Chatid; //request12.Name = "TestUpdate"; //OapiChatUpdateResponse response12 = client12.Execute(request12, accessToken); //Console.WriteLine("更新群对话"); //Console.WriteLine(response12.Body); //Console.WriteLine(); // 发送工作通知 DefaultDingTalkClient client13 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); OapiMessageCorpconversationAsyncsendV2Request request13 = new OapiMessageCorpconversationAsyncsendV2Request(); request13.AgentId = agentId; request13.ToAllUser = true; OapiMessageCorpconversationAsyncsendV2Request.MsgDomain msgDomain = new OapiMessageCorpconversationAsyncsendV2Request.MsgDomain(); msgDomain.Msgtype = "oa"; msgDomain.Oa = new OapiMessageCorpconversationAsyncsendV2Request.OADomain(); msgDomain.Oa.MessageUrl = "http://dingtalk.com"; msgDomain.Oa.Head = new OapiMessageCorpconversationAsyncsendV2Request.HeadDomain(); msgDomain.Oa.Head.Bgcolor = "FFBBBBBB"; msgDomain.Oa.Head.Text = "头部标题"; msgDomain.Oa.Body = new OapiMessageCorpconversationAsyncsendV2Request.BodyDomain(); msgDomain.Oa.Body.Title = "正文标题"; msgDomain.Oa.Body.Form = new List <OapiMessageCorpconversationAsyncsendV2Request.FormDomain>(); msgDomain.Oa.Body.Form.Add(new OapiMessageCorpconversationAsyncsendV2Request.FormDomain() { Key = "姓名", Value = "张三" }); msgDomain.Oa.Body.Form.Add(new OapiMessageCorpconversationAsyncsendV2Request.FormDomain() { Key = "年龄", Value = "20" }); msgDomain.Oa.Body.Form.Add(new OapiMessageCorpconversationAsyncsendV2Request.FormDomain() { Key = "身高", Value = "1.8米" }); msgDomain.Oa.Body.Form.Add(new OapiMessageCorpconversationAsyncsendV2Request.FormDomain() { Key = "体重", Value = "130斤" }); msgDomain.Oa.Body.Form.Add(new OapiMessageCorpconversationAsyncsendV2Request.FormDomain() { Key = "学历", Value = "本科" }); msgDomain.Oa.Body.Form.Add(new OapiMessageCorpconversationAsyncsendV2Request.FormDomain() { Key = "爱好", Value = "打球、听音乐" }); msgDomain.Oa.Body.Rich = new OapiMessageCorpconversationAsyncsendV2Request.RichDomain(); msgDomain.Oa.Body.Rich.Num = "15.6"; msgDomain.Oa.Body.Rich.Unit = "元"; msgDomain.Oa.Body.Content = "大段文本大段文本大段文本大段文本大段文本"; msgDomain.Oa.Body.Image = "@lADOADmaWMzazQKA"; msgDomain.Oa.Body.FileCount = "3"; msgDomain.Oa.Body.Author = "李四"; request13.Msg_ = msgDomain; //request13.Msg = @"{ // ""msgtype"": ""oa"", // ""oa"": { // ""message_url"": ""http://dingtalk.com"", // ""head"": { // ""bgcolor"": ""FFBBBBBB"", // ""text"": ""头部标题"" // }, // ""body"": { // ""title"": ""正文标题"", // ""form"": [ // {""key"": ""姓名:"", ""value"": ""张三""}, // {""key"": ""年龄:"", ""value"": ""20""}, // {""key"": ""身高:"", ""value"": ""1.8米""}, // {""key"": ""体重:"", ""value"": ""130斤""}, // {""key"": ""学历:"", ""value"": ""本科""}, // {""key"": ""爱好:"", ""value"": ""打球、听音乐""} // ], // ""rich"": {""num"": ""15.6"", ""unit"": ""元""}, // ""content"": ""大段文本大段文本大段文本大段文本大段文本大段文本"", // ""image"": ""@lADOADmaWMzazQKA"", // ""file_count"": ""3"", // ""author"": ""李四"" // } // } //}"; OapiMessageCorpconversationAsyncsendV2Response response13 = client13.Execute(request13, accessToken); Console.WriteLine("发送工作通知:"); Console.WriteLine(response13.Body); Console.WriteLine(); //获取工作通知消息的发送进度 DefaultDingTalkClient client14 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/getsendprogress"); OapiMessageCorpconversationGetsendprogressRequest request14 = new OapiMessageCorpconversationGetsendprogressRequest(); request14.AgentId = agentId; request14.TaskId = response13.TaskId; OapiMessageCorpconversationGetsendprogressResponse response14 = client14.Execute(request14, accessToken); Console.WriteLine("获取工作通知消息的发送进度"); Console.WriteLine(response14.Body); Console.WriteLine(); //获取工作通知消息的发送结果 DefaultDingTalkClient client15 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/getsendresult"); OapiMessageCorpconversationGetsendresultRequest request15 = new OapiMessageCorpconversationGetsendresultRequest(); request15.AgentId = agentId; request15.TaskId = response13.TaskId; OapiMessageCorpconversationGetsendresultResponse response15 = client15.Execute(request15, accessToken); Console.WriteLine("获取工作通知消息的发送结果"); Console.WriteLine(response15.Body); Console.WriteLine(); ////发送消息到企业群(使用时需将client10注释恢复) //DefaultDingTalkClient client16 = new DefaultDingTalkClient("https://oapi.dingtalk.com/chat/send"); //OapiChatSendRequest request16 = new OapiChatSendRequest(); //request16.Chatid = response10.Chatid; //request16.Msg = @"{ // ""msgtype"":""text"", // ""text"": { // ""content"": ""测试发送消息到企业群: 还在coding吗兄弟?"", // } //}"; //OapiChatSendResponse response16 = client16.Execute(request16, accessToken); //Console.WriteLine("发送消息到企业群"); //Console.WriteLine(response16.Body); //Console.WriteLine(); //发起代办 IDingTalkClient client17 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/workrecord/add"); OapiWorkrecordAddRequest request17 = new OapiWorkrecordAddRequest(); request17.Userid = userId; request17.CreateTime = 1496678400000L; request17.Title = "title"; request17.Url = "https://oa.dingtalk.com"; List <OapiWorkrecordAddRequest.FormItemVoDomain> formItemList = new List <OapiWorkrecordAddRequest.FormItemVoDomain>(); OapiWorkrecordAddRequest.FormItemVoDomain formItem = new OapiWorkrecordAddRequest.FormItemVoDomain(); formItemList.Add(formItem); formItem.Title = "标题"; formItem.Content = "内容 "; request17.FormItemList_ = formItemList; request17.BizId = "TestRecord"; OapiWorkrecordAddResponse response17 = client17.Execute(request17, accessToken); Console.WriteLine("发起代办"); Console.WriteLine(response17.Body); Console.WriteLine(); //更新代办 IDingTalkClient client18 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/workrecord/update"); OapiWorkrecordUpdateRequest request18 = new OapiWorkrecordUpdateRequest(); request18.Userid = userId; request18.RecordId = "TestRecord"; request18.SetHttpMethod("GET"); OapiWorkrecordUpdateResponse response18 = client.Execute(request18, accessToken); Console.WriteLine("更新代办"); Console.WriteLine(response18.Body); Console.WriteLine(); ////根据部门获取到Urid //DefaultDingTalkClient clie = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getDeptMember"); //OapiUserGetDeptMemberRequest req = new OapiUserGetDeptMemberRequest(); //req.DeptId = "1"; //req.SetHttpMethod("GET"); //OapiUserGetDeptMemberResponse rsp = clie.Execute(req, AccessToken); //List<string> userid = rsp.UserIds; ////获取到Urid就是在公司里要发送到那个人的id //string Urid = userid[0]; ////发送消息 //IDingTalkClient cl = new DefaultDingTalkClient("https://eco.taobao.com/router/rest"); //CorpMessageCorpconversationAsyncsendRequest req1 = new CorpMessageCorpconversationAsyncsendRequest(); //req1.Msgtype = "oa";//发送消息是以oa的形式发送的,其他的还有text,image等形式 //req1.AgentId = 917416506;//微应用ID //req1.UseridList = Urid;//收信息的userId,这个是by公司来区分,在该公司内这是一个唯一标识符 //req1.ToAllUser = false;//是否发给所有人 // //消息文本 ////req1.Msgcontent = "{\"message_url\": \"http://dingtalk.com\",\"head\": {\"bgcolor\": \"FFBBBBBB\",\"text\": \"头部标题\"},\"body\": {\"title\": \"拿钱学习\",\"form\": [{\"key\": \"姓名:\", \"value\": \"hong\" },{\"key\": \"年龄:\", \"value\": \"18\" },{\"key\": \"身高:\", \"value\": \"1.6米\"},{\"key\": \"体重:\",\"value\": \"90斤\"},{\"key\": \"学历:\",\"value\": \"硕士\"},{\"key\": \"爱好:\",\"value\": \"学习\"}],\"rich\": {\"num\": \"10000\",\"unit\": \"元\"},\"content\": \"快去学习!!!\",\"file_count\": \"1\",\"author\": \"小白\"}}"; ////CorpMessageCorpconversationAsyncsendResponse rsp1 = cl.Execute(req1, AccessToken);//发送消息 //req1.Msgcontent = @"{ // ""message_url"": ""http://dingtalk.com"", // ""head"": {""bgcolor"": ""FFBBBBBB"",""text"": ""头部标题""}, // ""body"": { // ""title"": ""拿钱学习"", // ""form"": [ // {""key"": ""姓名:"", ""value"": ""hong"" }, // {""key"": ""年龄:"", ""value"": ""18"" }, // {""key"": ""身高:"", ""value"": ""1.6米""}, // {""key"": ""体重:"", ""value"": ""90斤""}, // {""key"": ""学历:"", ""value"": ""硕士""}, // {""key"": ""爱好:"", ""value"": ""学习""}], // ""rich"": {""num"": ""10000"",""unit"": ""元""}, // ""content"": ""快去学习!!!"", // ""file_count"": ""1"", // ""author"": ""小白"" // } //}"; //CorpMessageCorpconversationAsyncsendResponse rsp1 = cl.Execute(req1, AccessToken);//发送消息 //Console.WriteLine(rsp1.Body); Console.ReadLine(); }
//// /** * 同一个微应用相同消息内容同一个用户一天只能接收一次,重复发送会发送成功但用户接收不到。 * 该接口是异步发送消息,接口返回成功并不表示用户收到消息,需要通过“查询工作通知消息的发送结果”接口查询是否给用户发送成功。 */ /// <summary> /// 发送工作消息 /// </summary> /// <param name="parameters"></param> /// <returns></returns> public static ResponseParameters SendWorkNotificationMessage(RequestParameters parameters) { try { log.Info("发送工作消息"); DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request(); log.Info("UseridList:" + parameters.Userid_list + ";AgentId:" + parameters.Agent_id + ";ToAllUser:"******"text"; msg.Text = text; request.Msg_ = msg; log.Info("发送Text消息:" + parameters.Message); break; case SendMsgType.image: OapiMessageCorpconversationAsyncsendV2Request.ImageDomain image = new OapiMessageCorpconversationAsyncsendV2Request.ImageDomain(); image.MediaId = parameters.MediaId; msg.Msgtype = "image"; msg.Image = image; request.Msg_ = msg; log.Info("发送image:" + parameters.MediaId); break; case SendMsgType.file: OapiMessageCorpconversationAsyncsendV2Request.FileDomain file = new OapiMessageCorpconversationAsyncsendV2Request.FileDomain(); file.MediaId = parameters.MediaId; msg.Msgtype = "file"; msg.File = file; request.Msg_ = msg; log.Info("发送file:" + parameters.MediaId); break; case SendMsgType.link: OapiMessageCorpconversationAsyncsendV2Request.LinkDomain link = new OapiMessageCorpconversationAsyncsendV2Request.LinkDomain(); link.Title = parameters.Title; link.Text = parameters.Text; link.MessageUrl = parameters.MessageUrl; link.PicUrl = parameters.PicUrl; msg.Link = link; msg.Msgtype = "link"; request.Msg_ = msg; log.Info("发送link:Title" + parameters.Title); break; case SendMsgType.markdown: OapiMessageCorpconversationAsyncsendV2Request.MarkdownDomain markdown = new OapiMessageCorpconversationAsyncsendV2Request.MarkdownDomain(); markdown.Text = parameters.Text; markdown.Title = parameters.Title; msg.Msgtype = "markdown"; msg.Markdown = markdown; request.Msg_ = msg; break; case SendMsgType.action_card: OapiMessageCorpconversationAsyncsendV2Request.ActionCardDomain actionCard = new OapiMessageCorpconversationAsyncsendV2Request.ActionCardDomain(); actionCard.Title = parameters.Title; actionCard.Markdown = parameters.Markdown; actionCard.SingleTitle = parameters.SingleTitle; actionCard.SingleUrl = parameters.SingleUrl; msg.ActionCard = actionCard; msg.Msgtype = "action_card"; request.Msg_ = msg; break; case SendMsgType.oa: OapiMessageCorpconversationAsyncsendV2Request.OADomain oA = new OapiMessageCorpconversationAsyncsendV2Request.OADomain(); OapiMessageCorpconversationAsyncsendV2Request.HeadDomain head = new OapiMessageCorpconversationAsyncsendV2Request.HeadDomain(); OapiMessageCorpconversationAsyncsendV2Request.BodyDomain body = new OapiMessageCorpconversationAsyncsendV2Request.BodyDomain(); head.Text = parameters.Text; body.Content = parameters.Message; oA.Head = head; oA.Body = body; msg.Oa = oA; msg.Msgtype = "oa"; request.Msg_ = msg; break; default: break; } OapiMessageCorpconversationAsyncsendV2Response response = client.Execute(request, Get_Access_Token()); log.Info("请求完成" + response.ToString()); return(new ResponseParameters() { ErroCode = response.Errcode, ErroMsg = response.Errmsg, TaskId = response.TaskId }); } catch (Exception ex) { return(new ResponseParameters() { ErroCode = -1, ErroMsg = ex.Message }); } }