using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
public class DDMsgHelper
{
private static int _Agent_id = 123456; //填写创建的钉钉应用id号
private static string _CorpId = "11111111111"; //appkey
private static string _CorpSecret = "fbdvzvzvzvzvcvxzvcxvcxzvc"; //appsecret
private static string _Accesstoken = GetAccessToken();
public static string SendMsg(string userid, string content)
{
string MessageUrl = string.Format("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token={0}", _Accesstoken);
var par = new
{
agent_id = _Agent_id,
userid_list = userid,
msg = new
{
msgtype = "text",
text = new
{
content = content
}
}
};
string jsonRequest = JsonConvert.SerializeObject(par);
string res = HttpPost(MessageUrl, jsonRequest);
return res;
}
/// <summary>
/// 获取钉钉部门员工id
/// </summary>
/// <param name="accesstoken"></param>
/// <param name="agent_id"></param>
/// <param name="department_id"></param>
public static List<User> GetUsers(string department_id)
{
List<User> users = new List<User>();
var AccessToken = _Accesstoken;
//获取部门人员id
string MessageUrl = string.Format("https://oapi.dingtalk.com/user/simplelist?access_token={0}&department_id={1}&agent_id={2}&offset=0&size=100", AccessToken, department_id, _Agent_id);
Newtonsoft.Json.Linq.JObject jsonmsg = Newtonsoft.Json.Linq.JObject.Parse(HttpGet(MessageUrl));
if (jsonmsg.GetValue("userlist") != null)
{
var userlist = jsonmsg.GetValue("userlist");
foreach (var item in userlist)
{
User user = new User(item["name"].ToString(), item["userid"].ToString());
users.Add(user);
}
}
return users;
}
/// <summary>
/// 获取钉钉接口交互凭证
/// </summary>
/// <returns></returns>
private static string GetAccessToken()
{
string AccessUrl = string.Format("https://oapi.dingtalk.com/gettoken?corpid={0}&corpsecret={1}", _CorpId, _CorpSecret);
Newtonsoft.Json.Linq.JToken json = Newtonsoft.Json.Linq.JToken.Parse(HttpGet(AccessUrl));
string AccessToken = json["access_token"].ToString();
return AccessToken;
}
private static string HttpGet(string url)
{
//创建
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
//设置请求方法
httpWebRequest.Method = "GET";
//请求超时时间
httpWebRequest.Timeout = 20000;
//发送请求
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
//利用Stream流读取返回数据
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
//获得最终数据,一般是json
string responseContent = streamReader.ReadToEnd();
streamReader.Close();
httpWebResponse.Close();
return responseContent;
}
private static string HttpPost(string url, string data)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
//字符串转换为字节码
byte[] bs = Encoding.UTF8.GetBytes(data);
//参数类型,这里是json类型
httpWebRequest.ContentType = "application/json";
//参数数据长度
httpWebRequest.ContentLength = bs.Length;
//设置请求类型
httpWebRequest.Method = "POST";
//设置超时时间
httpWebRequest.Timeout = 20000;
//将参数写入请求地址中
httpWebRequest.GetRequestStream().Write(bs, 0, bs.Length);
//发送请求
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
//读取返回数据
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
string responseContent = streamReader.ReadToEnd();
streamReader.Close();
httpWebResponse.Close();
httpWebRequest.Abort();
return responseContent;
}
}
public class User
{
public User(string name, string id)
{
this.Name = name;
this.Id = id;
}
public string Name { get; set; }
public string Id { get; set; }
}