3 changed files with 247 additions and 3 deletions
@ -0,0 +1,149 @@ |
|||
using Newtonsoft.Json; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Net.Http; |
|||
using System.Security.Cryptography; |
|||
using System.Text; |
|||
|
|||
namespace JdShopListener |
|||
{ |
|||
public class DingApiHelper |
|||
{ |
|||
/// <summary>
|
|||
/// 钉钉Secret
|
|||
/// </summary>
|
|||
private string dDSecret = "SEC23f4c5aa97b598b5a81b29ecb4b4facc5863e15e6b3c38c02f406c6541fdbb8e"; |
|||
|
|||
/// <summary>
|
|||
/// 钉钉的webHookUrl
|
|||
/// </summary>
|
|||
private string webHookUrl = "https://oapi.dingtalk.com/robot/send?access_token=ce194adc85ffc4438b894c20c895ba3e3eb2bfeea1cde4d0e9b4bd1122f905dc"; |
|||
|
|||
/// <summary>
|
|||
/// 初始化
|
|||
/// </summary>
|
|||
/// <param name="secret">钉钉Secret,机器人设置加签处获取</param>
|
|||
/// <param name="webHook">机器人设置完成的Webhook地址</param>
|
|||
public DingApiHelper(string secret, string webHook) |
|||
{ |
|||
dDSecret = secret; |
|||
webHookUrl = webHook; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 发送钉钉通知
|
|||
/// </summary>
|
|||
/// <param name="msg">内容</param>
|
|||
public bool SendNotify(string msg) |
|||
{ |
|||
try |
|||
{ |
|||
string timestamp = ((DateTime.Now.Ticks - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).Ticks) / 10000).ToString(); |
|||
|
|||
string stringToSign = $"{timestamp}\n{dDSecret}"; |
|||
|
|||
string hmac_code = GetHash(stringToSign, dDSecret); |
|||
|
|||
string sign = System.Web.HttpUtility.UrlEncode(hmac_code, Encoding.UTF8); |
|||
|
|||
//消息类型
|
|||
var msgtype = MsgTypeEnum.text.ToString(); |
|||
|
|||
//文本内容
|
|||
var text = new Text |
|||
{ |
|||
Content = msg |
|||
}; |
|||
|
|||
//指定目标人群
|
|||
var at = new At() |
|||
{ |
|||
AtMobiles = new List<string>() { }, |
|||
IsAtAll = false |
|||
}; |
|||
|
|||
|
|||
string url = $"{webHookUrl}&sign={sign}×tamp={timestamp}"; |
|||
using (HttpClient httpClient = new HttpClient()) |
|||
{ |
|||
HttpContent content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(new |
|||
{ |
|||
msgtype, |
|||
text, |
|||
at |
|||
})); |
|||
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); |
|||
HttpResponseMessage response = httpClient.PostAsync(url, content).Result; |
|||
string html = response.Content.ReadAsStringAsync().Result; |
|||
} |
|||
return true; |
|||
|
|||
} |
|||
catch |
|||
{ |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
|
|||
public String GetHash(String text, String key) |
|||
{ |
|||
// change according to your needs, an UTF8Encoding
|
|||
// could be more suitable in certain situations
|
|||
ASCIIEncoding encoding = new ASCIIEncoding(); |
|||
|
|||
Byte[] textBytes = encoding.GetBytes(text); |
|||
Byte[] keyBytes = encoding.GetBytes(key); |
|||
|
|||
Byte[] hashBytes; |
|||
|
|||
using (HMACSHA256 hash = new HMACSHA256(keyBytes)) |
|||
hashBytes = hash.ComputeHash(textBytes); |
|||
|
|||
return Convert.ToBase64String(hashBytes); |
|||
} |
|||
|
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 钉钉群机器人消息类型枚举
|
|||
/// </summary>
|
|||
public enum MsgTypeEnum |
|||
{ |
|||
text, |
|||
link, |
|||
markdown, |
|||
actionCard, |
|||
feedCard |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 文本类型
|
|||
/// </summary>
|
|||
public class Text |
|||
{ |
|||
/// <summary>
|
|||
/// 文本内容
|
|||
/// </summary>
|
|||
[JsonProperty(PropertyName = "content")] |
|||
public string Content { get; set; } |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// @指定人
|
|||
/// </summary>
|
|||
public class At |
|||
{ |
|||
/// <summary>
|
|||
/// @的联系人
|
|||
/// </summary>
|
|||
[JsonProperty(PropertyName = "atMobiles")] |
|||
public List<string> AtMobiles { set; get; } |
|||
|
|||
/// <summary>
|
|||
/// 是否@所有人
|
|||
/// </summary>
|
|||
[JsonProperty(PropertyName = "isAtAll")] |
|||
public bool IsAtAll { set; get; } |
|||
} |
|||
} |
Loading…
Reference in new issue