Browse Source

加入钉钉通知

master
С·æ 4 years ago
parent
commit
73abe56037
  1. 149
      JdShopListener/JdShopListener/DingApiHelper.cs
  2. 28
      JdShopListener/JdShopListener/MainWindow.xaml
  3. 73
      JdShopListener/JdShopListener/MainWindowViewModel.cs

149
JdShopListener/JdShopListener/DingApiHelper.cs

@ -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}&timestamp={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; }
}
}

28
JdShopListener/JdShopListener/MainWindow.xaml

@ -300,8 +300,32 @@
</TabItem>
<TabItem Header="店铺监控">
</TabItem>
<TabItem Header="通知配置">
<DockPanel>
<Button DockPanel.Dock="Bottom" HorizontalAlignment="Right" Height="30" Content="保存" Width="100" Margin="0 0 10 10" Cursor="Hand" Command="{Binding Btn_SaveDD}"></Button>
<StackPanel Margin="20 20 20 20" DockPanel.Dock="Top">
<DockPanel>
<TextBlock Text="钉钉Secret:" Width="120" VerticalAlignment="Center" Foreground="Black"></TextBlock>
<TextBox Text="{Binding DDSecret}"></TextBox>
</DockPanel>
<DockPanel Margin="0 10 0 0">
<TextBlock Text="Webhook地址:" Width="120" VerticalAlignment="Center" Foreground="Black"></TextBlock>
<TextBox Text="{Binding Webhook}"></TextBox>
</DockPanel>
<DockPanel Margin="0 30 0 0">
<CheckBox Content="是否启用" HorizontalAlignment="Right" IsChecked="{Binding IsUseDingDing}"></CheckBox>
</DockPanel>
</StackPanel>
</DockPanel>
</TabItem>
<TabItem Header="日志">

73
JdShopListener/JdShopListener/MainWindowViewModel.cs

@ -19,6 +19,8 @@ namespace JdShopListener
{
private static MainWindowViewModel _mainWindowViewModel = new MainWindowViewModel();
public MainWindow Window;
string ddFileName = $"{System.Environment.CurrentDirectory}\\ddset.ini";
public static MainWindowViewModel MainViewModel
{
get
@ -27,6 +29,8 @@ namespace JdShopListener
}
}
DingApiHelper dingApi=null;
public string JDCookie { get; set; }
public MainWindowViewModel() {
@ -48,11 +52,28 @@ namespace JdShopListener
Btn_ShowList = new RelayCommand(ShowList);
Btn_Start = new RelayCommand(Start);
Btn_ShowData = new RelayCommand(ShowData);
Btn_SaveDD = new RelayCommand(SaveDD);
ItemList = new System.Collections.ObjectModel.ObservableCollection<ItemData>();
SkuList = new System.Collections.ObjectModel.ObservableCollection<SkuModel>();
SelectDate = DateList.FirstOrDefault();
SelectPro = ProList.FirstOrDefault();
Init();
if (System.IO.File.Exists(ddFileName))
{
string json = System.IO.File.ReadAllText(ddFileName);
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
if (data != null)
{
DDSecret = data.Secret;
Webhook = data.Webhook;
IsUseDingDing = data.IsUse;
}
dingApi = new DingApiHelper(DDSecret, Webhook);
}
}
/// <summary>
@ -163,6 +184,31 @@ namespace JdShopListener
set { Set(ref _Desc, value); }
}
private string _DDSecret;
public string DDSecret
{
get { return _DDSecret; }
set { Set(ref _DDSecret, value); }
}
private string _Webhook;
public string Webhook
{
get { return _Webhook; }
set { Set(ref _Webhook, value); }
}
private bool _IsUseDingDing;
public bool IsUseDingDing
{
get { return _IsUseDingDing; }
set { Set(ref _IsUseDingDing, value); }
}
/// <summary>
/// 所有sku最后变化记录
/// </summary>
@ -183,6 +229,11 @@ namespace JdShopListener
/// </summary>
public RelayCommand Btn_Start { get; set; }
/// <summary>
/// 保存钉钉配置
/// </summary>
public RelayCommand Btn_SaveDD { get; set; }
/// <summary>
/// 刷新数据
/// </summary>
@ -636,6 +687,21 @@ namespace JdShopListener
}
}
/// <summary>
/// 保存钉钉配置
/// </summary>
public void SaveDD()
{
System.IO.File.WriteAllText(ddFileName, Newtonsoft.Json.JsonConvert.SerializeObject(new { Secret = this.DDSecret, Webhook = this.Webhook,IsUse=this.IsUseDingDing }));
dingApi = new DingApiHelper(DDSecret, Webhook);
MessageBox.Show("保存成功!", "提示");
}
/// <summary>
/// 根据Sku获取详情
/// </summary>
@ -844,10 +910,15 @@ namespace JdShopListener
DbHelper.Db.AddItemChangeModel(item);
if (IsUseDingDing)
{
dingApi.SendNotify($"{sku.SkuId}监控完成!已产生变化记录");
}
AddLog($"{sku.SkuId}监控完成!已产生变化记录");
}
else {
AddLog($"{sku.SkuId}监控完成!未发生变化");
AddLog($"{sku.SkuId}监控完成!未发生变化");
}
Thread.Sleep(3000);
}

Loading…
Cancel
Save