using System; 
 | 
using System.IO; 
 | 
using System.Net; 
 | 
using System.Net.Http; 
 | 
using System.Text; 
 | 
using System.Threading.Tasks; 
 | 
  
 | 
namespace DLL 
 | 
{ 
 | 
    public class Cls_DDMsg 
 | 
    { 
 | 
        private HttpClient _client; 
 | 
        private string _baseUrl; 
 | 
  
 | 
        //获取企业的accessToken 
 | 
        public string GetAccessToken(string appKey, string appSecret) 
 | 
        { 
 | 
            string url = "https://api.dingtalk.com/v1.0/oauth2/accessToken"; 
 | 
  
 | 
            string postData = "{\"appKey\":\"" + appKey + "\", \"appSecret\":\"" + appSecret + "\"}"; 
 | 
  
 | 
            byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
 | 
  
 | 
            WebRequest request = WebRequest.Create(url); 
 | 
            request.Method = "POST"; 
 | 
            request.ContentType = "application/json"; 
 | 
            request.ContentLength = byteArray.Length; 
 | 
  
 | 
            using (Stream dataStream = request.GetRequestStream()) 
 | 
            { 
 | 
                dataStream.Write(byteArray, 0, byteArray.Length); 
 | 
            } 
 | 
  
 | 
            using (WebResponse response = request.GetResponse()) 
 | 
            using (Stream responseStream = response.GetResponseStream()) 
 | 
            using (StreamReader reader = new StreamReader(responseStream)) 
 | 
            { 
 | 
                string responseString = reader.ReadToEnd(); 
 | 
                return responseString; 
 | 
            } 
 | 
        } 
 | 
  
 | 
        //发送工作通知       
 | 
        public async Task<string> SendTextMessage(string accessToken, string agentId, string userIdList, string deptIdList, string toAllUser, string message) 
 | 
        { 
 | 
            try 
 | 
            { 
 | 
                _client = new HttpClient(); 
 | 
                _baseUrl = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"; 
 | 
                string url = $"{_baseUrl}?access_token={accessToken}"; 
 | 
                DateTimeOffset now = DateTimeOffset.Now; 
 | 
                string currentTime = now.ToString("yyyy-MM-dd HH:mm:ss"); 
 | 
                string messageWithTimestamp = $"{currentTime}-{message}"; // 在消息内容之前添加时间戳 
 | 
                string messageJson = $"{{\"msgtype\": \"text\", \"text\": {{ \"content\": \"{messageWithTimestamp}\" }} }}"; 
 | 
                string requestBody = $"agent_id={agentId}&userid_list={userIdList}&dept_id_list={deptIdList}&to_all_user={toAllUser}&msg={messageJson}"; 
 | 
  
 | 
                HttpResponseMessage response = await _client.PostAsync(url, new StringContent(requestBody, Encoding.UTF8, "application/x-www-form-urlencoded")); 
 | 
  
 | 
                if (response.IsSuccessStatusCode) 
 | 
                { 
 | 
                    return await response.Content.ReadAsStringAsync(); 
 | 
                } 
 | 
                else 
 | 
                { 
 | 
                    return $"Failed to send message. Status code: {response.StatusCode}"; 
 | 
                } 
 | 
            } 
 | 
            catch (Exception ex) 
 | 
            { 
 | 
                return "An error occurred: " + ex.Message; 
 | 
            } 
 | 
        } 
 | 
  
 | 
    } 
 | 
} 
 |