zrg
2024-06-28 589d28da7824bee9b2d98e39f41c218bc8495109
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
 
namespace DingDingMsg
{
    public class MSG
    {
        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;
            }
        }
    }
}