钉钉开发文档示例

钉钉接口文档百度地址

钉钉接口文档开放地址:

那么第一步我们先获取Token

/// <summary>
/// 获取企业授权的access_token
/// </summary>
/// <returns>access_token</returns>
private static string access_token()
{
try
{
string AccessTokenCorpid = "Corpid(20位字符可以在企业钉钉后台管理页面拿到)";
string AccessTokencorpsecret = "Corpsecret(在企业钉钉后台管理页面拿到)";
string Token = "https://oapi.dingtalk.com/gettoken?corpid=" + AccessTokenCorpid + "&corpsecret=" + AccessTokencorpsecret + "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Token);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
request.Method = "POST"; //上文说到的POST 提交方式
Stream stream = response.GetResponseStream();
Encoding encod = Encoding.UTF8;
StreamReader reader = new StreamReader(stream, encod);
string token = reader.ReadToEnd();
token = token.Replace("{"access_token":"", "").Substring(0, 32);
return token;
}
catch (Exception error)
{

}
return null;
}

第二部 在我们拿到token后 我们可以将token加入连接发送消息了

/// <summary>
/// </summary>
/// <param name="title">通知消息标题</param>
/// <param name="value">通知消息内容</param>
/// <param name="DingUser">通知人钉钉账号</param>
/// DDMessagerID()方法返回用户的钉钉 单需要一个参数 EmployeeCode
/// <returns></returns>
private static void AccessTokenDDMessager(string title, string content, string DDuserid)
{
try
{
//请勿轻易修改时间戳 不影响用户体验 时-分-秒-毫秒 格式 如果将多个公文流转至一个用户审批且通知内容相同 通知消息将无法发送 为此用此方法解决
title = title + "(" + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond.ToString().Substring(0, 3) + ")";
string postmessagerurl = "https://oapi.dingtalk.com/message/send?access_token=" + access_token();
StringBuilder PostMessager = new StringBuilder();
PostMessager.AppendLine("{");
PostMessager.AppendLine(""agentid": "37923093",");//消息发送者
PostMessager.AppendLine(""touser": "" + DDuserid + "",");//消息接受者
PostMessager.AppendLine(""msgtype":"oa",");//OA消息
PostMessager.AppendLine(""oa":");
PostMessager.AppendLine("{");
PostMessager.AppendLine(""message_url":"通知消息到达,用户点击消息链接地址",");//手机端消息链接
PostMessager.AppendLine(""pc_message_url":"通知消息到达,用户点击消息链接地址","); //电脑端消息链接
PostMessager.AppendLine(""head":{"bgcolor": "FFBBBBBB"},");//头部标题
PostMessager.AppendLine(""body":{"title": "" + title + "", "content": "" + content + "" }");//正文表题 正文内容
PostMessager.AppendLine(" }");
PostMessager.AppendLine("}");
WebClient webclient = new WebClient();
webclient.Headers.Add("Content-Type", "application/json");
string tostring = PostMessager.ToString();
byte[] messager = Encoding.UTF8.GetBytes(PostMessager.ToString());
byte[] result = webclient.UploadData(postmessagerurl, "POST", messager);
String resultString = Encoding.UTF8.GetString(result);//无法发送看此信息
}
catch (Exception error)
{
}
}

第三步为了快速的响应 我们创建一个线程池创建一个进程用来发送大的消息

/// <summary>
/// 这是一个线程池
/// 调用钉钉发送消息接口
/// 如果在web.config 拿不到钉钉Corpid消息将无法继续
/// </summary>
/// <param name="title">发送钉钉标题</param>
/// <param name="content">发送钉钉内容</param>
/// <param name="DDuserid">通知人钉钉</param>
public void threadDDmessager(string title, string content, string DDuserid)
{
try
{

//webconfig存入了Corpid 
string Corpid = System.Web.Configuration.WebConfigurationManager.AppSettings["Corpid"].Substring(0, 20);
if (Corpid.Equals("dingd6db9bf4ef046cb0"))//正确的企业钉钉Corpid
{
System.Threading.ThreadPool.QueueUserWorkItem(o =>
{
AccessTokenDDMessager(title, content, DDuserid);
});
}
}
catch (Exception error)
{

}
}

原文地址:https://www.cnblogs.com/rancrazy/p/6235835.html