chengsc
2025-05-07 48df8080d7befac9fff4f5345750699c592512f6
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Talk.Extensions;
 
namespace Utility.Tools
{
    /// <summary>
    /// 企业微信
    /// </summary>
    public class WeComTools
    {
        private static string access_token = "";
        public WeComTools()
        {
            access_token = GetToken();
        }
 
        public string GetToken()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("corpid", WeComConfig.corpid);
            dic.Add("corpsecret", WeComConfig.corpsecret);
            var response = HttpHelper.DoGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken", dic);
            var modResponse = JsonConvert.DeserializeObject<TokenResponse>(response);
            if (modResponse.errcode == 0)
            {
                return modResponse.access_token;
            }
            else
            {
                throw Oops.Bah(modResponse.errmsg);
            }
        }
        public bool SendNewsMessage(string title, string content, string touser = "@all")
        {
            MessageRequest request = new MessageRequest()
            {
                touser = touser,
                agentid = WeComConfig.agentid,
                msgtype = "mpnews",
                mpnews = new Mpnews()
                {
                    articles = new List<ArticlesItem>()
                    {
                        new ArticlesItem()
                        {
                            title = title,
                            thumb_media_id = WeComConfig.thumb_media_id,
                            content = content
                        }
                    }
                }
            };
            var response = HttpHelper.DoPost("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+ access_token, JsonConvert.SerializeObject(request), "WeCom", "WMS");
            var modResponse = JsonConvert.DeserializeObject<TokenResponse>(response);
            if (modResponse.errcode == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool SendTextMessage(string content, string touser = "@all")
        {
            MessageRequest request = new MessageRequest()
            {
                touser = touser,
                agentid = WeComConfig.agentid,
                msgtype = "text",
                text = new Text() { content = content }
            };
            var response = HttpHelper.DoPost("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+ access_token, JsonConvert.SerializeObject(request), "WeCom", "WMS");
            var modResponse = JsonConvert.DeserializeObject<TokenResponse>(response);
            if (modResponse.errcode == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
 
    public class WeComConfig
    {
        public static string corpid { get; set; }
        public static int agentid { get; set; }
        public static string corpsecret { get; set; }
        public static string thumb_media_id { get; set; }
    }
    public class TokenResponse
    {
        /// <summary>
        /// 
        /// </summary>
        public int errcode { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string errmsg { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string access_token { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int expires_in { get; set; }
    }
 
    public class ArticlesItem
    {
        /// <summary>
        /// 标题
        /// </summary>
        public string title { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string thumb_media_id { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string content { get; set; }
    }
 
    public class Mpnews
    {
        /// <summary>
        /// 
        /// </summary>
        public List<ArticlesItem> articles { get; set; }
    }
    public class Text
    {
        public string content { get; set; }
    }
    public class MessageRequest
    {
        /// <summary>
        /// 
        /// </summary>
        public string touser { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string msgtype { get; set; } = "mpnews";
        /// <summary>
        /// 
        /// </summary>
        public int agentid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Mpnews mpnews { get; set; }
        public Text text { get; set; }
    }
 
}