using CsharpHttpHelper; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Server.Controllers.FunctionSetting { public class nouse { public void nouse() { try { var http = new HttpHelper(); var item = http.GetItem(_info.api_location); item.Method = "post"; item.ContentType = "application/json;";//返回类型 可选项有默认值 switch (_info.notice_apitype) { case NoticeApiType.企业钉钉机器人API: { if (!string.IsNullOrEmpty(_info.token)) { long dingTimestamp = (DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0).Ticks) / 10000; string canonicalString = $"{dingTimestamp}\n{_info.token}"; string signature = Convert.ToBase64String(Sign(Encoding.UTF8.GetBytes(canonicalString), Encoding.UTF8.GetBytes(_info.token))); item.URL = item.URL + $"×tamp={dingTimestamp}&sign={signature}"; } item.PostDataType = CsharpHttpHelper.Enum.PostDataType.Byte; item.PostdataByte = Encoding.UTF8.GetBytes(HttpHelper.ObjectToJson(new { msgtype = "text", text = new { content = _message } })); } break; case NoticeApiType.企业微信机器人API: { item.PostDataType = CsharpHttpHelper.Enum.PostDataType.Byte; item.PostdataByte = Encoding.UTF8.GetBytes(HttpHelper.ObjectToJson(new { msgtype = "text", text = new { content = _message } })); } break; case NoticeApiType.飞书机器人API: { item.PostDataType = CsharpHttpHelper.Enum.PostDataType.Byte; if (_info.api_location.Contains("bot/v2/hook")) item.PostdataByte = Encoding.UTF8.GetBytes(HttpHelper.ObjectToJson(new { msg_type = "text", content = new { text = _message } })); else item.PostdataByte = Encoding.UTF8.GetBytes(HttpHelper.ObjectToJson(new { title = "", text = _message })); } break; default: throw new Exception("无法识别的API类型!" + _info.notice_apitype.ToString()); } var html = http.GetHtml(item).Html; if (html.Contains("ok") || html.Contains("success")) return; throw new Exception(html); } catch (Exception ex) { if (ex.Message.Contains("sign not match")) { Thread.Sleep(370); continue; } exception = ex; EventClient.OnEvent(null, $"{_info.name}提醒失败:" + ex.Message); } } } }